OnFocusChange并不总是工作 [英] OnFocusChange not always working

查看:539
本文介绍了OnFocusChange并不总是工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的活动之一,我有三个的EditText 和一个确定按钮。在 OnFocusChangeListener 设置为三个的EditText 秒。监听器应该引起每一个焦点失去的时间。

的EditText 作品完美地之间的切换。但是,如果用户presses OK按钮没有焦点变化(失去焦点)引发的的EditText 用户集中pressing按钮之前。

这有什么错我的code?

 私有类MyOnFocusChangeListener实现OnFocusChangeListener {
    私人的EditText EDITTEXT;    公共MyOnFocusChangeListener(最终的EditText EDITTEXT){
        超();        this.editText = EDITTEXT;
    }    @覆盖
    公共无效onFocusChange(最终查看视图,最终布尔isFocused){
        如果(!isFocused){
            如果(EDITTEXT == editText1){
                //做一个计算
            }否则如果(EDITTEXT == editText2){
                //另做计算
            }否则如果(EDITTEXT == editText3){
                //做一个不同的计算
            }
        }
    }
}@覆盖
公共无效的onCreate(最终束束){
    // ...
    editText1.setOnFocusChangeListener(新MyOnFocusChangeListener(editText1));
    editText2.setOnFocusChangeListener(新MyOnFocusChangeListener(editText2));
    editText3.setOnFocusChangeListener(新MyOnFocusChangeListener(editText3));
    // ...
}


解决方案

您可以尝试清除焦点时确定或其他按钮用户点击....

例如

  builder.setPositiveButton(android.R.string.ok,新DialogInterface.OnClickListener()
 {
     公共无效的onClick(DialogInterface对话,诠释whichButton)
     {
          editText1.clearfocus();
          editText2.clearfocus();
          editText3.clearfocus();
          ....
     }
 }

In one of my activities I have three EditTexts and an OK button. The OnFocusChangeListener is set to all three EditTexts. The listener should trigger every time the focus is lost.

Switching between EditTexts works perfectly. But if the user presses the OK button there's no focus change (losing the focus) triggered for the EditText the user focused before pressing the button.

What's wrong with my code?

private class MyOnFocusChangeListener implements OnFocusChangeListener {
    private EditText editText;

    public MyOnFocusChangeListener(final EditText editText) {
        super();

        this.editText = editText;
    }

    @Override
    public void onFocusChange(final View view, final boolean isFocused) {
        if (!isFocused) {
            if (editText == editText1) {
                // Do a calculation
            } else if (editText == editText2) {
                // Do another calculation
            } else if (editText == editText3) {
                // Do a different calculation
            }
        }
    }
}

@Override
public void onCreate(final Bundle bundle) {
    // ...
    editText1.setOnFocusChangeListener(new MyOnFocusChangeListener(editText1));
    editText2.setOnFocusChangeListener(new MyOnFocusChangeListener(editText2));
    editText3.setOnFocusChangeListener(new MyOnFocusChangeListener(editText3));
    // ...
}

解决方案

You could try to clear the focus when user click on OK or other button....

e.g.

 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() 
 {
     public void onClick(DialogInterface dialog, int whichButton) 
     {
          editText1.clearfocus();
          editText2.clearfocus();
          editText3.clearfocus();
          ....
     }
 }

这篇关于OnFocusChange并不总是工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