Android实现密码明密文切换(小眼睛)

作者:JH学编程 时间:2023-07-01 15:42:18 

本文实例为大家分享了Android实现密码明密文切换的具体代码,供大家参考,具体内容如下

小眼睛在密码栏右边!

Android实现密码明密文切换(小眼睛)

Android实现密码明密文切换(小眼睛)

奉上我使用的素材:

Android实现密码明密文切换(小眼睛)

Android实现密码明密文切换(小眼睛)

添加图片到res/darwable中

对安卓的知识掌握的非常浅,只知道 图片名称不要大写,大写会报错!
如果格式正确仍会报错的话,则 在gradle里加上这两句,俺也不懂为什么,都没有讲原理的。

aaptOptions.cruncherEnabled = false
aaptOptions.useNewCruncher = false

Android实现密码明密文切换(小眼睛)

Android实现密码明密文切换(小眼睛)

编辑登录页.xml

文本+可编辑文本框+小眼睛图片+按钮
小眼睛只要写一个ImageView即可

Android实现密码明密文切换(小眼睛)

<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

标签:Android,密码,密文
0
投稿

猜你喜欢

  • springMVC实现前台带进度条文件上传的示例代码

    2021-09-15 17:24:44
  • android自定义控件实现简易时间轴(2)

    2021-10-03 06:52:39
  • android studio 使用Mocklocation虚拟定位

    2022-12-31 12:26:34
  • c# Parallel类的使用

    2022-09-09 12:30:21
  • Android仿微信语音聊天功能

    2022-11-24 03:36:12
  • SpringBootTest单元测试报错的解决方案

    2021-09-08 23:25:47
  • Java多线程之线程同步

    2023-01-24 18:39:00
  • Android开发:微信授权登录与微信分享完全解析

    2023-03-20 14:08:10
  • Android自定义组件获取本地图片和相机拍照图片

    2022-07-09 21:54:48
  • C#如何实现调取钉钉考勤接口的功能

    2023-09-14 17:31:47
  • CentOS 7系统下配置自定义JDK的教程

    2022-02-27 13:46:26
  • idea中使用(Revert Commit)图解

    2023-03-22 18:45:58
  • Java排序之冒泡排序的实现与优化

    2023-11-10 21:35:56
  • 基于idea Maven中的redis配置使用详解

    2023-11-29 11:57:28
  • Spring Data JPA中 in 条件参数的传递方式

    2023-11-08 06:45:47
  • Spring整合Quartz实现定时任务调度的方法

    2023-07-07 00:55:55
  • Java算法比赛常用方法实例总结

    2023-11-28 07:15:26
  • 解决Maven多模块编译慢的问题

    2022-10-09 23:58:02
  • java实现ArrayList根据存储对象排序功能示例

    2022-01-24 01:06:05
  • C# 使用HttpClient模拟请求的案例

    2023-10-16 18:20:14
  • asp之家 软件编程 m.aspxhome.com