android实现带下划线的密码输入框

本文介绍了一种无需自定义组件即可实现类似微信、支付宝带下划线密码输入框的方法。通过使用透明EditText和TextView组合,配合输入监听,实现了密码输入的隐藏与下划线效果。

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

现在很多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">

      

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LakeSideHu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值