Android PreferenceActivity 使用

我想大家對于android的系統配置界面應該不會陌生吧,即便陌生,那么下面的界面應該似曾相識吧,假若還是不認識,那么也沒有關系,我們這一節主要就是介紹并講解android 中系統配置界面的使用,相信大家看完本節后,一定能知道怎么使用這些東西了。

從上述界面中(其實是一個界面),我們可以看到有2組元素,第一組為:自動更新,更新頻率。第二組為:是否登錄,賬號,密碼。

其實分組是為了方便管理而已。這里主要使用到了選擇,列表,編輯框等Preference.

我們還是來溫習下基礎知識吧。

一.基礎知識

通常我們開發一個程序時,會需要給用戶提供一個設置界面,使用戶可以對程序的一些參數進行設置。通常我們使用Preferences的鍵值對存儲方式,來對Android數據持久化。

?

android.content.SharedPreferences是一個接口,用來獲取和修改持久化存儲的數據。有三種方式可以獲取系統中持久化的數據,這些數據時存放在.xml中的:

1:public?SharedPreferences?getPreferences?(int?mode),只隸屬于activity對象,并以這個對象為文件名。

2:public?SharedPreferences?getSharedPreferences?(String?name,?int?mode),隸屬于整個應用,以name名稱保存。

3:public?static?SharedPreferences?getDefaultSharedPreferences?(Context?context),隸屬于整個應用,保存PreferenceActivity中的設置,根據包名和PreferenceActivity的布局文件來起一個名字保存。

SharedPreferences可以保存BooleanIntFloatLongString等類型。一般是SharedPreferences.Editor的putXXX()方法保存,并commit()方法提交;或者是remove(),方法移除,clear()方法清空,當然操作完畢后需要commit()方法提交。

??? 一般的,可以使用SharedPreferences保存持久化數據,比如登陸賬號,密碼,以及相對固定設置參數等等,看你應用需要。

??? 既然使用到設置參數界面,那么我們可以來認識下一下幾個類標簽:

1:PreferenceScreen 持久化設置界面的頂級容器,代表一屏,里面可以嵌套屏幕,嵌套的時候可以點擊跳轉到另外一屏。

2:PreferenceCategory 當前屏的分組容器,說白了,就是為了劃分組而已。

3:CheckBoxPreference,ListPreference,EditTextPreference等等組件。這些都是常見的對應上述的選擇標簽、列表標簽、編輯標簽。

上述組件都有titile,summary,key屬性。title標題描述,summary:詳細描述,key:保存SharedPreferences時候的鍵。

?

二.實戰

既然有了上述基礎知識后,我們就可以來做如上的設置界面了,在上述界面中,我們知道當前配置界面只需一屏,沒有額外的跳轉,那么需要PreferenceScreen,不需要嵌套其他PreferenceScreen,而設置界面分為兩欄,那么需要兩個PreferenceCategory,并且這個PreferenceCategory可以設置標題。同時根據界面,我們還需要CheckBoxPreferenceListPreference,EditTextPreference等。操作如圖:

?

?

當然了,我們還是實際操作下吧,然后實際聯系理論(理論聯系實際太抽象),我們首先編寫使用工具選擇標簽,然后形成代碼,存放在xml文件夾中,如下:

View Code
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title
="@string/setting" >
<PreferenceCategory android:title="@string/update" >
<CheckBoxPreference
android:key="@string/update_key"
android:summaryOn
="@string/auto_update_switch_summary_on"
android:summaryOff
="@string/auto_update_switch_summary_off"
android:summary
="@string/auto_update_setting"
android:title
="@string/auto_update_switch_title"
android:defaultValue
="true"
/>
<ListPreference
android:key="@string/auto_update_frequency_key"
android:dialogTitle
="@string/auto_update_frequency_title"
android:entries
="@array/auto_frequency_name"
android:entryValues
="@array/auto_frequency_value"
android:negativeButtonText
="@string/cancel"
android:summary
="@string/auto_update_frequency_summary"
android:title
="@string/auto_update_frequency_title" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/other" >
<CheckBoxPreference
android:key="@string/isneilflag_key"
android:title
="@string/isneilflag"
android:disableDependentsState
="true"
/>
<EditTextPreference
android:dependency="@string/isneilflag_key"
android:key
="@string/username_key"
android:summary
="@string/username_summary"
android:title
="@string/username" />
<EditTextPreference
android:dependency="@string/isneilflag_key"
android:key
="@string/password_key"
android:summary
="@string/password_summary"
android:title
="@string/password" />
</PreferenceCategory>
</PreferenceScreen>

當然這里使用到了一些文件,代碼一并釋放:

