如何实现这样的EditText

时间:2017-09-17 22:16:12

标签: android android-edittext

我想创建一个字符EditText enter image description here

我是否必须制作多个EditText或者还有其他方式

1 个答案:

答案 0 :(得分:2)

制作Multi-EditText是实现此目的的最简单方法。请把我的代码作为参考。它是一个数字 - EditText。输入前一个Edittext的数字后,它会将焦点切换到下一个editText XML:

<EditText
            android:id="@+id/id1"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:background="@drawable/rounded_edittext"
            android:focusableInTouchMode="true"
            android:gravity="center"
            android:inputType="number"
            android:maxLength="1"
            android:nextFocusForward="@+id/id2" />

        <EditText
            android:id="@+id/id2"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:background="@drawable/rounded_edittext"
            android:gravity="center"
            android:inputType="number"
            android:maxLength="1"
            android:nextFocusForward="@+id/id3" />

        <EditText
            android:id="@+id/id3"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:background="@drawable/rounded_edittext"
            android:gravity="center"
            android:inputType="number"
            android:maxLength="1"
            android:nextFocusForward="@+id/id4" />

        <EditText
            android:id="@+id/id4"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:background="@drawable/rounded_edittext"
            android:gravity="center"
            android:inputType="number"
            android:maxLength="1" />

JavaClass

e1 = (EditText) rootView.findViewById(R.id.id1);
e2 = (EditText) rootView.findViewById(R.id.id2);
e3 = (EditText) rootView.findViewById(R.id.id3);
e4 = (EditText) rootView.findViewById(R.id.id4);

e1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int count) {
                    if (count == 1) {
                        e2.requestFocus();
                    }
                }
            });

e2.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int count) {
                    if (count == 1) {
                        e3.requestFocus();
                    }
                }
            });

e3.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int count) {
                    if (count == 1) {
                        e4.requestFocus();
                    }
                }
            });