【问题标题】:Android: Dashed line under EditText with fixed sizeAndroid:EditText下的虚线具有固定大小
【发布时间】:2017-07-24 07:18:23
【问题描述】:

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

其他问题是解决将虚线仅用作布局中的分隔线的方案。

Question 1

Question 2

【问题讨论】:

  • 你能解释一下你到底想要什么吗?我的理解是您需要编辑只有 6 位长度和虚线的文本,对吗?
  • @AndyDeveloper 是的,这正是我想要的。我会添加一张照片更清晰
  • 好的,明白了。还有一个问题您需要在每个字符中使用点线,或者整个编辑文本只需简单的点线。
  • 也许你可以在线性布局中添加 6 个不同的编辑文本,水平方向,最大长度只有一个,并为每个编辑文本添加可绘制样式以在编辑文本中创建底线。
  • @Motassem Jalal 看到我的回答先生。

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


【解决方案1】:

首先创建一个可绘制的文件(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>

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

【讨论】:

  • 嗯,这是一个很好的答案,但它会因为尺寸太复杂。我的意思是您输入的相同尺寸对我不起作用。我相信每个设备都不一样。
【解决方案2】:

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

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

【讨论】:

  • 这个库正好提供了我需要的东西,但我还有一个关于它的问题。上面写着我应该添加一个监听器来获取文本。是否可以像普通的editText(使用getText()方法)以另一种方式获取?
  • 有可能,但为此您需要检查 github 上的库代码。我没有检查代码。
【解决方案3】:

如果 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);

【讨论】:

    猜你喜欢
    • 2010-12-23
    • 2011-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-13
    • 2013-01-09
    相关资源
    最近更新 更多