关于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和取值)的更多相关文章
- Android之PreferenceActivity 详解
看到很多书中都没有对PreferenceActivity做介绍,而我正好又在项目中用到,所以就把自己的使用的在这总结一下,也方便日后查找. PerferenceActivity是什么,看下面的截图: ...
- 自定义带有图片的PreferenceActivity
http://my.oschina.net/huangsm/blog/40027 和大家分享一下关于android中PreferenceActivity使用以及为配置信息文件中添加图标的功能,首先给大 ...
- PreferenceActivity详解
为了引入这个概率 首先从需求说起 即:现有某Activity专门用于手机属性设置 那么应该如何做呢? 根据已学知识 很快一个念头闪过 即:Activity + Preference 组合 前者用于界面 ...
- 从源码角度一步一步来修改PreferenceActivity界面
PreferenceActivity给我们封装好了一个数据存储对象,我们只需要在xml文件中写上控件即可完成简单的设置界面.但是系统提供的设置界面十分的简陋,要想做的好看必须要自己来进行修改 ...
- android基础知识:SharedPreferences和PreferenceActivity
1.android文件存储 对Android系统了解的都知道,Android系统有四种基本的数据保存方法,一是SharedPreference,二是文件,三是SQLite,四是ContentProvi ...
- 很全面的Android面试题
Activity 什么是Activity 四大组件之一,一个和用户交的互界面就是一个activity,是所有 View 的容器 我开发常用的的有FragmentActivitiy,ListActivi ...
- 第三部分:Android 应用程序接口指南---第二节:UI---第五章 设置(Settings)
第5章 设置(Settings) 应用程序通常包括允许用户修改应用程序的特性和行为的设置功能.例如,一些应用程序允许用户指定通知是否启用或指定多久使用云同步数据.如果你想要为你的应用程序提供设置,你应 ...
- 【起航计划 028】2015 起航计划 Android APIDemo的魔鬼步伐 27 App->Preferences->Launching preferences 其他activity获取Preference中的值
前给例子介绍了如何使用PreferenceActivity 来显示修改应用偏好,用户对Preferences的修改自动存储在应用对应的Shared Preferences中. 本例介绍了如何从一个Ac ...
- 【转】 Pro Android学习笔记(五七):Preferences(1):ListPreference
目录(?)[-] 例子1ListPreference小例子 定义一个preferences XML文件 继承PreferenceActivity 用户定制偏好的读取 第一次运行时设置缺省值 设置Cat ...
随机推荐
- c++ 函数的函数声明
c++ 函数的函数声明 只要在被调用函数的首部的末尾加一个分号,就成为对该函数的函数声明.函数声明的位置应当在函数调用之前. #include <iostream> using names ...
- 【iOS开发必收藏】详解iOS应用程序内使用IAP/StoreKit付费、沙盒(SandBox)测试、创建测试账号流程!【2012-12-11日更新获取”产品付费数量等于0的问题”】
转的别人的 看到很多童鞋问到,为什么每次都返回数量等于0?? 其实有童鞋已经找到原因了,原因是你在 ItunesConnect 里的 “Contracts, Tax, and Banking”没有完成 ...
- QQ协议的TEA加解密算法
QQ通讯协议里的加解密算法. #include <stdio.h> #include <stdlib.h> #include <memory.h> #include ...
- 【HDOJ】2732 Leapin' Lizards
贪心+网络流.对于每个结点,构建入点和出点.对于每一个lizard>0,构建边s->in position of lizard, 容量为1.对于pillar>0, 构建边in pos ...
- hadoop2.2编程: 重写comparactor
要点: 类型比较在hadoop的mapreduce中非常重要,主要用来比较keys; hadoop中的RawComparator<T>接口继承自java的comparator, 主要用来比 ...
- (转载)可重入函数(reentrant function)
(转载)http://blog.163.com/xu_jin_rong/blog/static/1491966220086775017178 由于cublog系统的缘故,将前段时间写的一篇blog文章 ...
- HDOJ --- 2151 Worm
Worm Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- Number Sequence ----HDOJ 1711
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 【转】qtp-learn
1.计算器的例子(手动添加,将结果写到日志文件中) SystemUtil.Run "C:\WINDOWS\system32\calc.exe",""," ...
- django 项目部署在 Apache 后, 设置二级域名(Apache虚拟主机 、 万网二级域名设置)
上一篇文章简单说了怎么把django的项目部署到Apache上. 现在想弄个二级域名,也就是我原来有个域名 www.mysite.com,现在我想弄个 bbs.mysite.com ,该怎么做呢. 要 ...