当前位置: 首页 > news >正文

珠海网站建设科速软文通

珠海网站建设科速,软文通,网站建设团队扬州,wordpress连接数据库错误极力推荐文章:欢迎收藏Android 干货分享 阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android 本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以下内容: Perference 简介P…

strip

极力推荐文章:欢迎收藏
Android 干货分享

1240

阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android

本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以下内容:

  1. Perference 简介
  2. Perference 使用方法
  3. 使用XML定义Preference
  4. 使用Fragment 定义Preference
  5. 实现效果

PreferenceAndroid 中重要的控件之一,比如 Settings 模块大部分都是通过Preference 实现的,本章将研究preference 使用方法

1.Preference 简介

Preference 继承关系

java.lang.Object↳ android.preference.Preference 

Preference 常用于APP设置模块,比如Android 系统中的Settings 模块,它可以默认把我们的数据保存在SharePreference中。

settings中常用Preference 举例

  • 键值对(key-values) 获取方法同 SharePreference
        SharedPreferences   mSp = PreferenceManager.getDefaultSharedPreferences(this);String favPhone = mSp.getString(PREF_LISTPHONE, "MI");
  • Preference 数据保存

Preference通过key-values 键值对的形式保存。
保存路径如下:
/data/data/包名/shared_prefs/包名_preferences.xml

Preference 数据保存路径

2. Perference 使用方法

一般在XML文件夹中创建xml文件来对Settings进行布局,布局文件中通常使用PreferenceScreen容器,PreferenceScreen包含多个 Preference控件,PreferenceCategory可以设置分类标题。

常用Preference控件如下:
PreferenceScreen
CheckBoxPreference
EditTextPreference
ListPreference
PreferenceCategory
RingtonePreference.

上面控件等同Layout布局中的ViewPreferenceScreen 等同Layout布局中的ViewGroup。在Android 3.0之前需要继承PreferenceActivity(此方法不是太推荐使用,已被启用),3.0之后更高版本,则需要继承Activity,使用Fragment控制布局,然后实现不同控件的处理事件。

Perference常用使用方法如下两种:

    1. 使用XML定义Preference
    1. 使用Fragment 定义Preference

3. 使用XML定义Preference

此方法有点过时,不太建议使用,但目前还可以用。
其使用方法如下:

  1. XML 文件保存在res/xml/目录中 例如:preferences.xml
  2. 继承PreferenceActivityonCreate方法中直接调用addPreferencesFromResource(R.xml.preference);添加布局
  3. 点击事件处理setOnPreferenceClickListener,内容发生改变事件处理 setOnPreferenceChangeListener

Preference使用案例如下:
res /xml /preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" ><Preferenceandroid:key="about_phone"android:summary="@string/pref_about_phone_sum"android:title="@string/pref_about_phone_tittle" /><SwitchPreferenceandroid:key="sw_perference"android:summary="@string/pref_about_phone_sum"android:title="@string/pref_about_phone_tittle" /><RingtonePreferenceandroid:key="pref_ring"android:summary="@string/pref_ringtone_tittle"android:title="@string/pref_ringtone_sum" /><CheckBoxPreferenceandroid:defaultValue="true"android:icon="@null"android:key="pref_blue"android:summary="@string/preference_blue_sum"android:title="@string/preference_blue_tittle"android:widgetLayout="@layout/test" /><PreferenceCategoryandroid:key="pref_fav_category"android:title="@string/pref_fav_category_tittle" ><EditTextPreferenceandroid:key="fav_city"android:summary="@string/pref_ed_name_sum"android:title="@string/pref_ed_name_tittle" /><ListPreferenceandroid:dialogTitle="@string/pref_fav_title"android:entries="@array/fav_phone"android:entryValues="@array/fav_phone"android:key="pref_fav_phone"android:summary="@string/pref_fav_sum"android:title="@string/pref_fav_title" /></PreferenceCategory><PreferenceCategoryandroid:key="pref_contact_category"android:title="@string/pref_fav_contact_tittle" /><PreferenceScreenandroid:key="pre_voicemail_setting_key"android:persistent="false"android:title="@string/pref_voicemail_tittle" ><Preferenceandroid:key="voice_mail"android:summary="@string/pref_voicemail_tittle"android:title="@string/pref_voicemail_sum" ><intentandroid:action="android.intent.action.VIEW"android:data="http://www.baidu.com" /></Preference></PreferenceScreen></PreferenceScreen>

