MainActivity:
package com.example.unit01b_shared_demo03;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
public class MainActivity extends Activity {
private EditText phoneEdit, pwdEdit;
private CheckBox checkBox;
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取SharedPreferences对象
sp = getSharedPreferences("user", MODE_PRIVATE);
// 找控件
phoneEdit = (EditText) findViewById(R.id.phone_edit);
pwdEdit = (EditText) findViewById(R.id.pwd_edit);
checkBox = (CheckBox) findViewById(R.id.mycheck);
// checkbox监听事件;
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {// 勾选记住密码,保存用户名密码
if (getEditText(phoneEdit) != null
&& getEditText(pwdEdit) != null) {
// 两个输入框都有值;
sp.edit().putString("phone", getEditText(phoneEdit))
.commit();
sp.edit().putString("pwd", getEditText(pwdEdit))
.commit();
}
}
}
});
getData();
}
// 获取edittext的值;
private String getEditText(EditText edit) {
String text = edit.getText().toString().trim();
if (isNotNull(text)) {
return text;
}
return null;
}
// 首先进入此界面,从SharedPreferences里面获取用户名和密码,如果有值,显示到界面上;
private void getData() {
String phone = sp.getString("phone", "");
String pwd = sp.getString("pwd", "");
if (isNotNull(phone) && isNotNull(pwd)) {// 不为空,显示到edittext;
phoneEdit.setText(phone);
pwdEdit.setText(pwd);
checkBox.setChecked(true);
}
}
// !!!!!!!!!
private boolean isNotNull(String text) {
if (text != null && !text.equals("")) {// 不为空,并且不是空字符串;
return true;
}
return false;
}
}
xml布局
<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:gravity="center_horizontal"
tools:context="com.example.unit01b_shared_demo03.MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
android:gravity="center_vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="手机号" />
<EditText
android:id="@+id/phone_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入您的手机号"
android:background="@drawable/edit_outlin"
android:padding="5dp"
android:layout_margin="5dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
android:gravity="center_vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:text="密码" />
<EditText
android:id="@+id/pwd_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入您的密码"
android:background="@drawable/edit_outlin"
android:padding="5dp"
android:layout_margin="5dp"
/>
</LinearLayout>
<CheckBox
android:id="@+id/mycheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码"
/>
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
android:layout_margin="10dp"
/>
</LinearLayout>