Example of using CheckBoxPreference
- Create a xml file, to define the CheckBoxPreference, /res/xml/checkboxpref.xml.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
| <? xml version = "1.0" encoding = "utf-8" ?> android:key = "checkbox_pref" android:title = "Check Box Preferences" > < CheckBoxPreference android:key = "pref_opt1" android:title = "Opt 1" android:summary = "option 1" android:defaultValue = "true" /> < CheckBoxPreference android:key = "pref_opt2" android:title = "Opt 2" android:summary = "option 2" /> < CheckBoxPreference android:key = "pref_opt3" android:title = "Opt 3" android:summary = "option 3" /> < CheckBoxPreference android:key = "pref_opt4" android:title = "Opt 4" android:summary = "option 4" /> </ PreferenceScreen > |
- Create a SetPreference.java extends PreferenceActivity, it simple call the function addPreferencesFromResource(R.xml.checkboxpref) in onCreate().
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
| package com.AndroidCheckBoxPreference; import android.os.Bundle; import android.preference.PreferenceActivity; public class SetPreference extends PreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super .onCreate(savedInstanceState); addPreferencesFromResource(R.xml.checkboxpref); } } |
- Modify the layout main.xml to have a TextView to show the preferences, and a Button to start SetPreference.java.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
| <? xml version = "1.0" encoding = "utf-8" ?> android:orientation = "vertical" android:layout_width = "fill_parent" android:layout_height = "fill_parent" > < TextView android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text = "@string/hello" /> < TextView android:id = "@+id/pref" android:layout_width = "fill_parent" android:layout_height = "wrap_content" /> < Button android:id = "@+id/setpref" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text = "Set Preferences" /> </ LinearLayout > |
- Main code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
| package com.AndroidCheckBoxPreference; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; import android.view.View; import android.widget.Button; import android.widget.TextView; public class AndroidCheckBoxPreferenceActivity extends Activity { TextView pref; Button setPref; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); pref = (TextView)findViewById(R.id.pref); setPref = (Button)findViewById(R.id.setpref); setPref.setOnClickListener( new Button.OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent( AndroidCheckBoxPreferenceActivity. this , SetPreference. class ); startActivityForResult(intent, 0 ); }}); checkPref(); } private void checkPref(){ SharedPreferences myPref = PreferenceManager.getDefaultSharedPreferences( AndroidCheckBoxPreferenceActivity. this ); String _pref = "Option 1: " + myPref.getBoolean( "pref_opt1" , true ) + "\n" + "Option 2: " + myPref.getBoolean( "pref_opt2" , false ) + "\n" + "Option 3: " + myPref.getBoolean( "pref_opt3" , false ) + "\n" + "Option 4: " + myPref.getBoolean( "pref_opt4" , false ); pref.setText(_pref); } @Override protected void onActivityResult( int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super .onActivityResult(requestCode, resultCode, data); checkPref(); } } |
- Modify AndroidManifest.xml to add activity of SetPreference.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
| <? xml version = "1.0" encoding = "utf-8" ?> package = "com.AndroidCheckBoxPreference" android:versionCode = "1" android:versionName = "1.0" > < uses-sdk android:minSdkVersion = "4" /> < application android:icon = "@drawable/icon" android:label = "@string/app_name" > < activity android:name = ".AndroidCheckBoxPreferenceActivity" android:label = "@string/app_name" > < intent-filter > < action android:name = "android.intent.action.MAIN" /> < category android:name = "android.intent.category.LAUNCHER" /> </ intent-filter > </ activity > < activity android:name = ".SetPreference" /> </ application > </ manifest >
akm www.cdacians.com |
No comments:
Post a Comment