关于PreferenceActivity的使用和一些问题的解决(自己定义Title和取值)

关于PreferenceActivity的使用和一些问题的解决(自己定义Title和取值)

大家好,又见面了,我是全栈君,祝每个程序员都可以多学几门语言。

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和取值)

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/118967.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • MIPI协议(下)

    MIPI协议(下)GOON…完成mipi信号通道分配后,需要生成与物理层对接的时序、同步信号: MIPI规定,传输过程中,包内是200mV、包间以及包启动和包结束时是1.2V,两种不同的电压摆幅,需要两组不同的LVDS驱动电路在轮流切换工作;为了传输过程中各数据包之间的安全可靠过渡,从启动到数据开始传输,MIPI定义了比较长的可靠过渡时间,加起来最少也有600多ns;而且规定各个时间参数是可调的,所以…

    2022年4月30日
    69
  • MySQL——开窗函数

    MySQL——开窗函数

    2021年5月20日
    504
  • 错误0x8000ffff怎么办_不支持该请求0x0000032

    错误0x8000ffff怎么办_不支持该请求0x00000320x8000ffff错误是我们在Windows10更新、升级时经常会遇到的错误之一。当我们遇到这种错误时,应该怎么修复它呢?本文为大家作出原因分析,然后提供有效的解决方法。

    2022年9月25日
    1
  • BetterIntelliJ 2021.4.4 激活码_通用破解码

    BetterIntelliJ 2021.4.4 激活码_通用破解码,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月17日
    95
  • 数据结构哈希表怎么画(数据结构哈希算法)

    数据结构哈希表参考代码如下:/* 名称:哈希表 语言:数据结构C语言版 编译环境:VC++6.0 日期:2014-3-26*/#include#include#include#defineNULLKEY0 //0为无记录标志#defineN10 //数据元素个数typedefintKeyType;//设关键字域为整型

    2022年4月11日
    393
  • 错误:备份集中的数据库备份与现有XXX数据库不同

    错误:备份集中的数据库备份与现有XXX数据库不同当在使用另外一台的数据库备份文件.bak恢复到本机数据库时,遇到“备份集中的数据库备份与现有XXX数据库不同”的错误时,只需要以下几步:删除与要恢复数据库同名的已经存在的数据库; 右击“数据库”选择“还原数据库”;选择源设备;然后再在弹出窗口的“目标数据库”中选择要还原数据库名;这时要备份的数据库的名字会弹出来,所以不需要提前创建现在手中有SqlServer2…

    2022年6月12日
    32

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号