Android喜好設定架構ListPreference

來源:互聯網
上載者:User

MainActivity.java檔案:

package com.chen.listpreference;import android.os.Bundle;import android.preference.ListPreference;import android.preference.PreferenceActivity;import android.preference.PreferenceManager;import android.util.Log;public class MainActivity extends PreferenceActivity{@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);addPreferencesFromResource(R.xml.flightpreference);PreferenceManager manager = getPreferenceManager();ListPreference listPreference = (ListPreference)manager.findPreference("selected_flight_sort_option");Log.i("儲存的值為", listPreference.getValue());}}

/res/xml/flightpreference.xml檔案

<?xml version="1.0" encoding="UTF-8"?><PreferenceScreen        xmlns:android="http://schemas.android.com/apk/res/android"        android:key="flight_option_preference"        android:title="@string/prefTitle"            android:summary="@string/prefSummary">                <ListPreference            android:key="@string/selected_flight_sort_option"            android:title="@string/listTitle"            android:summary="@string/listSummary"            android:entries="@array/flight_sort_options"            android:entryValues="@array/flight_sort_options_values"            android:dialogTitle="@string/dialogTitle"            android:defaultValue="@string/flight_sort_option_default_value"/>            </PreferenceScreen>   

/res/values/strings.xml檔案

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">ListPreference</string>    <string name="action_settings">Settings</string>    <string name="hello_world">Hello world!</string>    <string name="prefTitle">prefTitle</string><string name="prefSummary">prefSummary</string><string name="selected_flight_sort_option">selected_flight_sort_option</string><string name="listTitle">listTitle</string><string name="listSummary">listSummary</string><string name="dialogTitle">dialogTitle</string><string name="flight_sort_option_default_value">flight_sort_option_default_value</string><string-array name="flight_sort_options_values">flight_sort_options_values</string-array><string-array name="flight_sort_options">flight_sort_options</string-array></resources>

/res/values/array.xml檔案

<?xml version="1.0" encoding="utf-8"?><resources><string-array name="flight_sort_options">    <item>First</item>    <item>Second</item>    <item>Three</item></string-array><string-array name="flight_sort_options_values">    <item>0</item>    <item>1</item>    <item>2</item></string-array>    </resources>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.