Android onFocusChange 被多次触发

标签 android android-edittext focus listener

我正在使用列表适配器(扩展 SimpleCursorAdapter)来填充一些数据。

我在 bindView 中调用 View.SetOnFocusChangeListener,我将监听器附加到所需的 EditText。问题是事件被触发 4 次,最终 EditText 失去焦点,直到我再次单击事件被触发 3 次并且 EditText 保持焦点。

我认为它应该只调用一次。 OnTouch 事件是否对此负责?

这是我的代码:

@Override
public void bindView(View view, Context context, Cursor cursor) {       
    view.setId(cursor.getPosition());//Setting the row id to the corresponding cursor row id for easier data manipulation
    super.bindView(view, context, cursor);
    EditText etItemQuantity = (EditText)view.findViewById(R.id.etItemQuantity);
    if(!isNumeric(etItemQuantity.getText().toString())) {
        etItemQuantity.setFocusable(false);
        etItemQuantity.setFocusableInTouchMode(false);
        etItemQuantity.setClickable(false);
    } else {
        etItemQuantity.setFocusable(true);
        etItemQuantity.setFocusableInTouchMode(true);
        etItemQuantity.setClickable(true);
        etItemQuantity.setOnFocusChangeListener(ofcl);
        etItemQuantity.setOnEditorActionListener(oeal);
    }

}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    View view = inflater.inflate(layout, null);
    return view;
}

View.OnFocusChangeListener ofcl = new View.OnFocusChangeListener() {

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (!(v instanceof EditText)) {                     
            return;
        }
        EditText tableEt = (EditText)v;

        if(hasFocus) {
            tableEt.setTag(tableEt.getText().toString());
            tableEt.setText("");
        } else {
            if(tableEt.getText().toString().equals("")) {           
                tableEt.setText(tableEt.getTag().toString());
                tableEt.setTag(null);
            }
        }


    }
};

最佳答案

在我的例子中,我在 API 24 中触发了两次 onFocusChange

editText.onFocusChangeListener = View.OnFocusChangeListener { _, hasFocus ->
    // ... hasFocus
}

container.setOnTouchListener { v, _ ->
    editText.clearFocus()
    hideKeyboard(editText)
    v.performClick()
    return@setOnTouchListener false
}

当用户在 EditText 之外单击时,我想从 EditText 中移除焦点。但是在 API 24 中,editText.clearFocus() 触发了 onFocusChange 两次。为了在 API 24 和 API 30 中设置类似的行为,我添加了 these行到 XML :

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/container"
    ....
    android:focusableInTouchMode="true"
    android:focusable="true">

    <EditText ... />

您可以更改代码而不是 XML,但在 API 30 中可能会出现新问题:

editText.clearFocus()
hideKeyboard(editText)
v.performClick()
v.requestFocus() // 1.
v.requestFocusFromTouch() // 2.
return@setOnTouchListener false

关于Android onFocusChange 被多次触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23761658/

相关文章:

android - 如何在我的 React Native 应用程序中显示 gif?

android - 如何在android/ios下用delphi app全程跟踪用户位置

android - 在编辑文本区域外部触摸时隐藏 android 中的键盘

android - AlertDialog 中的多个 EditText 对象

java - Swing 中的可链接键盘处理程序

matlab - 抑制 Matlab 窗口焦点窃取

Java 阻止 JComponent 的焦点

android - 如何处理 9-p​​atch 和 CardView 上的 Ripple 效果,并控制选择器的状态?

java - android中的日期错误存储在sqlite中

java - 是否有内置方法、InputType 或其他聪明的方法来使 Android EditText 小部件拒绝接受逗号?