Xamarin android PreferenceActivity 实现应用程序首选项设置(一)

应用程序首选项屏幕 类似系统设置界面。

PreferenceActivity 是另一种类型的Activity,通过PreferenceActivity 可以以最少量的工作显示某些Preference列表项。大多数Preference列表项都允许定义一个可访问值得键,以及定义向用户显示的标题和描述信息。

  • CheckBoxPreference 简单的打开、关闭复选框控件
  • EditTextPreference 在对话框中显示可编辑的文本框
  • ListPreference 提供可以从列表中选择的选择项。可为其设置默认值
  • PreferenceCategoru 列表想的标题,Preference对象的分组
  • PreferenceScreen 占位符,可以导航到另一个首选项列表。单击时打开带有分组首选项的新首选项屏幕
  • RingtonePreference 提供用户选择的铃声列表
  • DialogPreference 是一个基类,通过继承该类,可以在首选项对话框中显示自己的UI

布局文件代码

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory
        android:title="First Category">
        <CheckBoxPreference
            android:key="chooseFromList"
            android:title="checkbox preference"
            android:defaultValue="true"
            android:summary="do you want choose from the list" />
        <ListPreference
            android:key="listChoice"
            android:title="list preference"
            android:summary="allows you to select an array item"
            android:dependency="chooseFromList"
            android:defaultValue="1"
            android:entries="@array/listChoiceEntries"
            android:entryValues="@array/listChoiceEntryValue" />
    </PreferenceCategory>
    <PreferenceCategory
        android:title="Second Category">
        <PreferenceScreen
            android:title="advanced options">
            <CheckBoxPreference
                android:key="advancedOption"
                android:title="advanced options"
                android:defaultValue="true"
                android:summary="this is an advanced option" />
        </PreferenceScreen>
        <EditTextPreference
            android:dialogTitle="EditTextTitle"
            android:key="mainOption"
            android:title="some title"
            android:summary="this is an editText preference"
            android:defaultValue="test" />
        <RingtonePreference
            android:key="ringtone"
            android:title="设置铃声"
            android:showSilent="true"
            android:ringtoneType="alarm"
            android:summary="set ringtone" />
        <命名空间.CustomDialogPreference
            android:key="customDialogPreference"
            android:dialogIcon="@drawable/icon"
            android:title="custom dialog"
            android:summary="Reset all quest-progress."
            android:dialogMessage="Are you sure you wish to reset your quest progress? This action cannot be undone!"
            android:positiveButtonText="Clear Quests"
            android:negativeButtonText="Cancel" />
    </PreferenceCategory>
</PreferenceScreen>

其中CustomDialogPreference为继承自DialogPreference自定义的类,没有什么实际的代码

class CustomDialogPreference:DialogPreference 
    {
        public CustomDialogPreference(Context context, IAttributeSet attrs)
            :base(context,attrs)
        {

        }
    }

Activity的OnCreate方法中初始化代码

this.AddPreferencesFromResource (Resource.Layout.Main);

            //var listPref = this.FindPreference ("listChoice") as ListPreference;
            //动态设置ListPreference显示项和值
            //listPref.SetEntries (new string[]{ "动态加载1","动态加载2","动态加载3"});
            //listPref.SetEntryValues (new string[]{"1","2","3"});
原文地址:https://www.cnblogs.com/my-tzc/p/3764504.html