关于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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Faster-RCNN算法精读[通俗易懂]

    Faster-RCNN算法精读[通俗易懂]读论文:《FasterR-CNN:TowardsReal-TimeObjectDetectionwithRegionProposalNetworks》摘要:算法主要解决两个问题:1、提出区域建议网络RPN,快速生成候选区域;2、通过交替训练,使RPN和Fast-RCNN网络共享参数。一、RPN网络结构RPN网络的作用是输入一张图像,输出一批矩形候选区域

    2022年10月4日
  • Java Spring框架 (底层原理+入门)

    Java Spring框架 (底层原理+入门)JavaSpring框架Spring一、Spring的概述定义:Spring框架是一个开放源代码的J2EE应用程序框架,由RodJohnson发起,是针对bean的生命周期进行管理的轻量级容器(lightweightcontainer)。Spring解决了开发者在J2EE开发中遇到的许多常见的问题,提供了功能强大IOC、AOP及WebMVC等功能。Spring可以单独应用于构筑应用程序,也可以和Struts、Webwork、Tapestry等众多Web框架组合使用,并且可以与Swing

    2022年7月7日
  • C/C++读写文本文件、二进制文件「建议收藏」

    C/C++读写文本文件、二进制文件「建议收藏」C语言文本文件读写方式;C语言二进制文件读写方式;CPP文本文件读写方式;CPP二进制文件读写方式;

    2022年6月1日
  • Failed to find Platform SDK with path: platforms;android-30「建议收藏」

    Failed to find Platform SDK with path: platforms;android-30「建议收藏」在运行的时候出现这个错误,版本的要求不一样点上对勾,直接apply就可以了

    2022年5月11日
  • Spring整合SpringDataJpa的乐观锁与悲观锁详情

    Spring整合SpringDataJpa的乐观锁与悲观锁详情Spring整合SpringDataJpa的乐观锁与悲观锁详情一、概述上一篇《Spring和SpringDataJpa整合详解》介绍了Spring如何结合Spring-data-jpa进行数据库访问操作。这一篇介绍下springmvc环境下spring-data-jpa如何进行乐观锁、悲观锁的使用。悲观锁和乐观锁的概念:悲观锁:就是独占锁,不管读写都上锁了。传统的关系型数据库里边就用到…

    2022年4月29日
  • linux网卡驱动怎么看_电脑网卡驱动在哪里查看

    linux网卡驱动怎么看_电脑网卡驱动在哪里查看Linux下查看网卡驱动和版本信息查看网卡生产厂商和信号查看基本信息:lspci查看详细信息:lspci-vvv#3个小写的v查看网卡信息:lspci|grepEthernet查看网卡驱动查看网卡驱动信息:lspci-vvv#找到网卡设备的详细信息,包括网卡驱动列出加载的所有驱动,包括网卡驱动:lsmod查看模块

    2022年10月18日

发表回复

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

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