登陆页面记住密码

来源:互联网 发布:路由器主人网络模式 编辑:程序博客网 时间:2024/05/04 17:54

SharedPreferences是一个轻量级的存储类,采用xml文件存放数据。它是通过其Editor接口中的一些方法来操作SharedPreference的。

做登陆页面记住密码首先要先做好页面布局,我采用了两个页面,一个是登陆页面,还有一个是主页面。

这个页面中,主要有两个textview控件来做用户名和密码的输入框,一个Button控件是登陆按钮,一个checkbox控件做记住密码的复选框。

部分代码如下:

<TextView
        android:id="@+id/tvUsername"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@string/tvName"
        android:textAppearance="?android:attr/textAppearanceMedium" />
    <EditText
        android:id="@+id/etUsername"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tvUsername"
        android:layout_below="@+id/tvUsername"
        android:background="@android:drawable/edit_text"
        android:ems="10" />
    <TextView
        android:id="@+id/tvPassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/etUsername"
        android:layout_below="@+id/etUsername"
        android:text="@string/tvPassword"
        android:textAppearance="?android:attr/textAppearanceMedium" />
    <EditText
        android:id="@+id/etPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tvPassword"
        android:layout_below="@+id/tvPassword"
        android:layout_marginTop="16dp"
        android:background="@android:drawable/edit_text"
        android:ems="10"
        android:inputType="textPassword" />
    <Button
        android:id="@+id/btnLogin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/etPassword"
        android:layout_below="@+id/etPassword"
        android:layout_marginTop="20dp"
        android:background="#FF72CAE1"
        android:onClick="save"
        android:text="@string/btnLogin" />
    <CheckBox
        android:id="@+id/cbSetPass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/btnLogin"
        android:layout_alignBottom="@+id/btnLogin"
        android:layout_alignLeft="@+id/etPassword"
        android:text="@string/cb_setPass" />

页面布局做好之后就是在LoginActivity类中写保存用户名和密码的代码。

在保存前要先创建文件:

//创建文件
sharedPreferences=getSharedPreferences("user", MODE_PRIVATE);

保存用户名和密码的方法:

public void save(View view){
// 获取文本框中的用户名和密码
String userName=etName.getText().toString();
String userPass=etPass.getText().toString();
if(!(TextUtils.isEmpty(userName))&& !(TextUtils.isEmpty(userPass))){  //如果用户名和密码不为空
if("admin".equals(userName)&& "admin".equals(userPass)){  //用户民和密码都设定为admin
Editor edit=sharedPreferences.edit();
if(cbSetPass.isChecked()){//如果保存密码的复选框被选中
edit.putString("userName", userName);
edit.putString("userPass", userPass);
edit.putBoolean("cbIscheck", true);
}
else{ //保存密码的复选框没有被选中,清除编辑
edit.clear();
}
edit.commit();//提交编辑
Intent intent=new Intent(LoginActivity.this,MainActivity.class);//跳转到主页面
startActivity(intent);
finish();
}else{
//如果密码或用户名错误给出提示
Toast.makeText(this,"账号或密码错误" , Toast.LENGTH_LONG).show();
}
}
}

我们还需要在程序启动时就验证文件中是否已经存在用户名和密码了,于是我创建了getIscheck方法,首先在文件中查询保存密码复选框是否被选中,如果被选中就向用户名和密码赋值。

public void getIscheck(){
//获取保存密码复选框是否被选中
cbIscheck=sharedPreferences.getBoolean("cbIscheck",false);
// 如果保存密码复选框被选中
if(cbIscheck==true){
//向文本框和复选框中赋值
etName.setText(sharedPreferences.getString("userName" ,""));
etPass.setText(sharedPreferences.getString("userPass" ,""));
cbSetPass.setChecked(true);
}
}

0 0
原创粉丝点击