View Code
<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="hello">Hello World, PreferenceActivityDemoActivity!</string>
<string name="app_name">PreferenceActivityDemo</string>
<string name="cancel">取消</string>
<string name="other">其他</string>
<string name="setting">設置</string>
<string name="update">更新</string>
<string name="auto_update_switch_title">自動更新</string>
<string name="auto_update_setting">自動更新設置</string>
<string name="auto_update_switch_summary_on">開啟自動更新</string>
<string name="auto_update_switch_summary_off">關閉自動更新</string>
<string name="auto_update_frequency_title">更新頻率</string>
<string name="auto_update_frequency_summary">設置更新周期</string>

<string name="isneilflag">是否匿名登錄</string>
<string name="username">帳號</string>
<string name="password">密碼</string>
<string name="username_summary">提供您帳號</string>
<string name="password_summary">提供您密碼</string>


<string name="update_key">update_key</string>
<string name="auto_update_frequency_key">auto_update_frequency_key</string>
<string name="isneilflag_key">isneilflag_key</string>
<string name="username_key">username_key</string>
<string name="password_key">password_key</string>
</resources>
View Code
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="auto_frequency_name">
<item>10分鐘</item>
<item>30分鐘</item>
<item>1小時</item>
<item>12小時</item>
</string-array>

<string-array name="auto_frequency_value">
<item>10</item>
<item>30</item>
<item>60</item>
<item>720</item>
</string-array>
</resources>

接下來,我們就來解釋下xml文件夾中設置文件的一些內容吧:

?android:key="@string/update_key"?? 這個是我們持久化時SharedPreferences保存的鍵,
??????????? android:summaryOn="@string/auto_update_switch_summary_on"???? 選中后顯示內容
??????????? android:summaryOff="@string/auto_update_switch_summary_off"???? 未選中后顯示內容
??????????? android:summary="@string/auto_update_setting"???????????????????????????? 平常狀態顯示內容
??????????? android:title="@string/auto_update_switch_title"?????????????????????????????? 標題
??????????? android:defaultValue="true"???????????????????????????????????????????????????????????? 默認值
??android:dialogTitle="@string/auto_update_frequency_title"?????????????????????????? 對話框標題
??????????? android:entries="@array/auto_frequency_name"??????????????????????????????? 列表項顯示名稱
??????????? android:entryValues="@array/auto_frequency_value"????????????????????????? 列表項值

? android:disableDependentsState="true"????????? 當CheckBoxPreference的值為真的時候,禁用依賴于該設置的其他設置項。

? android:dependency="@string/isneilflag_key"? 根據上述設置依賴于CheckBoxPreference的真值進行啟用與禁用。

? 注意:我們可以從上圖知道ListPreference是繼承之Preference,以及DialogPreference的,那么就有它們的屬性以及方法了。

?好了,設置界面已經定義完成,我們來定義一個activity繼承之PreferenceActivity,并使之顯示吧。

?當然噩夢可以再使之繼承接口OnPreferenceChangeListener,OnPreferenceClickListener,PreferenceActivity中的某一個Preference進行了點擊或者改變的操作時,都會回調接口中的函數。

具體代碼如下:

View Code
package com.dongzi;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceManager;
import android.preference.PreferenceActivity;
import android.util.Log;
public class PreferenceActivityDemoActivity extends PreferenceActivity
implements OnPreferenceChangeListener,OnPreferenceClickListener{
static final String TAG="PreferenceActivityDemoActivity";
SharedPreferences preference=null;
CheckBoxPreference updateCheckBoxPreference=null;
ListPreference lististPreference=null;
CheckBoxPreference isneilflag_CheckBoxPreference=null;
EditTextPreference usernameEditTextPreference=null;
EditTextPreference passwordEditTextPreference=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//設置顯示Preferences
addPreferencesFromResource(R.xml.preference);
//獲得SharedPreferences
preference=PreferenceManager.getDefaultSharedPreferences(this);

//找到preference對應的Key標簽并轉化
updateCheckBoxPreference=(CheckBoxPreference)findPreference(getString(R.string.update_key));
lististPreference=(ListPreference)findPreference(getString(R.string.auto_update_frequency_key));
isneilflag_CheckBoxPreference=(CheckBoxPreference)findPreference(getString(R.string.isneilflag_key));
usernameEditTextPreference=(EditTextPreference)findPreference(getString(R.string.username_key));
passwordEditTextPreference=(EditTextPreference)findPreference(getString(R.string.password_key));
//為Preference注冊監聽
updateCheckBoxPreference.setOnPreferenceChangeListener(this);
updateCheckBoxPreference.setOnPreferenceClickListener(this);

lististPreference.setOnPreferenceClickListener(this);
lististPreference.setOnPreferenceChangeListener(this);

isneilflag_CheckBoxPreference.setOnPreferenceChangeListener(this);
isneilflag_CheckBoxPreference.setOnPreferenceClickListener(this);

usernameEditTextPreference.setOnPreferenceChangeListener(this);
passwordEditTextPreference.setOnPreferenceChangeListener(this);
}
@Override
public boolean onPreferenceClick(Preference preference) {
//判斷是哪個Preference改變了
if(preference.getKey().equals(getString(R.string.update_key))){
Log.e(TAG, getString(R.string.update_key));
}else if(preference.getKey().equals(getString(R.string.isneilflag_key))){
Log.e(TAG, getString(R.string.isneilflag_key));
}
//返回true表示允許改變
return true;
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
//判斷是哪個Preference改變了
if(preference.getKey().equals(getString(R.string.username_key))){
//賬號
Log.e(TAG, getString(R.string.username_key));
}else if(preference.getKey().equals(getString(R.string.password_key))){
//密碼
Log.e(TAG, getString(R.string.password_key));

}else if(preference.getKey().equals(getString(R.string.auto_update_frequency_key))){
//列表
Log.e(TAG, getString(R.string.auto_update_frequency_key));
}
//返回true表示允許改變
return true;
}
}

