Android 输入数字后自动移动到下一个编辑文本

Android 输入数字后自动移动到下一个编辑文本,android,android-edittext,Android,Android Edittext,我有一个布局,其中我有4个圆形edittext输入PIN <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal"> <EditText and

我有一个布局,其中我有4个圆形
edittext
输入PIN

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/four_pin_background"
            android:inputType="number"
            android:textAlignment="center"
            android:maxLength="1"
            android:imeOptions="actionNext"
            android:cursorVisible="false"/>
  <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/four_pin_background"
            android:inputType="number"
            android:textAlignment="center"
            android:maxLength="1"
            android:imeOptions="actionNext"
            android:cursorVisible="false"/>

  <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/four_pin_background"
            android:inputType="number"
            android:textAlignment="center"
            android:maxLength="1"
            android:imeOptions="actionNext"
            android:cursorVisible="false"/>

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/four_pin_background"
            android:inputType="number"
            android:textAlignment="center"
            android:maxLength="1"
            android:imeOptions="actionNext"
            android:cursorVisible="false"/>

    </LinearLayout>

我想在为第一个
edittext
输入数字后,它应该自动移动到下一个
edittext
。 现在,在点击“下一步”
edittext
或在软键盘上点击“下一步”按钮后,我只能输入数字。
有人能帮我怎么做吗?

附上一个
TextWatcher

检查所需长度的
editText
输入,然后聚焦下一步

    editText.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable s) {
            if (s.length() == PIN_LENGTH) {
                focusNext();
            }
        }
    });

附加一个
TextWatcher

检查所需长度的
editText
输入,然后聚焦下一步

    editText.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable s) {
            if (s.length() == PIN_LENGTH) {
                focusNext();
            }
        }
    });

按照以下步骤操作,它将为您提供所需的结果:-

使用
editText
中的ID更新XML,如下所示:-

 <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal">

            <EditText
                android:id ="@+id/firstET"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/four_pin_background"
                android:inputType="number"
                android:textAlignment="center"
                android:maxLength="1"
                android:imeOptions="actionNext"
                android:cursorVisible="false"/>
      <EditText
               android:id ="@+id/secondET"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/four_pin_background"
                android:inputType="number"
                android:textAlignment="center"
                android:maxLength="1"
                android:imeOptions="actionNext"
                android:cursorVisible="false"/>

</LinearLayout>

按照以下步骤操作,它将为您提供所需的结果:-

使用
editText
中的ID更新XML,如下所示:-

 <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal">

            <EditText
                android:id ="@+id/firstET"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/four_pin_background"
                android:inputType="number"
                android:textAlignment="center"
                android:maxLength="1"
                android:imeOptions="actionNext"
                android:cursorVisible="false"/>
      <EditText
               android:id ="@+id/secondET"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/four_pin_background"
                android:inputType="number"
                android:textAlignment="center"
                android:maxLength="1"
                android:imeOptions="actionNext"
                android:cursorVisible="false"/>

</LinearLayout>
你需要使用

将TextWatcher添加到每当此EditText的文本更改时调用其方法的人的列表中

示例代码

    editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                                 // specify length of your editext here to move on next edittext
            if(editText.getText().toString().trim().length()>=1){
                NexteditText.requestFocus();
            }
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });
你需要使用

将TextWatcher添加到每当此EditText的文本更改时调用其方法的人的列表中

示例代码

    editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                                 // specify length of your editext here to move on next edittext
            if(editText.getText().toString().trim().length()>=1){
                NexteditText.requestFocus();
            }
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });

谢谢@Thunder,它对我有用。你能告诉我一件事吗?在键盘上点击X(后退)按钮应该会一个接一个地删除edittext中的数字。怎么做。欢迎@ananya,如果你点击“X”,它只会一个接一个地删除一个字符。谢谢@Thunder,它对我有效。你能告诉我一件事吗?在键盘上点击X(后退)按钮应该会一个接一个地删除edittext中的数字。怎么做。欢迎@ananya,如果你点击“X”,它只会一个接一个地删除一个字符