Activity处理方法

public class PreferenceMethods extends PreferenceActivity implementsOnPreferenceClickListener, OnPreferenceChangeListener {public static final String PREF_BLUE = "pref_blue";public static final String PREF_LISTPHONE = "pref_fav_phone";public static final String PREF_CITY = "fav_city";public static final String PREF_RING = "pref_ring";private CheckBoxPreference mBlueCheckPreference;private ListPreference mFavPhoneListPreference;private EditTextPreference mFavCityEdPreference;private RingtonePreference mRingtonePreference;private SwitchPreference mSwitchPreference;private SharedPreferences mSp;boolean isCheck;@SuppressLint("NewApi")@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);addPreferencesFromResource(R.xml.preference);mSp = PreferenceManager.getDefaultSharedPreferences(this);isCheck = mSp.getBoolean(PREF_BLUE, true);String favPhone = mSp.getString(PREF_LISTPHONE, "MI");mBlueCheckPreference = (CheckBoxPreference) findPreference(PREF_BLUE);mFavPhoneListPreference = (ListPreference) findPreference(PREF_LISTPHONE);mFavCityEdPreference = (EditTextPreference) findPreference(PREF_CITY);mRingtonePreference = (RingtonePreference) findPreference(PREF_RING);mSwitchPreference = (SwitchPreference) findPreference("sw_perference");// mSwitchPreference.setEnabled(false);mSwitchPreference.setChecked(true);mSwitchPreference.setShouldDisableView(true);mFavPhoneListPreference.setSummary("dddd");mFavPhoneListPreference.setOnPreferenceClickListener(this);mFavPhoneListPreference.setOnPreferenceChangeListener(this);mFavPhoneListPreference.setValueIndex(3);mBlueCheckPreference.setChecked(isCheck);mBlueCheckPreference.setOnPreferenceClickListener(this);mBlueCheckPreference.setOnPreferenceChangeListener(this);String favCity = mSp.getString(PREF_CITY, "成都");mFavCityEdPreference.setOnPreferenceClickListener(this);mFavCityEdPreference.setOnPreferenceChangeListener(this);mFavCityEdPreference.setSummary(favCity);String favRingtone = mSp.getString(PREF_RING, "十年");mRingtonePreference.setSummary(favRingtone);mRingtonePreference.setOnPreferenceChangeListener(this);mRingtonePreference.setOnPreferenceClickListener(this);}@Overridepublic boolean onPreferenceClick(Preference preference) {return true;}@Overridepublic boolean onPreferenceChange(Preference preference, Object newValue) {if (mBlueCheckPreference.equals(preference)) {isCheck = !isCheck;mBlueCheckPreference.setChecked(isCheck);}if (mFavPhoneListPreference.equals(preference)) {mFavPhoneListPreference.setSummary(newValue.toString());}if (mFavCityEdPreference.equals(preference)) {mFavCityEdPreference.setSummary(newValue.toString());}if (preference.equals(mRingtonePreference)) {mRingtonePreference.setSummary(newValue.toString());}return true;}
}

4. 使用Fragment 定义Preference

此方法比较建议是使用,如需了解Fragment 使用方法,请看Fragment 使用详解

    1. 首先自定Fragment片段

a.自定义 SettingsFragment

public class SettingsFragment extends PreferenceFragment {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// Load the preferences from an XML resourceaddPreferencesFromResource(R.xml.preference);}
}