從上述代碼可知:?

1:設置顯示Preferences可以調用方法addPreferencesFromResource(R.xml.preference); 傳遞preference文件即可。

2:然后通過findPreference();方法找到鍵的類標簽

3:我們定義2個監聽接口監聽參數是否設置成功。

4:在監聽接口中返回true就是設置允許提交。

結果打印如下:

?

成功!

也許你會問,我們這里并沒有使用

SharedPreferences?settings?=?PreferenceManager.getDefaultSharedPreferences(this);

settings.getXXXX();settings.putXXXX();以及commit()等方法,設置、獲取并提交值啊?!是的,在這里我們不需這樣,因為

PreferenceActivity中的內容改變時,Android系統會自動進行保存和持久化維護,我們只需要在要用的設置界面中數據的地方進行讀取就可以了

當然,您可以在其他界面使用SharedPreferences?settings?=?PreferenceManager.getDefaultSharedPreferences(this);等類方法來獲取數據,看能不能獲取到

?

<linker : http://www.cnblogs.com/zhangdongzi/archive/2012/01/05/2313519.html>

?

?

?

轉載于:https://www.cnblogs.com/MMLoveMeMM/articles/3338547.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377795.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377795.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377795.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Pandas(數據分析處理庫)---講解

本內容來自《跟著迪哥學Python數據分析與機器學習實戰》&#xff0c;該篇博客將其內容進行了整理&#xff0c;加上了自己的理解&#xff0c;所做小筆記。若有侵權&#xff0c;聯系立刪。 迪哥說以下的許多函數方法都不用死記硬背&#xff0c;多查API多看文檔&#xff0c;確實&a…

hdu 1141

地址&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid1141 題意&#xff1a;atmel公司1960年發布4bits的處理器&#xff0c;每10年翻一番。給一個年份&#xff0c;問最近一次發布的處理器能運算的n!最大的n是多少。 mark&#xff1a;最大的處理器位數是2160年的4194304…

leetcode 78. 子集 思考分析

題目 給定一組不含重復元素的整數數組 nums&#xff0c;返回該數組所有可能的子集&#xff08;冪集&#xff09;。 說明&#xff1a;解集不能包含重復的子集。 思考分析 畫出解空間樹。 我們可以發現我們所需要的結果是解空間的所有結點。而我們之前組合問題和分割問題都是…

PHP checkdate()函數與示例

PHP checkdate()函數 (PHP checkdate() function) checkdate() function is used to check the valid Gregorian dates. It accepts the date and returns Boolean values (True/False) based on the date values. checkdate()函數用于檢查有效的公歷日期。 它接受日期&#xf…

設計模式讀書筆記-----備忘錄模式

個人比較喜歡玩單機游戲&#xff0c;什么仙劍、古劍、鬼泣、使命召喚、三國無雙等等一系列的游戲我都玩過(現在期待凡人修仙傳)&#xff0c;對于這些游戲除了劇情好、場面大、爽快之外&#xff0c;還可以隨時存檔&#xff0c;等到下次想玩了又可以從剛開始的位置玩起(貌似現在的…

【C++grammar】vector類和字符串字面量

C的vector類 用數組存放數據時&#xff0c;容量大小不可變&#xff0c;vector對象容量可自動增大。 vector的操作&#xff1a; 調用push_back函數時&#xff0c;vector對象的容量可能會增大。 觀察下列操作對vector的影響&#xff1a; #include <vector> #include <…

除去數組中的空字符元素array_filter

