如何避免在Android';编辑文本
如何避免出现以下代码行:如何避免在Android';编辑文本,android,android-edittext,Android,Android Edittext,如何避免出现以下代码行: ((EditText) findViewById(R.id.MyEditText)).setText("Hello"); 将在此处引发事件: ((EditText) findViewById(R.id.MyEditText)).addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before,
((EditText) findViewById(R.id.MyEditText)).setText("Hello");
将在此处引发事件:
((EditText) findViewById(R.id.MyEditText)).addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
// HERE
}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
我想知道,在选择AutoCompleteTextView的下拉结果(没有执行任何onTextChanged!)的情况下,是否有任何方法可以禁止执行onTextChanged
我不是在寻找“if hello do nothing”(如果你好,什么都不做)之类的解决方法。我认为没有一种简单的方法可以禁用侦听器,但您可以通过以下两种方式来绕过它:
- 在设置文本之前删除
,然后重新添加TextWatcher
- 或者在设置文本之前设置一个
标志,告诉boolean
忽略它TextWatcher
文本观察程序中
:
new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
/*
If the flag is set to ignore the next text change,
reset the flag, and return without doing anything.
*/
if (ignoreNextTextChange){
ignoreNextTextChange = false;
return;
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
这有点粗糙,但应该可以用:)我只能从两个方面看问题
- 您可以在侦听器中进行检查
- 您可以根据特定条件添加/删除侦听器
mBlockCompletion = true;
replaceText(convertSelectionToString(selectedItem));
mBlockCompletion = false;
这是实现你想做的事情的最好方法。
然后检查setText是否已通过完成并从方法中返回
void doBeforeTextChanged() {
if (mBlockCompletion) return;
对于应用程序来说,添加和删除TextWatcher将花费更多的时间实现这一点的另一种方法是使用
setOnKeyListener
这只会在用户按键时触发,而不是在用户或以编程方式更改EditText时触发
myEditText.setOnKeyListener(new EditText.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch(event.getAction()) {
case KeyEvent.ACTION_DOWN:
// beforeTextChanged
break;
case KeyEvent.ACTION_UP:
// afterTextChanged
break;
}
return false;
}
});
您可以检查哪个视图当前具有焦点,以区分用户和程序触发的事件
EditText myEditText = (EditText) findViewById(R.id.myEditText);
myEditText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(myEditText.hasFocus()) {
// is only executed if the EditText was directly changed by the user
}
}
//...
});
查看该答案的更详细版本。或者,您可以简单地使用
mEditText.hasFocus()
来区分人工更改的文本或程序更改的文本,这对我来说很好:
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
if (mEditText.hasFocus()) {
// Do whatever
}
}
希望这有帮助 设置文本将触发该侦听器。一种简单的方法是引用
TextWatcher
,在设置文本之前删除侦听器(使用removeTextChangedListener(watcher)
),设置文本,重新启用侦听器(使用addTextChangedListener(watcher)
)。为了方便起见,如果您多次这样做,您可以在方法中使用此选项。同一问题的示例可能重复,以便在复选框上使用侦听器更新,这将阻止在文本更改之前和之后执行,或者您是否需要另一个布尔检查?有趣的想法,但是它会捕捉到用户将文本粘贴到字段中所做的文本更改吗?我不知道它如何帮助创建解决方案。我的问题没有想到这一点。
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
if (mEditText.hasFocus()) {
// Do whatever
}
}