用SharePreference實現簡單的保存賬號密碼
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
editText1 = (EditText) findViewById(R.id.editText1);
editText2 = (EditText) findViewById(R.id.editText2);
checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
// 定義一個啥熱捧reference存儲的文件名login
preference = this.getSharedPreferences("login", MODE_PRIVATE);
// 創建edit向sharepreference裏面存數據
editor = preference.edit();
// 判斷存儲的內容不爲null並且不爲空,這樣子就可以拿數據了,實現保存功能
if (preference != null && !preference.getAll().isEmpty()) {
editText1.setText(preference.getString("usename", ""));
editText2.setText(preference.getString("password", ""));
checkBox1.setChecked(preference.getBoolean("savestate", false));
checkBox2.setChecked(preference.getBoolean("saveusename", false));
}
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 存儲數據
if (editText1.getText().toString().trim().equals("admin")) {
if (checkBox1.isChecked()) {
editor.putString("usename", editText1.getText()
.toString().trim());
} else {
editor.putString("usename", "");
}
if (checkBox2.isChecked()) {
editor.putString("password", editText2.getText()
.toString().trim());
} else {
editor.putString("password", "");
}
editor.putBoolean("savestate", checkBox1.isChecked());
editor.putBoolean("saveusename", checkBox2.isChecked());
editor.commit();
}
}
});
}
}
佈局:<RelativeLayout 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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="36dp"
android:text="賬號" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_marginLeft="32dp"
android:layout_toRightOf="@+id/textView1"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_marginTop="33dp"
android:layout_toLeftOf="@+id/editText1"
android:text="密碼" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/editText1"
android:ems="10"
android:inputType="textPassword" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/editText2"
android:layout_marginTop="54dp"
android:text="保存用戶" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/checkBox1"
android:layout_alignBottom="@+id/checkBox1"
android:layout_marginLeft="42dp"
android:layout_toRightOf="@+id/checkBox1"
android:text="保存密碼" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/checkBox1"
android:layout_marginTop="39dp"
android:layout_toLeftOf="@+id/editText2"
android:text="登陸" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button1"
android:layout_alignLeft="@+id/checkBox2"
android:text="取消" />
</RelativeLayout>
運行後: