如何在列表视图中重点关注下一个编辑文本 [英] How to focus next edit text in listview

查看:113
本文介绍了如何在列表视图中重点关注下一个编辑文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Listview中的每一行都有2个EditText.这些行可能是n个数字.我想在第一行的第1个EditText中输入值,然后在软键盘中按next.焦点应该转到第一行的第二EditText.之后,我将在第二编辑文本中输入值,然后在软键盘中按下一步,焦点应移至第二行第一编辑文本.我附上了我的xml和适配器代码.

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>

我的适配器代码是:

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)无法正常工作.请帮助我解决我的问题.

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.

推荐答案

您可以通过设置

  • listview到android:descendantFocusability ="afterDescendants"
  • listview到setItemsCanFocus(true);
  • 将活动设置为
    android:windowSoftInputMode ="adjustResize"
  • listview to android:descendantFocusability="afterDescendants"
  • listview to setItemsCanFocus(true);
  • set your activity to
    android:windowSoftInputMode="adjustResize"

这篇关于如何在列表视图中重点关注下一个编辑文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