调用EditText onfocuschange,但单击Button时HasFocus不返回false

时间:2014-04-14 20:32:14

标签: android android-listview android-edittext

我的活动中有一个listview,动态添加项目,我有一个EditText,其setOnFocusChangeListener为:

 holder.productQTYTxt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                public void onFocusChange(View v, boolean hasFocus) {
                    if (!hasFocus){
                        final int position = v.getId();
                        final EditText Caption = (EditText) v;
                        ((ListItem)myItems.get(position)).productQTY = Caption.getText().toString();
                    }
                }
            });

它的外观如下: enter image description here

你可以看到代码将EditText值保存在列表中,但仅在焦点丢失的情况下{ei !hasFocus

编辑:这是列表在父linearLayout中的活动的res,而在Child中的按钮

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:id="@+id/txtSelectedClient" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Select Client"
            android:id="@+id/o_btnSelect"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add Product"
            android:id="@+id/o_btnAdd"
            android:visibility="invisible"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Save"
            android:id="@+id/o_btnSave"
            android:visibility="invisible"/>
        </LinearLayout>
    <ListView android:id="@+id/orderList" android:layout_height="fill_parent"
        android:layout_width="fill_parent" android:descendantFocusability="beforeDescendants">
    </ListView>
</LinearLayout>

将其他EditText集中在列表中会触发它但单击按钮不会

1 个答案:

答案 0 :(得分:-1)

为什么不使用TextChangeListener而不是FocusListener(请参阅TextChangeListener)。您可以在onClick方法中请求按钮的焦点。然后触发FocusChange。 (我更喜欢TextChangeListener)

修改

回到TextChangeListener的想法。

创建一个新的(内部)类

class CustomTextWatcher implements TextWatcher {
    private final View mView;
    private final Type?! mMyItems;

    public CustomTextWatcher(View view, Type?! myItems) {
        mView = view;
        mMyItems = myItems;
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        final int position = mView.getId();
        final EditText Caption = (EditText) mView;
        ((ListItem)myItems.get(position)).productQTY = Caption.getText().toString();

    }
}

只需注册TextWatcher:

holder.productQTYTxt.addTextChangedListener(new CustomTextWatcher(holder.productQTYTxt, myItems));