0

如果我有一个 EditText 允许用户只输入 6 位数字。如何在每个数字下创建一条线,每个数字之间有一个小空间。

像这样

其他问题正在解决仅将虚线用作布局中的分隔线的情况。

问题 1

问题2

4

3 回答 3

6

首先创建一个可绘制文件 (myline.xml) 来创建线条。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:bottom="1dp"
    android:left="-2dp"
    android:right="-2dp"
    android:top="-2dp">
    <shape android:shape="rectangle">
        <stroke
            android:width="1dp"
            android:color="@android:color/black"
            <!-- Change Gap & Width accroding to your need. -->
            android:dashGap="3.1dp"
            android:dashWidth="7dp" />
    </shape>
</item>

现在在您的 layout.xml 中使用myline.xml在编辑文本背景中。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter Digit: "
        android:textColor="#000"
        android:textSize="18dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/myline"
        android:lines="1"
        android:maxLength="6"
        android:text="123456"
        android:textColor="#000"
        android:textSize="18dp" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter Digit: "
        android:textColor="#000"
        android:textSize="18dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/myline"
        android:lines="1"
        android:maxLength="6"
        android:text="123"
        android:textColor="#000"
        android:textSize="18dp" />
</LinearLayout>

完成后,请参阅下面屏幕截图中的输出。

在此处输入图像描述

于 2017-07-24T07:49:34.730 回答
3

这是一个可以解决您的问题的库。

https://android-arsenal.com/details/1/5999

于 2017-07-24T07:38:49.750 回答
1

如果 EditText 的输入只是数字,请使用它。

String zeroLeading = String.format("%06d", number)

这将返回零前导和 6 长度文本。然后替换零

zeroLeading = zeroLeading.replaceAll("0","_");

在某些情况下 _ _ 2 0 4 5 像这样。有点不同

String zeroLeading = String.format("%06d", number);
    int numberLen = (number+"").length();
    String underlineLeading = zeroLeading.substring(0, numberLen).replaceAll("0", "_")+zeroLeading.substring(numberLen, 6);
于 2017-07-24T10:00:40.290 回答