【问题标题】:Databinding not working for EditText in Android数据绑定不适用于 Android 中的 EditText
【发布时间】:2021-05-15 16:34:26
【问题描述】:

我正在尝试在我的项目中实现一个简单的数据绑定。我试图从 EditText 中输入的文本中删除空格,但它在编译时失败。以下是代码。 有人可以帮忙吗?

自定义 BindingAdapter 类

    public class BindingAdapters {

    @BindingAdapter("nospace")
    public static void setText(TextView textView, boolean value) {
        
    }
} // I also tried with signature public static void setText(TextView textView, String value)

布局xml

       <EditText
        android:id="@+id/first_name"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        app:layout_constraintTop_toBottomOf="@+id/first_lastname"
        app:layout_constraintLeft_toLeftOf="parent"
        app:nospace="true" />

片段

@Override
public View onCreateView(@NonNull LayoutInflater inflater,
                         @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState ) {
    Log.w("MainFragment", "onCreateView...");
    binding = DataBindingUtil.inflate(inflater, R.layout.main_fragment, container, false);
    MyViewModel mainViewModel = MyViewModel .getInstance();
    binding.setMyViewModel(mainViewModel);
    return binding.getRoot();
}

视图模型

    public class MyViewModel extends ViewModel {

    private static MyViewModel myViewModel;
    private User user;

    private MyViewModel() {
    }

    public static MyViewModel getInstance() {
        if(myViewModel == null) {
            myViewModel = new MyViewModel();
        }
        return myViewModel;
    }
   }

我得到的错误是AAPT: error: attribute nospace (aka com.example.portfolio:nospace) not found.

【问题讨论】:

    标签: android data-binding


    【解决方案1】:

    这将为您完成工作。

     <EditText
            android:id="@+id/first_name"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            app:layout_constraintTop_toBottomOf="@+id/first_lastname"
            app:layout_constraintLeft_toLeftOf="parent"
            app:nospace="@{true}" /> //in databinding you should always use @{}
    

    【讨论】:

    • 谢谢兄弟。现在正在调用 adpater 方法。但它是在 Fragment 加载时调用的,即只有一次。我需要在每个字符条目上调用它。我需要做什么?
    【解决方案2】:

    大家好,我完成了全部要求。感谢您的支持。

       <EditText
        android:id="@+id/first_name"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        app:layout_constraintTop_toBottomOf="@+id/first_lastname"
        app:layout_constraintLeft_toLeftOf="parent"
        app:nospace="@{true}" />    
    
        public class BindingAdapters {
     
        @BindingAdapter("nospace")
        public static void setNospace(EditText editText, boolean value) {
            
            if(value) {
                editText.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
                    }
    
                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                        if(s != null && s.toString().contains(" ")) {
                            editText.setText(s.toString().replace(" ", ""));
                            editText.setSelection(editText.getText().length());
                        }
                    }
    
                    @Override
                    public void afterTextChanged(Editable s) {
    
                    }
                });
            }
        }
    

    【讨论】:

      【解决方案3】:

      你可以试试

      @BindingAdapter("app:nospace")
      public static void setText(TextView textView, boolean value) {
          
      }
      

      属性的全名需要匹配,所以要么在代码中包含 app: 要么在 xml 中移除 app:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-06
        • 2019-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-28
        相关资源
        最近更新 更多