一、目标 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 >
九、效果图
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步