我在 TextInputEditText 选择属性的正确数据绑定方面遇到问题。
这是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 方法。有没有人知道如何使用数据绑定绑定选择属性以获取当前光标位置?