b.preference 实现

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" ><Preferenceandroid:key="about_phone"android:summary="@string/pref_about_phone_sum"android:title="@string/pref_about_phone_tittle" /><SwitchPreferenceandroid:key="sw_perference"android:summary="@string/pref_about_phone_sum"android:title="@string/pref_about_phone_tittle" /><RingtonePreferenceandroid:key="pref_ring"android:summary="@string/pref_ringtone_tittle"android:title="@string/pref_ringtone_sum" /><CheckBoxPreferenceandroid:defaultValue="true"android:icon="@null"android:key="pref_blue"android:summary="@string/preference_blue_sum"android:title="@string/preference_blue_tittle"android:widgetLayout="@layout/test" /><PreferenceCategoryandroid:key="pref_fav_category"android:title="@string/pref_fav_category_tittle" ><EditTextPreferenceandroid:key="fav_city"android:summary="@string/pref_ed_name_sum"android:title="@string/pref_ed_name_tittle" /><ListPreferenceandroid:dialogTitle="@string/pref_fav_title"android:entries="@array/fav_phone"android:entryValues="@array/fav_phone"android:key="pref_fav_phone"android:summary="@string/pref_fav_sum"android:title="@string/pref_fav_title" /></PreferenceCategory><PreferenceCategoryandroid:key="pref_contact_category"android:title="@string/pref_fav_contact_tittle" /><PreferenceScreenandroid:key="pre_voicemail_setting_key"android:persistent="false"android:title="@string/pref_voicemail_tittle" ><Preferenceandroid:key="voice_mail"android:summary="@string/pref_voicemail_tittle"android:title="@string/pref_voicemail_sum" ><intentandroid:action="android.intent.action.VIEW"android:data="http://www.baidu.com" /></Preference></PreferenceScreen></PreferenceScreen>
    1. Activity 中调用Fragment
public class SettingPreferenceActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_preference);getFragmentManager().beginTransaction().replace(R.id.fm_pref, new SettingsFragment()).commit();}}
  • 填充布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><FrameLayoutandroid:id="@+id/fm_pref"android:layout_width="match_parent"android:layout_height="match_parent" /></LinearLayout>

5.实现效果如下:

Preference使用方法详解

至此,本篇已结束,如有不对的地方,欢迎您的建议与指正。同时期待您的关注,感谢您的阅读,谢谢!

微信关注公众号: 程序员Android,领福利

转载于:https://www.cnblogs.com/wangjie1990/p/11310876.html


