[英]How to add more than one text changed listener in a layout?
在代碼中,我只會做:
myEditText.addTextChangedListener(firstTextWatcher);
myEditText.addTextChangedListener(secondTextWatcher);
在xml中,這有效:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:addTextChangedListener="@{viewModel.firstTextWatcher}"/>
但是,這:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:addTextChangedListener="@{viewModel.firstTextWatcher}"
app:addTextChangedListener="@{viewModel.secondTextWatcher}"/>
結果是:
重復屬性addTextChangedListener
你知道為什么會這樣嗎? 我正在使用MVVM,因此我想從數據綁定中受益。 我知道我可以將這兩個TextWatcher合並為一個,但這真的有必要嗎?
您還想給EditTexts一個ID,可以通過XML來實現,例如: android:id="@+id/box1"
而不是在XML文件中執行此操作,而是要在Java中執行此操作。 這是您需要的所有代碼片段,但是您必須將它們放置! 您還需要為第二個EditText復制這些,因為這只是一個:
private EditText box1;
final EditText box1 = (EditText) findViewByID(R.id.box1);
box1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(final CharSequence txt,int s, int b, int c) {
}
});
這應該工作。 在更改的文本中,我不確定int s
, int b
和int c
是做什么的。 我只是在下面使用此方法來鍵入: https : //developer.android.com/reference/android/text/TextWatcher.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.