以下是一个使用Android数据绑定来观察包装在自定义视图中的EditText的值的示例解决方法:
android {
...
dataBinding {
enabled = true
}
}
public class CustomViewModel extends BaseObservable {
private String editTextValue;
@Bindable
public String getEditTextValue() {
return editTextValue;
}
public void setEditTextValue(String editTextValue) {
this.editTextValue = editTextValue;
notifyPropertyChanged(BR.editTextValue);
}
}
public class MainActivity extends AppCompatActivity {
private CustomViewModel customViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
customViewModel = new CustomViewModel();
binding.setCustomViewModel(customViewModel);
}
}
这样,您就可以在自定义视图中使用Android数据绑定来观察包装在EditText中的值的变化。