Android实现密码明密文切换(小眼睛)
作者:JH学编程 发布时间:2023-07-01 15:42:18
标签:Android,密码,密文
本文实例为大家分享了Android实现密码明密文切换的具体代码,供大家参考,具体内容如下
小眼睛在密码栏右边!
奉上我使用的素材:
添加图片到res/darwable中
对安卓的知识掌握的非常浅,只知道 图片名称不要大写,大写会报错!
如果格式正确仍会报错的话,则 在gradle里加上这两句,俺也不懂为什么,都没有讲原理的。
aaptOptions.cruncherEnabled = false
aaptOptions.useNewCruncher = false
编辑登录页.xml
文本+可编辑文本框+小眼睛图片+按钮
小眼睛只要写一个ImageView即可
<LinearLayout
? ? ? ? ? ? android:id="@+id/ll_username"
? ? ? ? ? ? android:layout_below="@id/iv"
? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:layout_marginTop="60dp"
? ? ? ? ? ? android:layout_marginLeft="10dp"
? ? ? ? ? ? android:layout_marginRight="10dp"
? ? ? ? ? ? android:layout_marginBottom="5dp"
? ? ? ? ? ? android:layout_centerVertical="true"
? ? ? ? ? ? android:background="#6B009688">
? ? ? ? <TextView
? ? ? ? ? ? ? ? android:id="@+id/tv_login_username"
? ? ? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? ? ? android:text="账号:"
? ? ? ? ? ? ? ? android:padding="10dp"
? ? ? ? ? ? ? ? android:textSize="20dp"
? ? ? ? ? ? ? ? android:textColor="@color/white"/>
? ? ? ? <EditText
? ? ? ? ? ? ? ? android:id="@+id/et_login_username"
? ? ? ? ? ? ? ? android:maxLines="1"
? ? ? ? ? ? ? ? android:maxLength="16"
? ? ? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? ? ? android:layout_marginLeft="10dp"
? ? ? ? ? ? ? ? android:background="@null"/>
? ? </LinearLayout>
? ? <LinearLayout
? ? ? ? ? ? android:id="@+id/ll_password"
? ? ? ? ? ? android:layout_below="@id/ll_username"
? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:layout_marginLeft="10dp"
? ? ? ? ? ? android:layout_marginRight="10dp"
? ? ? ? ? ? android:layout_centerVertical="true"
? ? ? ? ? ? android:background="#6B009688">
? ? ? ? <TextView
? ? ? ? ? ? ? ? android:id="@+id/tv_login_password"
? ? ? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? ? ? android:text="密码:"
? ? ? ? ? ? ? ? android:padding="10dp"
? ? ? ? ? ? ? ? android:textSize="20dp"
? ? ? ? ? ? ? ? android:textColor="@color/white"/>
? ? ? ? <EditText
? ? ? ? ? ? ? ? android:id="@+id/et_login_password"
? ? ? ? ? ? ? ? android:maxLines="1"
? ? ? ? ? ? ? ? android:maxLength="6"
? ? ? ? ? ? ? ? android:layout_width="255dp"
? ? ? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? ? ? android:layout_marginLeft="10dp"
? ? ? ? ? ? ? ? android:background="@null"/>
? ? ? ? <ImageView android:layout_width="20dp"
? ? ? ? ? ? ? ? ? ?android:layout_height="20dp"
? ? ? ? ? ? ? ? ? ?android:layout_marginTop="14dp"
? ? ? ? ? ? ? ? ? ?android:id="@+id/display_password"/>
? ? </LinearLayout>
? ? <LinearLayout
? ? ? ? ? ? android:id="@+id/ll_btm"
? ? ? ? ? ? android:layout_below="@id/ll_password"
? ? ? ? ? ? android:gravity="center"
? ? ? ? ? ? android:orientation="vertical"
? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? android:layout_height="wrap_content">
? ? ? ? <Button
? ? ? ? ? ? ? ? android:id="@+id/btn_login"
? ? ? ? ? ? ? ? android:layout_width="300dp"
? ? ? ? ? ? ? ? android:layout_height="50dp"
? ? ? ? ? ? ? ? android:layout_marginTop="50dp"
? ? ? ? ? ? ? ? android:text="登录"
? ? ? ? ? ? ? ? android:textSize="18dp"
? ? ? ? ? ? ? ? android:background="@color/white"
? ? ? ? />
? ? </LinearLayout>
编辑登录页小眼睛功能.java
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
? ? private EditText loginUsername;
? ? private EditText loginPassword;
? ? private Button login;
? ? private ImageView displayPassword;
? ? private boolean isHideFirst = false;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_login);
? ? ? ? ActionBar actionBar = getSupportActionBar();
? ? ? ? if (actionBar != null) {
? ? ? ? ? ? actionBar.hide();
? ? ? ? }
? ? ? ? //隐藏标题栏
? ? ? ? login = findViewById(R.id.btn_login);
? ? ? ? loginUsername = findViewById(R.id.et_login_username);
? ? ? ? loginPassword = findViewById(R.id.et_login_password);
? ? ? ? displayPassword = findViewById(R.id.display_password);
? ? ? ? login.setOnClickListener(this);
? ? ? ? displayPassword.setOnClickListener(this);
? ? ? ? displayPassword.setImageResource(R.drawable.open);
? ? }
? ? @Override
? ? public void onClick(View v){
? ? ? ? switch (v.getId()) {
? ? ? ? ? ? case R.id.display_password:{
? ? ? ? ? ? ? ? if (isHideFirst) {
? ? ? ? ? ? ? ? ? ? displayPassword.setImageResource(R.drawable.open);
? ? ? ? ? ? ? ? ? ? HideReturnsTransformationMethod method1 = HideReturnsTransformationMethod.getInstance();
? ? ? ? ? ? ? ? ? ? loginPassword.setTransformationMethod(method1);
? ? ? ? ? ? ? ? ? ? isHideFirst = false;
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? displayPassword.setImageResource(R.drawable.close);
? ? ? ? ? ? ? ? ? ? TransformationMethod method = PasswordTransformationMethod.getInstance();
? ? ? ? ? ? ? ? ? ? loginPassword.setTransformationMethod(method);
? ? ? ? ? ? ? ? ? ? isHideFirst = true;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? int index = loginPassword.getText().toString().length();
? ? ? ? ? ? ? ? loginPassword.setSelection(index);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ? ? case R.id.btn_login: {
?? ??? ??? ??? ?//。。。。。
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
来源:https://blog.csdn.net/m0_46651408/article/details/117820998


猜你喜欢
- 数组翻转的方法(java实现),数组翻转,就是将数组倒置,例如原数组为:{"a","b","
- 现在很多app一打开就是一个ViewPager,然后可以用手指滑,每滑一次就换一张图,底下还会有圈圈表示说现在滑到第几章~通常这些图片都是放
- Maven 错误找不到符号问题,通常有三种原因: 1. 可能项目编码格式不统一。 2. 可能项目编码使用的JDK版本不统一。 3
- mysql插件实现原理官网的关键信息参考文档https://mybatis.org/mybatis-3/zh/configuration.h
- 前言前两篇我们详细了解了 findById 和 findAll 以及 findAll 的分页查询,如果说JPA只有上面的两种查询功能,那就太
- 应朋友们反馈的Android基础薄弱的问题,决定出一套Android基础教程,帮助大家复习,巩固Android基础,今天要讲的是Androi
- System.Collections.ArrayList类是一个特殊的数组。通过添加和删除元素,就可以动态改变数组的长度。一.优点1. 支持
- 访问登记属性 android.permission.ACCESS_CHECKIN_PROPERTIES ,读取或写入登记check-in数据
- 1、Json的制作package com.example.usingjson2; import org.json.
- 讲完了inbound事件和outbound事件的传输流程, 这一小节剖析异常事件的传输流程传播异常事件简单的异常处理的场景@Override
- 本文实例讲述了Android在JNI中使用ByteBuffer的方法。分享给大家供大家参考。具体如下:一、ByteBuffer 定义在NIO
- 一、Android前端实现新建一个login的项目,主要的几个文件在这里1、gradle引入OKhttp3依赖implementation
- 最近部分采用了TDD的方法来开发一个模块,小有收获特此总结一下:1. TDD的基本原则TDD的最核心思想就是先明确需求,且用代码的方式量化,
- 1.什么是String?首先,初学者一定要知道String是一个类,它是字符串类型,但它不属于基本数据类。 所谓字符串类型,意思就好比将字符
- 1.问题由来迷宫实验是取自心理学的一个古典实验。在该实验中,把一只老鼠从一个无顶大盒子的门放入,在盒中设置了许多墙,对行进方向形成了多处阻挡
- 本文实例讲述了Android使用ContentResolver搜索手机通讯录的方法。分享给大家供大家参考,具体如下:在这个程序中使用Cont
- 本文实例讲述了Android开发实现的标准体重计算器功能。分享给大家供大家参考,具体如下:运行结果界面: 界面设计<Rela
- 目录wait-notifyjoin方式ReentrantLockReentrantLock+ConditionSemaphore三个线程T1
- 一. 先来一段代码我们先上一段代码:String str1 = new StringBuilder("你好").appe
- 根据不同系统动态获取换行符和盘分割符1、获取盘分割符File.separator2、获取换行符windows系统为\r\n,Linux系统为