登录页面要求
(1)模仿以下登录程序界面,以SharedPreferences方式存储用户账号、密码信息,在下次打开软件时,可以在界面中显示上次登录的账号、密码;
(2)对以上程序界面,增加记住密码选项,实现以下功能
a. 用户登录验证(登录账号、密码可在源码中固定)功能;
b. 记住密码选项被选定且登录成功,则存储用户账号、密码等信息;
c. 在下次打开软件时,可以在界面中显示上次登录成功的账号、密码
要点
- 如何理清登录页面逻辑
- 如何使用SharedPreferences存储数据和checkbox的值
Main
class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText et_act;
private EditText et_pwd;
private CheckBox cb_one;
private Button btn_lg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
Map<String ,String> userInfo = SPSaveQQ.getUserInfo(this);
if(userInfo!=null) {
et_act.setText(userInfo.get("act"));
et_pwd.setText(userInfo.get("pwd"));
cb_one.setChecked(Boolean.valueOf(userInfo.get("checked")));
//修改为通过SPSaveQQ保存信息
}
}
private void initView(){
et_act = findViewById(R.id.et_act);
et_pwd = findViewById(R.id.et_pwd);
cb_one = findViewById(R.id.cb_one);
btn_lg = findViewById(R.id.but_lg);
btn_lg.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.but_lg:
String act = et_act.getText().toString().trim();
String pwd = et_pwd.getText().toString().trim();
//重点
boolean checked= cb_one.isChecked();
//检验账号密码是否为空
if(TextUtils.isEmpty(act)){
Toast.makeText(this,"请输入账号",Toast.LENGTH_SHORT).show();
return;
}
if(TextUtils.isEmpty(pwd)){
Toast.makeText(this,"请输入密码",Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show();
//改为通过SPSaveQQ保存信息
boolean isSaveSucces =SPSaveQQ.saveUserInfo(this,act, pwd,checked);
if(isSaveSucces){
Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(this,"保存失败",Toast.LENGTH_SHORT).show();
}
break;
}
spssave
public class SPSaveQQ {
public static boolean saveUserInfo(Context context, String act, String pwd,boolean checked) {
SharedPreferences sp = context.getSharedPreferences("data",Context.MODE_PRIVATE);
SharedPreferences.Editor edit = sp.edit();
edit.putString("userName",act);
// edit.putString("password",pwd);
if (checked){
edit.putString("password",pwd);
edit.putBoolean("checked",true);
}
else {
edit.putBoolean("checked",false);
edit.remove("password");
edit.clear() ;
}
edit.commit();
return true;
}
public static Map<String ,String> getUserInfo(Context context){
SharedPreferences sp = context.getSharedPreferences("data",Context.MODE_PRIVATE);
String act = sp.getString("userName",null);
String pwd = sp.getString("password",null);
//重点
boolean checked = sp.getBoolean("checked",false);
Map<String,String> userMap = new HashMap<String,String>();
userMap.put("act",act);
userMap.put("pwd",pwd);
//重点
userMap.put("checked",String.valueOf(checked));
return userMap;
}
}
```bash
xml
<?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"
android:background="#E6E6E6"
tools:context=".MainActivity">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:src="@drawable/iu"
android:layout_gravity="center"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@color/white"
android:padding="15dp">
<TextView
style="@style/tv_two"
android:text="账号:" />
<EditText
android:id="@+id/et_act"
style="@style/etOne"
/>
</LinearLayout>
<!-- 分界线-->
<TextView
android:layout_width="match_parent"
android:layout_height="2dp"></TextView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="horizontal"
android:padding="15dp">
<TextView
style="@style/tv_two"
android:text="密码:">
</TextView>
<EditText
android:id="@+id/et_pwd"
style="@style/etOne"
android:inputType="textPassword" />
</LinearLayout>
<CheckBox
android:id="@+id/cb_one"
android:layout_width="100dp"
android:layout_height="30dp"
android:text="记住密码"
/>
<Button
android:id="@+id/but_lg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"
/>
## 结果截图