一、目标
JumpToPreferenceActivity.java跳转到MyPreferenceActivity.java.
MyPreferenceActivity.java是应用设置Activity.
因为要和另一个项目关联,所以用了Intent,这里Intent纯粹鸡肋,关键是PreferenceActivity的使用。
二、工程文件结构
JumpToPreferenceActivity.java引用main.xml;
MyPreferenceActivity.java引用setting.xml;
setting.xml中的ListPreference引用/res/values/array.xml中的string-array
三、JumpToPreferenceActivity.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
|
| package com.lucux.prefrence; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class JumpToPreferenceActivity extends Activity{ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button jumpToPrefrenceBtn = (Button)findViewById(R.id.jumpToPrefrenceBtn); jumpToPrefrenceBtn.setOnClickListener(new JumpToPrefOnClickListener()); } public class JumpToPrefOnClickListener implements OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setClass( JumpToPreferenceActivity.this,MyPreferenceActivity.class ); JumpToPreferenceActivity.this.startActivity( intent ); } } }
|
四、MyPreferenceActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13
|
| package com.lucux.prefrence; import android.os.Bundle; import android.preference.PreferenceActivity; public class MyPreferenceActivity extends PreferenceActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.setting); } }
|
五、AndroidManifest.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
|
| <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.lucux.prefrence" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".JumpToPreferenceActivity" 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=".MyPreferenceActivity"> </activity> </application> </manifest>
|
六、mail.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/jumpToPrefrenceBtn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="跳转到设置"/> </LinearLayout>
|
七、/res/xml/setting.xml
ListPreference中的属性
android:entries="@array/entries_repeat" 显示给用户的内容
android:entryValues="@array/entriesvalue_repeat" 实际传给java代码的值
在/res/values/array.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 46 47 48
|
| <?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceCategory android:title="提醒设置"> <CheckBoxPreference android:key="setVibrate" android:title="振动" android:summaryOn="开启" android:summaryOff="关闭" android:defaultValue="true" /> <CheckBoxPreference android:key="setRingtone" android:title="铃声" android:summaryOn="开启" android:summaryOff="关闭" android:defaultValue="true" /> <RingtonePreference android:key="chooseRingtone" android:title="选择铃声" android:summary="还书提醒铃声" /> </PreferenceCategory> <PreferenceCategory android:title="再响设置"> <ListPreference android:key="setRepeat" android:title="再响" android:summary="选择几天后再次提醒" android:dialogTitle="选择几天后再次提醒" android:entries="@array/entries_repeat" android:entryValues="@array/entriesvalue_repeat" /> </PreferenceCategory> <PreferenceCategory android:title="期限设置"> <ListPreference android:key="setDeadline" android:title="还书期限" android:summary="系统会在期限时间后提醒您还书" android:dialogTitle="选择还书期限" android:entries="@array/entries_deadline" android:entryValues="@array/entriesvalue_deadline" /> </PreferenceCategory> </PreferenceScreen>
|
八、array.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 46 47 48 49 50 51 52 53
|
| <?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="entries_repeat"> <item>1天</item> <item>2天</item> <item>3天</item> <item>4天</item> <item>5天</item> <item>6天</item> <item>7天</item> <item>8天</item> <item>9天</item> <item>10天</item> </string-array> <string-array name="entriesvalue_repeat"> <item>1</item> <item>2</item> <item>3</item> <item>4</item> <item>5</item> <item>6</item> <item>7</item> <item>8</item> <item>9</item> <item>10</item> </string-array> <string-array name="entries_deadline"> <item>10天</item> <item>20天</item> <item>30天</item> <item>40天</item> <item>50天</item> <item>60天</item> <item>70天</item> <item>80天</item> <item>90天</item> <item>100天</item> </string-array> <string-array name="entriesvalue_deadline"> <item>10</item> <item>20</item> <item>30</item> <item>40</item> <item>50</item> <item>60</item> <item>70</item> <item>80</item> <item>90</item> <item>100</item> </string-array> </resources>
|
九、效果图