android的Setting往往用PreferenceActivity来写的
我们在建立layout文件:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceCategory android:title="常规设置" android:key="set_local"> <CheckBoxPreference android:key="new_message_notify" android:title="新消息通知" android:defaultValue="true" android:summary="收到新消息时是否需要通知" /> <RingtonePreference android:layout="?android:attr/preferenceLayoutChild" android:dependency="new_message_notify" android:key="account_ringtone" android:title="铃声" android:ringtoneType="notification" android:defaultValue="content://settings/system/notification_sound" /> <CheckBoxPreference android:layout="?android:attr/preferenceLayoutChild" android:dependency="new_message_notify" android:key="account_vibrate" android:defaultValue="false" android:title="振动"/> </PreferenceCategory> <PreferenceCategory android:title="个人设置" android:key="personal_local"> <Preference android:key="clear_cache" android:summary="点击将清理应用程序的缓存" android:title="清除缓存" defaultValue="false"> </Preference> <CheckBoxPreference android:key="save_setting" android:summary="下次开启记住个人设置" android:title="保存个人设置" android:defaultValue="true"> </CheckBoxPreference> <EditTextPreference android:key="edit_text" android:title="Edit" android:summary="EditTextPreference" ></EditTextPreference> <SwitchPreference android:key="switch" android:summary="SwitchPreference" ></SwitchPreference> <ListPreference android:key="list" android:summary="ListPreference" android:entries="@array/entry" android:entryValues="@array/entry_value" android:title="ListTitle" ></ListPreference> <MultiSelectListPreference android:summary="MultiSelectListPreference" android:key="mutiSelect" android:entries="@array/entry" android:entryValues="@array/entry_value" android:title="mutiTitle" /> </PreferenceCategory> </PreferenceScreen>
类的代码如下:
public class Settings extends PreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState) { //这个是给Settings加自定义Title final boolean isCustom = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); super.onCreate(savedInstanceState); if(isCustom){ getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_list); } TextView title_text = (TextView)findViewById(R.id.title_text); title_text.setText("Settings"); Button back = (Button)findViewById(R.id.back); back.setVisibility(View.VISIBLE); back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); addPreferencesFromResource(R.xml.seting_preferences); } }
定义themes,把这个activity的theme设置成以下的样子
<style name="AppTheme" parent="AppBaseTheme"> <!-- All customizations that are NOT specific to a particular API-level can go here. --> <!-- 避免重复设置标题 --> <item name="android:windowActionBar">false</item> <!-- 设置标题栏宽度 --> <item name="android:windowTitleSize">60dp</item> //titlebar的高度 <!-- <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item> --> </style>
取值的时候可以这样做:
SharedPreferences shp = PreferenceManager.getDefaultSharedPreferences(this); String s = shp.getString("list", null); //这个是取ListPreference TextView listData = (TextView)findViewById(R.id.listData); listData.setText(s); HashSet set = (HashSet) shp.getStringSet("mutiSelect", null); //这个是取MultiSelectListPreference的值 Iterator<String> it = set.iterator(); String content = ""; while(it.hasNext()) { content += it.next()+","; } TextView muti_select_data = (TextView)findViewById(R.id.muti_select_data); muti_select_data.setText(content);
截图是:
关于PreferenceActivity的使用和一些问题的解决(自定义Title和取值),布布扣,bubuko.com
时间: 2024-10-26 16:42:59