【问题标题】:TextInputEditText fix character length with custom design underlineTextInputEditText 使用自定义设计下划线修复字符长度
【发布时间】:2017-05-31 05:09:58
【问题描述】:

我想设计如下:

在输入用户输入之前:

用户输入数字

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

一直在找设计讨论,

how can i underline each character in edittext?

Can I underline text in an android layout?

但它似乎与我想要的不太相符。

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

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

【问题讨论】:

  • 您可以使用 4 种不同的编辑文本制作此视图,并以编程方式管理每个编辑文本的焦点。从每个文本框的edittext合并值中获取值。设置每个文本框的最大长度为 1
  • 非常感谢。我也想到了这个实现。我只是想知道是否还有其他更优雅的方式来处理此类 UI 请求?
  • 请检查我的回答@rodent_la

标签: android android-layout android-edittext android-xml android-textinputedittext


【解决方案1】:

我的朋友试试这个

像这样创建一个 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) {

            }
        });

【讨论】:

  • 非常感谢。我会试试的。
猜你喜欢
  • 1970-01-01
  • 2019-11-25
  • 1970-01-01
  • 2019-12-01
  • 2013-10-30
  • 1970-01-01
  • 1970-01-01
  • 2020-01-20
  • 2019-05-11
相关资源
最近更新 更多