Android onFocusChange()的行为类似于textChanged()

Android onFocusChange()的行为类似于textChanged(),android,android-edittext,onfocus,Android,Android Edittext,Onfocus,我在EditText中添加了一个OnFocusChangeListener,以便在用户单击EditText视图外部或选项卡时对值进行验证: EditText myEditText = (EditText) itemView.findViewById(R.id.myedittext); myEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocu

我在EditText中添加了一个OnFocusChangeListener,以便在用户单击EditText视图外部或选项卡时对值进行验证:

  EditText myEditText = (EditText) itemView.findViewById(R.id.myedittext);
  myEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
  @Override
  public void onFocusChange(View view, boolean hasFocus) {
    EditText editText = (EditText)view;
    if (!hasFocus) {
      validate(editText.getText().toString());
    }
  }
}
但是,onFocusChange()方法的调用似乎与textChanged()方法类似。每次我在文本字段中输入任何内容都会调用它。我在2.2模拟器和我自己的硬件设备上都试过了,它们的作用是一样的

如果您能深入了解OnFocusChangeListener应该如何工作,以及它为什么以我的方式工作,我将不胜感激


提前谢谢。

首先,您不必在
onFocusChange
方法中重新声明EditText。myEditText应该是在
onCreate
方法中设置的类变量。我认为您的代码将其视为两个独立的实例

然后尝试添加
OnEditorActionListener

myEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            validate(myEditText.getText().toString());
            return true;
        }
        return false;
    }
});

我还遇到了这个问题,没有找到有关处理
onFocusChange()
以及
EditText
文本更改的详细信息

我确信每次调用
[editText].setText()
时都会调用
onFocusChange()
。在本例中,我希望在输入完成后验证输入文本,或者
hasFocus()
返回false。验证之后,将文本设置为agian。因此,这两个进程
onFocusChange()
[editText].setText()
正在相互中断

解决方案

用这样的东西

 meditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {

                yourcalc();

                return true;
            }
            return false;
        }
    });