如何在listview

时间:2016-09-09 05:00:51

标签: android listview android-edittext

Listview中的每一行都有2个EditText。行可能是n个数字。我想在第一行的第一个EditText中输入值,然后在软键盘中按下一个。重点应该放在第一行的第二个EditText中。之后,我将在第二个EditText中输入值,然后在软键盘中按下一个,焦点应该转到第2行第1个EditText。在此我附上了我的xml和适配器代码。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="5dp"
        android:layout_weight="10"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <com.vijay.textview.AutofitTextView
            android:layout_gravity="center_vertical"
            android:id="@+id/txt_component"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="6"
            android:fontFamily="sans-serif"
            android:text="@string/empty_string"
            android:textColor="@color/planned_schedule_list_item_color"
            android:textSize="@dimen/bol_listview_font_size"/>

        <com.vijay.edittext.AutoFitEditText
            android:id="@+id/edt_ready_to_go"
            android:paddingBottom="3dp"
            android:layout_marginTop="-15dp"
            style="@style/scan_text_fields_white_bg"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:fontFamily="sans-serif"
            android:inputType="number"
            android:textColor="@color/planned_schedule_list_item_color"
            android:textSize="@dimen/bol_listview_header_font_size"
            android:imeOptions="flagNoExtractUi"
            android:maxLength="5"
            android:nextFocusRight="@id/edt_damaged"/>

        <com.vijay.edittext.AutoFitEditText
            android:id="@+id/edt_damaged"
            android:paddingBottom="3dp"
            android:layout_marginTop="-15dp"
            style="@style/scan_text_fields_white_bg"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:fontFamily="sans-serif"
            android:inputType="number"
            android:textColor="@color/planned_schedule_list_item_color"
            android:textSize="@dimen/bol_listview_header_font_size"
            android:imeOptions="flagNoExtractUi"
            android:maxLength="5"/>
    </LinearLayout>
</LinearLayout>

我的适配器代码是:

holder.edtReadyToGo.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_NEXT) {
                if (position + 1 != mData.size()) {
                mListView.smoothScrollToPosition(position+1);
                mListView.postDelayed(new Runnable() {
                    public void run() {
                        AutoFitEditText nextField = (AutoFitEditText) holder.edtDamaged.focusSearch(View.FOCUS_RIGHT);
                        if (nextField != null) {
                            nextField.requestFocus();
                        }
                    }
                }, 200);
                return true;
               }

            }else if (actionId == EditorInfo.IME_ACTION_DONE) {
                holder.edtReadyToGo.setCursorVisible(false);
            }
            return false;
        }
    });

    holder.edtDamaged.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_NEXT) {
                if (position + 1 != mData.size()) {
                    mListView.smoothScrollToPosition(position+1);
                    mListView.postDelayed(new Runnable() {
                        public void run() {
                            AutoFitEditText nextField = (AutoFitEditText) holder.edtReadyToGo.focusSearch(View.FOCUS_DOWN);
                            if (nextField != null) {
                                nextField.requestFocus();
                            }
                        }
                    }, 200);
                    return true;
                }

            } else if (actionId == EditorInfo.IME_ACTION_DONE) {
                holder.edtDamaged.setCursorVisible(false);
            }
            return false;
        }
    });

这里我的第二个EditText(edt_damaged)正常工作。当我点击下一步时,它会自动聚焦到下一个字段。但是第一个EditText(edt_ready_to_go)无效。请帮我解决我的问题。

1 个答案:

答案 0 :(得分:0)

您可以尝试设置

  • listview to android:descendantFocusability =“afterDescendants”
  • listview to setItemsCanFocus(true);
  • 将您的活动设置为
    机器人:windowSoftInputMode = “adjustResize”