gpt4 book ai didi

android - TextInputEditText 的数据绑定(bind)和选择

转载 作者:行者123 更新时间:2023-12-03 10:42:53 26 4
gpt4 key购买 nike

我在 TextInputEditText 选择属性的正确数据绑定(bind)方面遇到问题。

这是xml

    <?xml version="1.0" encoding="utf-8"?><layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>
<variable name="inputViewModel" type="com.example.ui.TextInputViewModel"/>
</data>

<FrameLayout

android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.design.widget.TextInputLayout
android:id="@+id/layoutInput"
android:hint="@{inputViewModel.hint}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:errorEnabled="@{!inputViewModel.valid}"
app:error="@{inputViewModel.errorMessage}">

<android.support.design.widget.TextInputEditText
android:id="@+id/editInput"
android:text="@={inputViewModel.value}"
android:inputType="@{inputViewModel.inputType}"
android:maxLines="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:selection="@{inputViewModel.selection}"
app:setOnFocusChangeListener="@{inputViewModel.onFocusChangeListener}"
app:addTextChangedListener="@{inputViewModel.watcher}" />
</android.support.design.widget.TextInputLayout>
</FrameLayout>
</layout>

和 MVVM 类:
@Getter
@Setter
public class InputModel {
private String value;
private String errorMessage;
private boolean valid;
private String hint;
private int inputType;
private int selection;
}

public abstract class BaseInputViewModel extends BaseObservable{
protected InputModel mInputModel;

public BaseInputViewModel(InputModel inputModel){
this.mInputModel = inputModel;
}
}


public class TextInputViewModel extends BaseInputViewModel {

public TextInputViewModel() {
setWatcher(new TextWatcher() {

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Log.d("TextInputViewModel","getSelection1: " + getSelection());

}

@Override
public void onTextChanged(CharSequence value, int start, int before, int count) {
Log.d("TextInputViewModel","getSelection2: " + getSelection());

}

@Override
public void afterTextChanged(Editable s) {
Log.d("TextInputViewModel","getSelection3: " + getSelection());

}
});
}

@Bindable
public int getSelection() {
return mInputModel.getSelection();
}

@Bindable
public TextWatcher getWatcher() {
return watcher;
}

public void setWatcher(TextWatcher watcher) {
this.watcher = watcher;
notifyPropertyChanged(BR.watcher);
}
}
}



public class TextInputVie extends FrameLayout {

public TextInputVie(Context context) {
super(context);
initView(context, null);
}

public TextInputVie(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context, attrs);
}

void initView(@NonNull Context context, @Nullable AttributeSet attrs) {
mViewModel = createViewModel();

ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.text_input_layout, this, true);
binding.setVariable(BR.inputViewModel, mViewModel);
}

}

不幸阅读 getSelection()无论光标在编辑字段中的何处,始终返回 0。另一方面,如果我打破分隔规则并使用像 view.getSelectionStart() 这样的东西我得到了正确的结果。但这更像是一种 MVC/MVP 方法。有没有人知道如何绑定(bind)选择属性以使用数据绑定(bind)获取当前光标位置?

最佳答案

使用 Kotlin 和双向数据绑定(bind)

创建一个适配器来设置光标位置,如下所述:

@BindingAdapter("app:cursorPosition")
fun setCursorSelection(view:TextInputEditText, flag:Boolean) {
if (flag && view.text?.toString()?.length > 0) view.setSelection(view.text.toString().length)
}

通过传递您希望适配器设置光标位置的指示器天气,如下在您的 xml 中使用它:
 <android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/space_large"
android:layout_marginRight="@dimen/space_large"
android:layout_marginTop="@dimen/space_large"
android:hint="@string/enter_username"
app:errorText="@{(loginData.userNameErrorEnable &amp;&amp; loginData.name != null &amp;&amp; loginData.name.length() > 0 ) ? loginData.userNameError : null}"
style="@style/MaterialEditText"
app:layout_constraintTop_toBottomOf="@+id/headerImage">
<android.support.design.widget.TextInputEditText
android:id="@+id/usernameET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@={loginData.name}"
app:clearTextChangeListener="@{loginData.userNameWatcher}"
app:cursorPosition="@{loginData.name != null}" />
</android.support.design.widget.TextInputLayout>

关于android - TextInputEditText 的数据绑定(bind)和选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47039021/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com