How to focus next edit text in listview

牧云@^-^@ 提交于 2019-12-11 04:36:23

问题


I have 2 EditText for each row in Listview. The rows maybe n numbers. I want to enter the value in 1st EditText in first row and then press next in soft keyboard. The focus should be go to 2nd EditText in 1st row. After than I will enter the value in 2nd EditText and press next in soft keyboard the focus should go to 2nd row 1st EditText. Herewith I attached my xml and adapter code.

<?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>

My adapter code is:

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

Here my 2nd EditText(edt_damaged) working fine. When I click next it automatically focus to next field. But 1st EditText(edt_ready_to_go) not working. Please help me to resolve my issue.


回答1:


You can try by setting

  • listview to android:descendantFocusability="afterDescendants"
  • listview to setItemsCanFocus(true);
  • set your activity to
    android:windowSoftInputMode="adjustResize"


来源:https://stackoverflow.com/questions/39404149/how-to-focus-next-edit-text-in-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!