<?php$str1_arrayarray(電影,,http://www,,1654,);$str1_arrayarray_filter($str1_array);print_r($str1_array); ?>顯示結果&#xff1a;Array( [0] > 電影 [2] > http://www [4] > 1654) 轉載于:https://www.cnblogs.com/skillCoding/archive/20…

date.after方法_Java Date after()方法與示例

date.after方法日期類after()方法 (Date Class after() method) after() method is available in java.util package. after()方法在java.util包中可用。 after() method is used to check whether this date is after the given date (d) or not. after()方法用于檢查此日期是…

Matplotlib(數據可視化庫)---講解

本內容來自《跟著迪哥學Python數據分析與機器學習實戰》&#xff0c;該篇博客將其內容進行了整理&#xff0c;加上了自己的理解&#xff0c;所做小筆記。若有侵權&#xff0c;聯系立刪。 迪哥說以下的許多函數方法都不用死記硬背&#xff0c;多查API多看文檔&#xff0c;確實&a…

找min和max

看到的貌似是阿里的筆試題&#xff0c;題意是一組數&#xff0c;要找到min和max&#xff0c;同時要求時間復雜度&#xff08;比較次數&#xff09;小于2n&#xff08;2n的辦法都想得到&#xff09;。 別人的思路&#xff1a;n個數的數組里看作每兩個一組&#xff0c;若n是奇數&…

Shader Compiler 界面進展1

先從模仿Composer的界面開始. 目前的進展:不用不知道,雖然wxweidgets有很多界面工具如DialogBlocks(DB), 但仍然不好使. 我使用wxAui界面, DialogBlocks并不支持輸出其xrc格式, 我猜是wx本身就沒有解析wxAui的xrc格式.像wxAuiToolBar或其他wxToolBar, DB工具也不能獨立輸出xrc.…

leetcode 90. 子集 II 思考分析

與本題相關聯的題目解析&#xff1a; leetcode 78. 子集 思考分析 leetcode 40. 組合總和 II思考分析 題目 給定一個可能包含重復元素的整數數組 nums&#xff0c;返回該數組所有可能的子集&#xff08;冪集&#xff09;。 說明&#xff1a;解集不能包含重復的子集。 思考 …

java bitset_Java BitSet and()方法與示例

java bitsetBitSet類和()方法 (BitSet Class and() method) and() method is available in java.util package. and()方法在java.util包中可用。 and() method is used to perform logical AND between two Bitset. This bit set is updated so that every bit holds the value…

Redis-主從復制

一、Redis的Replication&#xff1a; 這里首先需要說明的是&#xff0c;在Redis中配置Master-Slave模式真是太簡單了。相信在閱讀完這篇Blog之后你也可以輕松做到。這里我們還是先列出一些理論性的知識&#xff0c;后面給出實際操作的案例。 下面的列表清楚的解釋了Redis…

.wav音樂文件轉換為.fft.npy頻譜格式文件

需要修改的地方 十個文件夾&#xff0c;每個文件夾下都有100首.au格式的音樂&#xff0c;這里舉個例子&#xff0c;那其中5個類別進行轉換 genre_list ["classical", "jazz", "country", "pop", "rock", "metal"…

WINDOWS編程筆記 2012.2.7

操作系統感知事件和傳遞事件是通過消息機制來實現的typedef struct tagMSG{ HWND hwnd; //窗口的句柄 UINT message; WPARAM wParam; //信息的附加參數 LPARAM lParam; DWORD time; //消息傳遞的時間 POINT pt; //消息投遞的時候&#xff0c;光標的位置}…

php 郵件驗證_PHP程序來驗證電子郵件地址

php 郵件驗證Suppose there is a form floating where every user has to fill his/her email ID. It might happen that due to typing error or any other problem user doesnt fill his/her mail ID correctly. Then at that point, the program should be such that it sho…

【C++grammar】結構化綁定

目錄定義1、用于原生數組的結構化綁定聲明2、用于std::array的結構化綁定聲明3、用于對象數據成員的結構化綁定聲明定義 結構化綁定聲明是一個聲明語句&#xff0c;意味著聲明了一些標識符并對標識符做了初始化。將指定的一些名字綁定到初始化器的子對象或者元素上。 對于初始…

URAL 1106 Two Teams (DFS)

題意 小組里有N個人&#xff0c;每個人都有一個或多個朋友在小組里。將小組分成兩個隊伍&#xff0c;每個隊伍的任意一個成員都有至少一個朋友在另一個隊伍。 思路 一開始覺得和前幾天做過的一道2-sat&#xff08;每個隊伍任意兩個成員都必須互相認識&#xff09;相似然后就往那…

七、邏輯回歸項目實戰---音樂分類器

一、項目需求 訓練集數據為六類音樂([“classical”, “jazz”, “country”, “pop”, “rock”, “metal”])&#xff0c;格式為.wav&#xff0c;每類音樂都有100首 音樂分類器項目&#xff0c;主要運用到了傅里葉變換函數 很多東西越在高維空間處理起來就會變得越是簡單 例…