1022# 2023-10-28 17:20 采纳率: 50%
浏览 16

AndroidStudio登录界面隐藏密码的问题

#登录界面隐藏密码问题,点击一次眼睛图标后再次点击就没反应了图标也换不了密码也不能再次隐藏了

public class LoginActivity extends AppCompatActivity {
    //声明控件
    //登陆界面的控件
    EditText user_name;
    EditText user_password;
    ImageView ivEyeShow,ivEyeHide;
    boolean showPW;//用于判断是否显示密码
    HideReturnsTransformationMethod method_show;

    Button denglu;
    Button zhuce;

    //声明数据库
    Mysql mysql;
    SQLiteDatabase db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //找到当前xml文件内的控件ID
        //数据编辑框的ID
        user_name = this.findViewById(R.id.user_name);
        user_password = this.findViewById(R.id.user_password);

        //隐藏密码控件
        ivEyeShow=findViewById(R.id.ivEyeShow);
        ivEyeHide=findViewById(R.id.ivEyeHide);
        //按键属性的ID
        denglu = this.findViewById(R.id.denglu);
        zhuce = this.findViewById(R.id.zhuce);

        //监听隐藏密码控件
        if(showPW){

            ivEyeShow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {//按下时show图片变成hide图片
                    ivEyeHide.setVisibility(View.VISIBLE);
                    ivEyeShow.setVisibility(View.GONE);
                    //隐藏密码
                    showPW=false;
                    PasswordTransformationMethod method_hide=PasswordTransformationMethod.getInstance();
                    user_password.setTransformationMethod(method_hide);
                }
            });

        }
        else{
            ivEyeHide.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ivEyeHide.setVisibility(View.GONE);
                    ivEyeShow.setVisibility(View.VISIBLE);
                    //显示密码
                    showPW=true;
                    method_show=HideReturnsTransformationMethod.getInstance();
                    user_password.setTransformationMethod(method_show);
                }
            });
        }
 //取出数据库内的数据
        mysql = new Mysql(this, "Userinfo", null, 1);
        db = mysql.getReadableDatabase();
        //登录按键按下之后处理的事情
        denglu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //需要获取的输入的用户名和密码
                String storage_username = user_name.getText().toString();//用户控件.得到数据.转换为字符串;
                String storage_userpassword = user_password.getText().toString();//用户控件.得到数据.转换为字符串;

                //查询用户名和密码相同的数据
                Cursor cursor = db.query("logins", new String[]{"usname", "uspwd"}, " usname=? and uspwd=?",
                        new String[]{storage_username, storage_userpassword}, null, null, null);
                int flag = cursor.getCount(); //查询出来的记录项的条数,若没有该用户则为0条
                //登录成功后响应的数据
                if (flag != 0) {
                    Toast.makeText(getApplicationContext(), "登录成功!", Toast.LENGTH_SHORT).show();//显示登录成功的弹窗,简单写法
                    Intent intent = null;  //这个变量初始申明为空
                    intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                } else {
                    //假设正确的账号和密码分别是(VIP账号,没有写入数据库,无需注册账号)
                    if (storage_username.equals("lq") && storage_userpassword.equals("123456")) {
                        //如果正确
                        Toast.makeText(getApplicationContext(), "超级VIP登录成功!", Toast.LENGTH_SHORT).show();//显示登录成功的弹窗,简单写法
                        Intent intent = null;  //这个变量初始申明为空
                        intent = new Intent(LoginActivity.this, MainActivity.class);
                        startActivity(intent);
                    } else {
                        Toast.makeText(getApplicationContext(), "用户名输入错误或密码不正确,请重新登录!", Toast.LENGTH_SHORT).show();//获取显示的内容
                    }
                }

            }
        });
        //注册按键按下之后,响应的事件
        zhuce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //实现界面跳转,从登录界面跳转到注册界面
                Intent intent = null;  //这个变量初始申明为空
                intent = new Intent(LoginActivity.this, RegisterActivity.class);//跳转界面
                startActivity(intent);
            }
        });}}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"

    tools:context=".LoginActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginTop="180dp">
        <ImageView
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:background="@mipmap/newlogo"
          />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/appName"
            android:textColor="@color/colorBlack"
            android:textSize="30dp"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginRight="10dp"
        android:background="@drawable/user_background">

        <ImageView
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="2dp"
            android:background="@mipmap/login4" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp"
            android:text="用户名:"
            android:textSize="25sp" />

        <EditText
            android:id="@+id/user_name"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_marginRight="30dp"
            android:maxLines="1"
            android:hint="请输入账号"
            android:textColor="@color/colorBlack" />
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginRight="10dp"
        android:background="@drawable/user_background">

        <ImageView
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="2dp"
            android:background="@mipmap/password1" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp"
            android:text="密 码:"
            android:textSize="25sp" />

        <EditText
            android:id="@+id/user_password"
            android:layout_width="200dp"
            android:layout_height="60dp"
            android:layout_marginLeft="10dp"
            android:hint="请输入密码"
            android:inputType="textPassword"
            android:maxLength="16"
            android:maxLines="1"
            android:textColor="@color/colorBlack" />

        <ImageView
            android:id="@+id/ivEyeShow"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="13dp"
            android:src="@mipmap/show"
            android:visibility="gone" />

        <ImageView
            android:id="@+id/ivEyeHide"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_gravity="center"
            android:layout_marginLeft="1dp"
            android:src="@mipmap/hide"
            android:visibility="visible" />




    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:orientation="horizontal">
        <Button
            android:id="@+id/denglu"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_marginLeft="25dp"
            android:layout_height="wrap_content"
            android:text="登录"
            android:textSize="25sp"
            android:layout_gravity="center"
          />
        <Button
            android:id="@+id/zhuce"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_marginLeft="25dp"
            android:layout_height="wrap_content"
            android:text="注册"
            android:textSize="25sp"
            android:layout_gravity="center"
            />


    </LinearLayout>



</LinearLayout>

  • 写回答

3条回答 默认 最新

  • 叶灼hua 2023-10-29 08:44
    关注

    你可以加一个if判断,比如以奇偶数来判断,默认闭眼,然后你点击第1次,闭眼;第2次,睁眼;第3次,闭眼......这样,奇数次是闭眼,偶数次是睁眼,该怎么写你懂了吧

    评论

报告相同问题?

问题事件

  • 创建了问题 10月28日

悬赏问题

  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 angular开发过程中,想要读取模型文件,即图1的335行,会报404错误(如图2)。但我的springboot里配置了静态资源文件,如图3。且在该地址下我有模型文件如图4,请问该问题该如何解决呢?
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常
  • ¥15 Java,消息推送配置