Android 眼睛 显示隐藏密码(ImageView)-LMLPHP

package com.example.test2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private EditText password;
    private ImageView imageView;
    private boolean flag=true;
    private HideReturnsTransformationMethod method_show;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        password = findViewById(R.id.password);
        imageView = findViewById(R.id.img);
        imageView.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.img:
                changeImage();

        }
    }

    private void changeImage() {
        // todo 每次进入登陆时输入的密码都是隐藏的
        PasswordTransformationMethod methodHide=PasswordTransformationMethod.getInstance();
        password.setTransformationMethod(methodHide);

        if(flag){
            imageView.setImageDrawable(this.getResources().getDrawable(R.drawable.eye1));
            flag=false;
            // todo 隐藏密码
            PasswordTransformationMethod method_hide = PasswordTransformationMethod.getInstance();
            password.setTransformationMethod(method_hide);
        }
        else{
            imageView.setImageDrawable(this.getResources().getDrawable(R.drawable.eye2));
            flag=true;
            // todo  显示密码
            method_show= HideReturnsTransformationMethod.getInstance();
            password.setTransformationMethod(method_show);

        }

        if (!password.getText().toString().trim().equals("")) {
            password.setSelection(password.getText().toString().trim().length());//将光标移至文字末尾
        }

    }
}

布局代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/white"
        android:orientation="horizontal" >
        <EditText
            android:id="@+id/password"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="10dp"
            android:inputType="textPassword"
            android:hint="请设置登录密码"
            android:background="@null"/>

        <button
            android:id="@+id/btn"
            android:layout_width="89dp"
            android:layout_height="match_parent"
            android:background="@drawable/eye2"/>


    </LinearLayout>


</LinearLayout>
<manifest ... >
  <application ... >
      <activity android:name=".ExampleActivity" />
      ...
  </application ... >
  ...
</manifest >
<activity android:name=".ExampleActivity" android:icon="@drawable/app_icon">
    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/plain" />
    </intent-filter>
</activity>

管理 Activity 生命周期 一个 Activity 在其生命周期中会经历多种状态。您可以使用一系列回调来处理状态之间的转换。下面几节将介绍这些回调。

onCreate()

onStart()

onResume()

onPause()

onStop()

onRestart()

onDestroy()







觉得有用的可以给个三连,关注一波!!!带你了解更多的Android小项目

06-22 11:23