checkbox勾选 成功登陆则下次启动时会将之前的用户名保存下来,登陆失败和未勾选则不会保存用户名
MAinActivity.class
package com.superxingyun.sharedpreferences;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText ed_username;
EditText ed_password;
CheckBox chk_checkBox;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chk_checkBox = (CheckBox) findViewById(R.id.checkBox);
ed_username = (EditText) findViewById(R.id.ed_username);
ed_password = (EditText) findViewById(R.id.ed_userpassword);
sharedPreferences = getSharedPreferences("UserInfo", MODE_PRIVATE);
editor = sharedPreferences.edit();
String name = sharedPreferences.getString("Username", "");
if (name == null){
chk_checkBox.setChecked(false);
}
else {
chk_checkBox.setChecked(true);
ed_username.setText(name);
}
}
public void doClick(View view) {
switch (view.getId()){
case R.id.btn_login:
String name = ed_username.getText().toString().trim();
String pass = ed_password.getText().toString().trim();
if ("admin".equals(name) && "123456".equals(pass)){
if (chk_checkBox.isChecked()){
editor.putString("Username", name);
editor.commit();
}
else{
editor.remove("Username");
editor.commit();
}
Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(MainActivity.this, "登陆失败", Toast.LENGTH_SHORT).show();
}
break;
default:
break;
}
}
}
昨天晚上弄完之后发现用户名和密码都匹配的情况下无论checkbox是否勾选再起启动时用户名都无法保存,后来才发现是String name = sharedPreferences.getString(“Username”, “”);这句话中传入的参数username的开头没有大写导致无论怎么取都是空。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.superxingyun.sharedpreferences.MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/txt_logo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/txt_logo"
android:textSize="35sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:orientation="horizontal">
<TextView
android:id="@+id/txt_username"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/txt_username" />
<EditText
android:id="@+id/ed_username"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:orientation="horizontal">
<TextView
android:id="@+id/txt_userpassword"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/txt_password" />
<EditText
android:id="@+id/ed_userpassword"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:singleLine="true" />
</LinearLayout>
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:text="@string/chk_remusername" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn_login"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="doClick"
android:text="@string/btn_login" />
<Button
android:id="@+id/btn_cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="doClick"
android:text="@string/txt_cancel" />
</LinearLayout>
</LinearLayout>