【安卓筆記】快速開發設定介面-----PreferenceActivity,安卓介面

來源:互聯網
上載者:User

【安卓筆記】快速開發設定介面-----PreferenceActivity,安卓介面

通常app都會有一個設定介面,如下:


通常做法是自己定義布局,然後在代碼裡面添加響應函數,並將結果儲存到Sharedpreferences中。android給我們提供了PreferenceActivity來簡化開發設定介面。你只需這樣做: 1.建立一個類繼承PreferenceActivity,並匯入設定介面布局:

package com.example.preferenceactivitydemo1;import android.os.Bundle;import android.preference.PreferenceActivity;public class MainActivity extends PreferenceActivity{@SuppressWarnings("deprecation")@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);addPreferencesFromResource(R.xml.main_preference);}}

2.在res目錄下添加xml檔案夾,並建立一個名為main_preference(即代碼中引用的資源檔)的檔案. 3.編寫資源檔。設定介面對應的資源檔如下:
<?xml version="1.0" encoding="utf-8"?><PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"    android:title="標題" >    <PreferenceCategory        android:summary="分組1"        android:title="分組1標題" >        <ListPreference            android:defaultValue="@string/defaultvalues"            android:dialogTitle="對話方塊標題"            android:entries="@array/entries"            android:entryValues="@array/entriesvalue"            android:key="myListPreference"            android:summary="標題說明"            android:title="小標題" />        <CheckBoxPreference            android:key="doubi"            android:summaryOff="逗比模式關閉"            android:summaryOn="逗比模式開啟"            android:title="逗比模式" >        </CheckBoxPreference>    </PreferenceCategory>    <PreferenceCategory        android:summary="分組2"        android:title="分組2標題" >        <SwitchPreference            android:key="fly"            android:summaryOff="已關閉飛航模式"            android:summaryOn="已開啟飛航模式"            android:switchTextOff="關閉"            android:switchTextOn="開啟"            android:title="飛航模式" >        </SwitchPreference>        <EditTextPreference            android:defaultValue="zhangsan"            android:dialogTitle="編輯姓名"            android:key="name"            android:summary="編輯您的姓名"            android:title="編輯" >        </EditTextPreference>    </PreferenceCategory></PreferenceScreen>

其中引用了string.xml的相關內容:


<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">PreferenceActivityDemo1</string>    <string name="action_settings">Settings</string>    <string name="hello_world">Hello world!</string><string-array name="entries">    <item>小蘋果</item>    <item>小香蕉</item>    <item>小西瓜</item></string-array><string-array name="entriesvalue">    <item>apple</item>    <item>banana</item>    <item>watermelon</item></string-array><string name="defaultvalues">apple</string></resources>

恩,就這麼簡單~
所有結果會儲存到data/data/包名/shared_prefs目錄下的包名_preferences.xml中,跟sharepreferences一樣。




怎用preference寫出如的介面,怎在介面中設定android preference 控制項的長寬

這個應該就是一個簡單的Activity吧,PreferenceActivity不能做出這個效果吧。PreferenceActivity裡面就是一個ListView,你可以加header和footer,但這種效果,可能即使一個普通的activity。
 
安卓手機應用開發,怎實現設定頁面頂部添加導覽列,以及自訂風格

做包含檔案吧
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.