现在很多app的密码输入框,都采用微信、支付宝等密码输入框的样式。还有一种就是每个密码字符下面带有一条下划线的样式。
仿微信、支付宝网上搜了下有很多demo,但是带下划线的不多,或者讲的比较复杂,都是自定义什么的。这两天正好要做个这样的东西,研究了一下,感觉没那么麻烦,不需要各种自定义。我的思路是:
1). 布局中定义一个edittext, 全透明,输入的字符大小为0sp, 这样用户就看不见。不能设置为visibility=gone,否则无法获取焦点,弹 不出输入法。
2). 然后定义四个textview, 来显示edittext的输入。同时定义四个下划线,分别在每个textview的下方。
3). 监听edittext的输入事件,控制每个textview的显示
1. 布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/input_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginTop="24dp"
android:text="请输入密码"
android:textSize="20sp"
android:textColor="#333333"/>
<EditText
android:id="@+id/input_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:inputType="number"
android:digits="0123456789"
android:maxLength="4"
android:maxLines="1"
android:background="@null"
android:cursorVisible="false"
android:textColor="#00000000"
android:textSize="0sp"
android:visibility="visible"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@+id/input_title"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="24dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:layout_marginRight="45dp"
android:paddingBottom="10dp">
android实现带下划线的密码输入框
最新推荐文章于 2023-10-09 17:36:30 发布
本文介绍了一种无需自定义组件即可实现类似微信、支付宝带下划线密码输入框的方法。通过使用透明EditText和TextView组合,配合输入监听,实现了密码输入的隐藏与下划线效果。
开发板推荐:天空星STM32F407VET6开发板
超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印
开发板推荐:天空星STM32F407VET6开发板
超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印


3064

被折叠的 条评论
为什么被折叠?



