<headerandroid:fragment="kadacom.com.pad.ui.activity.progressbar.SettingsActivity$GeneralPreferenceFragment"android:icon="@drawable/ic_info_black_24dp"android:title="@string/pref_header_general" />

有这种的不多说 ,直接 SwitchPreference

package  xxxx.com.pad.ui.activity.progressbar;import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.preference.RingtonePreference;
import android.preference.SwitchPreference;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;import com.sdsmdg.tastytoast.TastyToast;import xxxxx.com.pad.R;import static android.os.ParcelFileDescriptor.MODE_WORLD_READABLE;public class PrefereFragment extends PreferenceFragment {private SwitchPreference mMistakeTouchPreference;private static final String MISTAKE_TOUCH_MODE_KEY = "nice";@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);addPreferencesFromResource(R.xml.pref_general);//        bindPreferenceSummaryToValue(findPreference("example_switch"));bindPreferenceSummaryToValue(findPreference("example_text"));bindPreferenceSummaryToValue(findPreference("example_list"));
//        bindPreferenceSummaryToValue(findPreference("nice"));initMistakeTouchPreference();}private void initMistakeTouchPreference() {mMistakeTouchPreference = (SwitchPreference)findPreference(MISTAKE_TOUCH_MODE_KEY);mMistakeTouchPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {@Overridepublic boolean onPreferenceChange(Preference preference, Object objValue) {final String key = preference.getKey();System.out.print("key--"+key);if (MISTAKE_TOUCH_MODE_KEY.equals(key)){if (mMistakeTouchPreference.isChecked() != (Boolean)objValue) {boolean value = (Boolean)(objValue);mMistakeTouchPreference.setChecked(value);//最自己的设置}}return true;}});}@Overridepublic void onViewCreated(View view, @Nullable Bundle savedInstanceState) {super.onViewCreated(view, savedInstanceState);}private static void bindPreferenceSummaryToValue(Preference preference) {preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,PreferenceManager.getDefaultSharedPreferences(preference.getContext()).getString(preference.getKey(), ""));}private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {@Overridepublic boolean onPreferenceChange(Preference preference, Object value) {String stringValue = value.toString();if (preference instanceof ListPreference) {// For list preferences, look up the correct display value in// the preference's 'entries' list.ListPreference listPreference = (ListPreference) preference;int index = listPreference.findIndexOfValue(stringValue);// Set the summary to reflect the new value.preference.setSummary(index >= 0? listPreference.getEntries()[index]: null);//                TastyToast.makeText(get,stringValue+"", TastyToast.LENGTH_SHORT, 2);} else if (preference instanceof RingtonePreference) {// For ringtone preferences, look up the correct display value// using RingtoneManager.if (TextUtils.isEmpty(stringValue)) {// Empty values correspond to 'silent' (no ringtone).preference.setSummary(R.string.pref_ringtone_silent);} else {Ringtone ringtone = RingtoneManager.getRingtone(preference.getContext(), Uri.parse(stringValue));if (ringtone == null) {// Clear the summary if there was a lookup error.preference.setSummary(null);} else {// Set the summary to reflect the new ringtone display// name.String name = ringtone.getTitle(preference.getContext());preference.setSummary(name);}}} else {// For all other preferences, set the summary to the value's// simple string representation.preference.setSummary(stringValue);}return true;}};
}

bindPreferenceSummaryToValue 这个方法支持 自动生成的代码里面说的
只是适用这几个
// Bind the summaries of EditText/List/Dialog/Ringtone preferences
// to their values. When their values change, their summaries are
// updated to reflect the new value, per the Android Design
// guidelines.

SwitchPreference 要用的话,单独写Listener 下

pref_general.xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"><SwitchPreferenceandroid:defaultValue="true"android:key="nice"android:summary="@string/pref_description_social_recommendations"android:title="@string/pref_title_social_recommendations" /><!-- NOTE: EditTextPreference accepts EditText attributes. --><!-- NOTE: EditTextPreference's summary should be set to its value by the activity code. --><EditTextPreferenceandroid:capitalize="words"android:defaultValue="@string/pref_default_display_name"android:inputType="textCapWords"android:key="example_text"android:maxLines="1"android:selectAllOnFocus="true"android:singleLine="true"android:title="@string/pref_title_display_name" /><!-- NOTE: Hide buttons to simplify the UI. Users can touch outside the dialog todismiss it. --><!-- NOTE: ListPreference's summary should be set to its value by the activity code. --><ListPreferenceandroid:defaultValue="-1"android:entries="@array/pref_example_list_titles"android:entryValues="@array/pref_example_list_values"android:key="example_list"android:negativeButtonText="@null"android:positiveButtonText="@null"android:title="@string/pref_title_add_friends_to_messages" /></PreferenceScreen>

然后添加下这个 PrefereFragment

public class ActivityPreferenceFragmentActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_preference_fragment);FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();fragmentTransaction.replace(R.id.frame_container, new PrefereFragment()).commit();}
}

