TextInputEditText 使用自定义设计下划线修复字符长度

TextInputEditText fix character length with custom design underline

我想设计如下:

在输入用户输入之前:

用户输入数字

是否可以扩展 TextInputEditText 来实现这个要求?

一直在找设计讨论,

Can I underline text in an android layout?

但是好像不太符合我的要求。

起初我以为我会使用letterSpacing,但是下划线有问题。同样对于不同的屏幕分辨率和字体大小,letterSpacing 可能是问题所在,它不能放在一行中。我需要确保它在一行中。

有什么建议可以实现吗?如果有更好的方法,我想避免使用四个编辑文本来实现,是否可以在一个编辑文本中进行修改?

试试这个我的朋友

像这样创建一个 xml 布局文件

                <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="30dp"
                android:layout_marginLeft="60dp"
                android:layout_marginRight="60dp"
                android:layout_marginTop="20dp"
                android:orientation="horizontal"
                android:weightSum="4">

                <EditText
                    android:id="@+id/edDigit1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:hint="1"
                    android:imeOptions="actionNext"
                    android:inputType="number"
                    android:maxLength="1" />

                <EditText
                    android:id="@+id/edDigit2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:hint="2"
                    android:imeOptions="actionNext"
                    android:inputType="number"
                    android:maxLength="1" />

                <EditText
                    android:id="@+id/edDigit3"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:hint="3"
                    android:imeOptions="actionNext"
                    android:inputType="number"
                    android:maxLength="1" />

                <EditText
                    android:id="@+id/edDigit4"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:hint="4"
                    android:imeOptions="actionNext"
                    android:inputType="number"
                    android:maxLength="1" />

            </LinearLayout>

**now in your activity file**

      EditText edDigit1, edDigit2, edDigit3, edDigit4;
      edDigit1 = (EditText) findViewById(R.id.edDigit1);
      edDigit2 = (EditText) findViewById(R.id.edDigit2);
      edDigit3 = (EditText) findViewById(R.id.edDigit3);
      edDigit4 = (EditText) findViewById(R.id.edDigit4);

     edDigit1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (edDigit1.getText().toString().equals("")) {
                    edDigit1.requestFocus();
                } else {
                    edDigit2.requestFocus();
                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        edDigit2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (edDigit2.getText().toString().equals("")) {
                    edDigit2.requestFocus();
                } else {
                    edDigit3.requestFocus();
                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        edDigit3.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (edDigit3.getText().toString().equals("")) {
                    edDigit3.requestFocus();
                } else {
                    edDigit4.requestFocus();
                }


            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });