demo是这个样子,下次进入的时候会保存上次的信息
layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="please input your name"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_pw"
android:hint="please input your paseword"
android:layout_marginTop="10dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cb_rm"
android:text="rememberMe"
android:layout_centerVertical="true"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="login"
android:layout_alignParentRight="true"
android:onClick="login"
/>
</RelativeLayout>
</LinearLayout>
保存文件的名字 config.xml
SharedPreferences sp = getSharedPreferences("config", Context.MODE_PRIVATE);
找key(标签)的时候没有 返回defvalue 空字符串
String userName = sp.getString("userName", "");
获得读写操作
SharedPreferences.Editor editor = sp.edit();
Mainactivity
public class MainActivity extends AppCompatActivity {
private EditText et_username;
private EditText et_pw;
private CheckBox cb_rm;
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
sp = getSharedPreferences("config", Context.MODE_PRIVATE);
String userName = sp.getString("userName", "");
String pw = sp.getString("pw", "");
boolean isChecked = sp.getBoolean("isChecked", false);
showinfo(userName,pw,isChecked);
}
private void showinfo(String userName, String pw, boolean isChecked) {
this.et_username.setText(userName);
this.et_pw.setText(pw);
this.cb_rm.setChecked(isChecked);
}
private void initView() {
et_username = (EditText) findViewById(R.id.et_username);
et_pw = (EditText) findViewById(R.id.et_pw);
cb_rm = (CheckBox) findViewById(R.id.cb_rm);
}
public void login(View v){
String userName = et_username.getText().toString().trim();
String pw = et_pw.getText().toString().trim();
if (userName.isEmpty() || TextUtils.isEmpty(pw))/*判断空不空,两种方法第一个是string方法 第二个是textutils方法(静态)*/
{
//跳出窗口//
MyToast.show(this,"username or pw cannot be empty");
return;
}
boolean isChecked = cb_rm.isChecked();
if ( ! cb_rm.isChecked())/*如果这个变量(方法)是is开头的 那么这个变量(方法)的返回值一定是布尔类型*/
{
MyToast.show(this,"please remember yourself");
return;
}
boolean isSuccess = saveWithsp(userName, pw,isChecked);
if (isSuccess)
MyToast.show(this,"SUCCESS");
else
MyToast.show(this,"FALLED");
}
private boolean saveWithsp(String userName,String pw,boolean isChecked) { /*把数据保存*/
SharedPreferences.Editor editor = sp.edit();
editor.putString("userName", userName);
editor.putString("pw", pw);
editor.putBoolean("isChecked", isChecked);
boolean isSuccess = editor.commit();
return isSuccess;
}
}