activity_preference_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="kadacom.com.pad.ui.activity.progressbar.ActivityPreferenceFragmentActivity"><FrameLayoutandroid:id="@+id/frame_container"android:layout_width="match_parent"android:layout_height="match_parent" /></android.support.constraint.ConstraintLayout>

activity 等地方获取具体参数

  SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);String nide = prefs.getString("example_list","");System.out.println(nide);

android SwitchPreference ListPreference使用相关推荐

  1. android list字体大小,android自定义ListPreference字体大小

    这篇博客算是自己的一个记录吧,组长给了一个任务,需要实现一个紧急广播的一系列设置功能,但是客户那边设置界面的字体大小有特殊的要求,看了一下,对于ListPreference这样的控件,android系 ...

  2. Android 自定义ListPreference

    android-重写ListPrefrence添加一个OK按钮 效果图: 下面是源码: 自定义ListPreference源码: package com.tangzq.customize; impor ...

  3. android 自定义listpreference,java-Android,单击ListPreference项时显示对话框

    基本上,我有一个ListPreference,允许用户更改我的动态壁纸上某些文本的X位置. 它包含4个条目:顶部,中间,底部和手动输入X.前3个选项没问题,我只需在我的WallpaperService ...

  4. Android中类ListPreference使用介绍

    类ListPreference 一.概要: 1.XML属性 属性名称 相关方法 描述 android:entries 展现给用户的下拉列表的值 android:entryValues 展现的用户的选择 ...

  5. android switchpreference 怎么切换,Android中的自定义SwitchPreference

    您必须为交换机本身创建自定义布局,您可以像动态一样动态应用它. preference.setWidgetLayoutResource(R.layout.custom_switch); 但我会详细介绍如 ...

  6. android 自定义 ListPreference summary title 颜色

    在主题中添加以下配置 <!-- "Theme.AppCompat.Light.NoActionBar" 该主题默认是黑色字体 --> <!-- "The ...

  7. [Android分享] Android ListPreference的用法

    首先,我们明确,preference是和数据存储相关的. 其次,它能帮助我们方便的进行数据存储!为什么这个地方一定要强调下方便的这个词呢?原因是,我们可以根本就不使用,我们有另外的N种办法可以实现同样 ...

  8. android listpreference 自定义,android – 自定义布局ListPreference

    在您的preference.xml文件中,您可以通过类的全名来引用自定义ListPreference,即com.example.MyPreference android:key="pref_ ...

  9. android 弹出框 list,android - 如何实现点击listpreference 后,弹出一个dialog?

    小皮2017-04-17 13:21:011楼 android.preference.ListPreference 源码如下: @Override protected void onPrepareDi ...

最新文章

  1. ICCV2021|一种用于解决点云场景中同类对象分割的网络
  2. puppy linux不识别鼠标,不止于OS X!还有适用于Mac的八款替代操作系统
  3. 使用ExecutorService来停止线程服务
  4. 求给定精度的简单交错序列部分和_单个神经元的简单模型:Leaky integrate and fire (LIF) model...
  5. Qt拖放 drag and drop
  6. 好程序员web前端培训分享JavaScript框架
  7. 小学计算机课画图软件,小学小学三年级信息技术使用画图软件(一)
  8. 安卓4.X的桌面启动器-尖端启动器APEX_我是亲民_新浪博客
  9. 京东用户行为数据分析报告(python)
  10. TCP的三次握手与四次挥手
  11. MFC 执行顺序总结
  12. source 1.5 中不支持 diamond 运算符 (请使用 -source 7 或更高版本以启用 diamond 运算符) 问题解决
  13. 字节跳动前端实习一面二面(凉面)
  14. 【C标准库】详解fopen函数 一篇让你搞懂fopen函数
  15. 击碎数据标注五大误解,这门生意真不是你想象的“富士康” | 钛媒体深度
  16. node.js毕业设计安卓在线教育APP(程序+APP+LW)
  17. 关于 写代码 2013年9月28日18:22:43
  18. InVEST模型,掌握产水(包括水源涵养)、碳存储(包括固碳)、土壤保持、水质(氮磷)、生境质量和热岛缓解等生态系统服务评估方法,开展人类活动影响、重大工程实施的生态成效评估
  19. 财务报表怎么做?这款报表工具你千万不能错过!
  20. 计算机逻辑Introduction

热门文章

  1. 智慧社区安防和雪亮工程建设方案
  2. IO流批量改文件名字,把文件夹中类似于文件名,“我java_爱好者_最帅“改成“爱好者_最轻特工组合“
  3. 在 Ubuntu19.10 上安装 wine 并安装 QQ 等软件
  4. 《爱情路过广州拐角》经典简摘
  5. 最新系统漏洞--D-Link DAP-1330堆栈缓冲区溢出漏洞
  6. 基于SQLserver实现的商品销售信息管理系统
  7. 闺蜜测试卷软件,闺蜜期末测试卷
  8. 阿里CTO程立:好的架构师,都是写代码写出来的!
  9. 三个小时完成毕业论文的方法
  10. 深入浅出通信原理(Python代码版)