TextWatcher 的数据绑定不起作用 Android
Databinding for TextWatcher is not working Android
我正在制作密码创建屏幕,我想在他们键入时显示密码强度指示器。
我的布局如下所示:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewModel"
type="com.example.PasswordViewModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:textChangedListener="@{viewModel.passwordTextWatcher}" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{viewModel.passwordQuality}"/>
</LinearLayout>
</layout>
PasswordViewModel.java 如下所示
public class PasswordViewModel extends BaseObservable {
private String password;
@Bindable
public String getPasswordQuality() {
if (password == null || password.isEmpty()) {
return "Enter a password";
} else if (password.equals("password")) {
return "Very bad";
} else if (password.length() < 6) {
return "Short";
} else {
return "Okay";
}
}
public void setPassword(String password) {
this.password = password;
notifyPropertyChanged(BR.passwordQuality);
}
@Bindable
public TextWatcher getPasswordTextWatcher() {
return new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// Do nothing.
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
setPassword(s.toString());
}
@Override
public void afterTextChanged(Editable s) {
// Do nothing.
}
};
}
最后
public class EditTextBindingAdapters {
@BindingAdapter("textChangedListener")
public static void bindTextWatcher(EditText editText, TextWatcher textWatcher) {
editText.addTextChangedWatcher(textWatcher);
}
}
但是我开始写的时候onTextChanged方法没有调用!谁能帮我解决这个问题?提前谢谢。
根据您对问题的描述,您似乎没有在 activity 中初始化绑定。如果是这样,那么
首先检查您是否已在 activity 中初始化绑定并像下面那样在其上设置 ViewModel
ActivityMainBinding activityMainBinding = null;
PasswordViewModel passwordViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
passwordViewModel = new PasswordViewModel();
activityMainBinding.setViewModel(passwordViewModel);
}
现在第二个问题是 addTextChangedWatcher
,添加 addTextChangedListener
代替它。
像这样
public class EditTextBindingAdapters {
@BindingAdapter("textChangedListener")
public static void bindTextWatcher(EditText editText, TextWatcher textWatcher) {
editText.addTextChangedListener(textWatcher);
}
}
我正在制作密码创建屏幕,我想在他们键入时显示密码强度指示器。 我的布局如下所示:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewModel"
type="com.example.PasswordViewModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:textChangedListener="@{viewModel.passwordTextWatcher}" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{viewModel.passwordQuality}"/>
</LinearLayout>
</layout>
PasswordViewModel.java 如下所示
public class PasswordViewModel extends BaseObservable {
private String password;
@Bindable
public String getPasswordQuality() {
if (password == null || password.isEmpty()) {
return "Enter a password";
} else if (password.equals("password")) {
return "Very bad";
} else if (password.length() < 6) {
return "Short";
} else {
return "Okay";
}
}
public void setPassword(String password) {
this.password = password;
notifyPropertyChanged(BR.passwordQuality);
}
@Bindable
public TextWatcher getPasswordTextWatcher() {
return new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// Do nothing.
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
setPassword(s.toString());
}
@Override
public void afterTextChanged(Editable s) {
// Do nothing.
}
};
}
最后
public class EditTextBindingAdapters {
@BindingAdapter("textChangedListener")
public static void bindTextWatcher(EditText editText, TextWatcher textWatcher) {
editText.addTextChangedWatcher(textWatcher);
}
}
但是我开始写的时候onTextChanged方法没有调用!谁能帮我解决这个问题?提前谢谢。
根据您对问题的描述,您似乎没有在 activity 中初始化绑定。如果是这样,那么
首先检查您是否已在 activity 中初始化绑定并像下面那样在其上设置 ViewModel
ActivityMainBinding activityMainBinding = null;
PasswordViewModel passwordViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
passwordViewModel = new PasswordViewModel();
activityMainBinding.setViewModel(passwordViewModel);
}
现在第二个问题是 addTextChangedWatcher
,添加 addTextChangedListener
代替它。
像这样
public class EditTextBindingAdapters {
@BindingAdapter("textChangedListener")
public static void bindTextWatcher(EditText editText, TextWatcher textWatcher) {
editText.addTextChangedListener(textWatcher);
}
}