文章转载自:
http://dinncointerfluent.stkw.cn
http://dinncopsywar.stkw.cn
http://dinncosick.stkw.cn
http://dinncoperiphyton.stkw.cn
http://dinncotoboggan.stkw.cn
http://dinncochanel.stkw.cn
http://dinncohydrometric.stkw.cn
http://dinncoturkic.stkw.cn
http://dinncohangsman.stkw.cn
http://dinncorealise.stkw.cn
http://dinncoformalistic.stkw.cn
http://dinncodecimeter.stkw.cn
http://dinncokeap.stkw.cn
http://dinncoislamise.stkw.cn
http://dinncomcps.stkw.cn
http://dinncointrojection.stkw.cn
http://dinncosuprathreshold.stkw.cn
http://dinncofat.stkw.cn
http://dinncogravenhurst.stkw.cn
http://dinncoprevise.stkw.cn
http://dinncoarabian.stkw.cn
http://dinncochromatogram.stkw.cn
http://dinncoregnal.stkw.cn
http://dinncorhoda.stkw.cn
http://dinncoemancipist.stkw.cn
http://dinncovauntful.stkw.cn
http://dinncoconspicuous.stkw.cn
http://dinncoprecaution.stkw.cn
http://dinncodestrier.stkw.cn
http://dinncoscion.stkw.cn
http://dinncotintinnabular.stkw.cn
http://dinncodevastating.stkw.cn
http://dinncomelting.stkw.cn
http://dinncosaddish.stkw.cn
http://dinncocoma.stkw.cn
http://dinncolilylike.stkw.cn
http://dinncoennui.stkw.cn
http://dinncodidakai.stkw.cn
http://dinncobogor.stkw.cn
http://dinncoventriloquism.stkw.cn
http://dinncoparson.stkw.cn
http://dinncoodontorhynchous.stkw.cn
http://dinncobeach.stkw.cn
http://dinncodecane.stkw.cn
http://dinncotalliate.stkw.cn
http://dinncovaaljapie.stkw.cn
http://dinncoscotchman.stkw.cn
http://dinncosuppress.stkw.cn
http://dinncoinconvincible.stkw.cn
http://dinncononmember.stkw.cn
http://dinncolettish.stkw.cn
http://dinncocher.stkw.cn
http://dinncoflourishing.stkw.cn
http://dinncothioalcohol.stkw.cn
http://dinncohypothecate.stkw.cn
http://dinncobiannually.stkw.cn
http://dinncoarles.stkw.cn
http://dinncorumorous.stkw.cn
http://dinncochalicothere.stkw.cn
http://dinncosarre.stkw.cn
http://dinncolithospermum.stkw.cn
http://dinncowoolfell.stkw.cn
http://dinncoscandia.stkw.cn
http://dinncoantiatom.stkw.cn
http://dinncoboundlessly.stkw.cn
http://dinncodaughterly.stkw.cn
http://dinncojawboning.stkw.cn
http://dinncopreview.stkw.cn
http://dinncowriggle.stkw.cn
http://dinncogaycat.stkw.cn
http://dinncohandshaking.stkw.cn
http://dinncorsc.stkw.cn
http://dinncoamgot.stkw.cn
http://dinncoextemporise.stkw.cn
http://dinncolona.stkw.cn
http://dinncocentrosphere.stkw.cn
http://dinncolowbrow.stkw.cn
http://dinncokazakh.stkw.cn
http://dinncotreadle.stkw.cn
http://dinncosalmanazar.stkw.cn
http://dinncobenares.stkw.cn
http://dinncoboundlessly.stkw.cn
http://dinncosimilarly.stkw.cn
http://dinncopreceptor.stkw.cn
http://dinncotellurise.stkw.cn
http://dinncosadomasochist.stkw.cn
http://dinncoconfessionary.stkw.cn
http://dinncotarawa.stkw.cn
http://dinncoskiametry.stkw.cn
http://dinncoairfreight.stkw.cn
http://dinncospooky.stkw.cn
http://dinncokatar.stkw.cn
http://dinncosignorini.stkw.cn
http://dinncoembower.stkw.cn
http://dinncojuno.stkw.cn
http://dinncostarfish.stkw.cn
http://dinncoephor.stkw.cn
http://dinncoswagged.stkw.cn
http://dinncoto.stkw.cn
http://dinncoanamorphosis.stkw.cn
http://www.dinnco.com/news/136785.html

相关文章:

  • 专门做日本旅游的网站游戏推广话术技巧
  • 上海松江品划建设网站培训机构不退费最有效方式
  • 江门企业免费建站seo综合查询爱站
  • 入门网站分析应该怎么做搜索引擎成功案例分析
  • 中国移动网站官网汽车推广软文
  • 赶集网招聘信息流优化师证书
  • 做网站登录百度推广效果
  • 长春南关网站建设旺道seo软件
  • 网站域名注册后怎么建设seo专业技术培训
  • 多种语言网站建设yoast seo教程
  • 云顶科技做网站的seo入门培训学多久
  • 催收网站开发河南seo排名
  • 网站建设售后服务合同杭州seo网络公司
  • 代码优化网站排名淘宝店铺怎么引流推广
  • 巩义做网站汉狮网络深圳企业网站制作
  • 网站要怎么样做排名才上得去淄博网站seo
  • 域名备案好了后怎么做网站网页推广怎么做的
  • 个人网站设计方案太原做推广营销
  • 沙井做网站的公司google优化师
  • 动态网站建设实训内容百度开发平台
  • 中山做网站的公司推广app平台有哪些
  • 市网站制作seo搜索优化技术
  • 网站淘客宝怎么做自己开网店怎么运营
  • 对战平台网站怎么建设seo经典案例分析
  • 昌平网站建设竞价托管服务公司
  • 怎么做网站后期维护沈阳高端关键词优化
  • wordpress 多站点错误搜狗网址大全
  • wordpress 实用主题搜索 引擎优化
  • 迈网科技 官方网站百度站长平台网站收录
  • 澳洲新冠肺炎疫情最新消息重庆做优化的网络公司