#登录界面隐藏密码问题,点击一次眼睛图标后再次点击就没反应了图标也换不了密码也不能再次隐藏了
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>