首页 > 解决方案 > 如何给更多的EditText字段一种单行的感觉

问题描述

我的代码中有 4 个编辑文本字段,用户必须在其中插入 4 个数字。我想让 4 个字段相互连接,就像一个文本字段一样。意思是,如果我输入了第一个数字,它应该直接跳转到第二个编辑文本,依此类推。删除它应该从 4 返回到 1,而无需用户选择每个字段并删除插入的值

我该如何管理?

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/number_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >


        <EditText
            android:id="@+id/numberone"
            android:layout_width="@dimen/edit_text"
            android:layout_height="@dimen/edit_text_h"
            android:inputType="number"
            android:maxLength="1"
            android:nextFocusDown="@id/numbertwo"
            />


        <EditText
            android:id="@+id/numbertwo"
            android:layout_width="@dimen/edit_text"
            android:layout_height="@dimen/edit_text_h"
            android:inputType="number"
            android:maxLength="1"
            android:nextFocusDown="@id/numberthree"
            />


        <EditText
            android:id="@+id/numberthree"
            android:layout_width="@dimen/edit_text"
            android:layout_height="@dimen/edit_text_h"
            android:inputType="number"
            android:maxLength="1"
            android:nextFocusDown="@id/numberfour"
            />


        <EditText
            android:id="@+id/numberfour"
            android:layout_width="@dimen/safe_edit_text"
            android:layout_height="@dimen/safe_edit_text_h"
            android:inputType="number"
            android:maxLength="1"
            android:nextFocusDown="@id/btn_confirmcode"
            />


    </androidx.constraintlayout.widget.ConstraintLayout>

标签: androidxmlkotlinandroid-edittext

解决方案


我把这个留给有同样问题的人:

 for(i in 1..4) {
                        val layoutID = this.resources
                                .getIdentifier("editText$i","id",this.activity!!.packageName)
                        val currentEditText = view.findViewById<EditText>(layoutID)

                        currentEditText.addTextChangedListener(object:TextWatcher{

                            override fun afterTextChanged(p0: Editable?) {
                                Log.e("p0?.length",p0?.length.toString())
                                if (p0?.length!! > 0) {
                                    //change focus to the next edittext
                                    if(i in 1..3){
                                        val layoutIDnext = this.resources
                                                .getIdentifier("editText"+(i+1),
                                                        "id",
                                                        this.activity!!.packageName)
                                        val nextEditText = view.findViewById<EditText>(layoutIDnext)
                                        nextEditText.requestFocus()
                                    }

                                } else if (p0?.length == 0) {
                                    //change focus to the previous edit text

                                    if(i in 2..4) {
                                        val layoutIdbefore = this.resources.getIdentifier("editText"+(i-1),
                                                "id",
                                                this.activity!!.packageName)
                                        val lastEditText = view.findViewById<EditText>(layoutIdbefore)
                                        lastEditText.requestFocus()

                                        currentEditText.setOnKeyListener { _, _, keyEvent ->
                                            if(keyEvent.keyeditText == KeyEvent.KEYeditText_DEL) {
                                                lastEditText.requestFocus()
                                            }
                                            return@setOnKeyListener false
                                        }

                                    }

                                }}

                            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                                }

                            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

                            }
                        })
                    }

推荐阅读