1

我在 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 方法。有没有人知道如何使用数据绑定绑定选择属性以获取当前光标位置?

4

1 回答 1

0

使用 Kotlin 和双向数据绑定

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

@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>
于 2018-10-12T17:56:17.610 回答