1. 此範例是利用SharedPreferences來儲存使用者在某個程式內的偏好設定。
2. MainActivity.java
01.
package
org.me.android_preference;
02.
import
android.app.Activity;
03.
import
android.content.SharedPreferences;
04.
import
android.os.Bundle;
05.
import
android.view.View;
06.
import
android.widget.Button;
07.
import
android.widget.EditText;
08.
09.
public
class
MainActivity
extends
Activity {
10.
private
EditText nameEditText;
11.
private
Button saveButton;
12.
private
Button restoreButton;
13.
14.
@Override
15.
public
void
onCreate(Bundle icicle) {
16.
super
.onCreate(icicle);
17.
setContentView(R.layout.main);
18.
nameEditText = (EditText) findViewById(R.id.nameEditText);
19.
saveButton = (Button) findViewById(R.id.saveButton);
20.
restoreButton = (Button) findViewById(R.id.restoreButton);
21.
saveButton.setOnClickListener(saveClickListener);
22.
restoreButton.setOnClickListener(restoreClickListener);
23.
}
24.
25.
//按下Save按鈕
26.
private
Button.OnClickListener saveClickListener =
new
Button.OnClickListener() {
27.
28.
public
void
onClick(View arg0) {
29.
//取得SharedPreference設定("Preference"為設定檔的名稱)
30.
SharedPreferences settings = getSharedPreferences(
"Preference"
,
0
);
31.
//置入name屬性的字串
32.
settings.edit().putString(
"name"
, nameEditText.getText().toString()).commit();
33.
}
34.
};
35.
36.
//按下Restore按鈕
37.
private
Button.OnClickListener restoreClickListener =
new
Button.OnClickListener() {
38.
39.
public
void
onClick(View arg0) {
40.
//取得SharedPreference設定("Preference"為設定檔的名稱)
41.
SharedPreferences settings = getSharedPreferences(
"Preference"
,
0
);
42.
//取出name屬性的字串
43.
String name = settings.getString(
"name"
,
""
);
44.
nameEditText.setText(name);
45.
}
46.
};
47.
}
3. main.xml(Layout)
01.
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
02.
<
AbsoluteLayout
03.
android:id
=
"@+id/widget0"
04.
android:layout_width
=
"fill_parent"
05.
android:layout_height
=
"fill_parent"
06.
xmlns:android
=
"http://schemas.android.com/apk/res/android"
07.
>
08.
<
TextView
09.
android:id
=
"@+id/nameView"
10.
android:layout_width
=
"wrap_content"
11.
android:layout_height
=
"wrap_content"
12.
android:text
=
"Name:"
13.
android:layout_x
=
"22px"
14.
android:layout_y
=
"34px"
15.
>
16.
</
TextView
>
17.
<
EditText
18.
android:id
=
"@+id/nameEditText"
19.
android:layout_width
=
"wrap_content"
20.
android:layout_height
=
"wrap_content"
21.
android:textSize
=
"18sp"
22.
android:layout_x
=
"90px"
23.
android:layout_y
=
"20px"
24.
>
25.
</
EditText
>
26.
<
Button
27.
android:id
=
"@+id/saveButton"
28.
android:layout_width
=
"wrap_content"
29.
android:layout_height
=
"wrap_content"
30.
android:text
=
"Save"
31.
android:layout_x
=
"20px"
32.
android:layout_y
=
"86px"
33.
>
34.
</
Button
>
35.
<
Button
36.
android:id
=
"@+id/restoreButton"
37.
android:layout_width
=
"wrap_content"
38.
android:layout_height
=
"wrap_content"
39.
android:text
=
"Restore"
40.
android:layout_x
=
"98px"
41.
android:layout_y
=
"84px"
42.
>
43.
</
Button
>
44.
</
AbsoluteLayout
>
4. 執行之後的畫面。(按下Store將Name的值儲存在偏好設定,按下Restore會將儲存在偏好設定的值取回)
全站熱搜