本文介绍了Android onFocusChange 被多次触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using a List Adapter (extends SimpleCursorAdapter) to populate some data.

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

I'm calling View.SetOnFocusChangeListener in bindView where i attach the listener to the desired EditText. The problem is that the event is fired 4 times where in the end the EditText loses it's focus, until i click again where the event is fired 3 times and the EditText retains focus.

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

I think it should only be called once.Is the OnTouch event responsible for this?

这是我的代码:

@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);
            }
        }


    }
};

推荐答案

在清单中添加以下行 int my activity 解决了问题:

Adding the following line int my activity in the manifest fixed the problem:

        android:windowSoftInputMode="adjustPan"

不知道为什么.

这篇关于Android onFocusChange 被多次触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:40