使用开关控制 activity 中的选项并显示摘要是否 on/off

controlling options in an activity using a switch and displaying summary whether on/off

我想在我的首选项中添加一个选项 activity 类似于 chrome "home page",“保存密码”选项(android 应用程序)。根据我的理解,这会启动一个 activity ,其中包含一个控制子选项的开关。这进一步给出了一个摘要,指示是否 on/off。我的问题是我如何着手实施一些关于偏好 activity 的类似内容,因为我找不到实现此目的的示例。 有什么指点吗?

PreferenceActivity (http://developer.android.com/reference/android/preference/PreferenceActivity.html) 的文档有一个很好的用法示例。 您还有其他要找的东西吗?

使用偏好:

A Preference object is the building block for a single setting. Each Preference appears as an item in a list and provides the appropriate UI for users to modify the setting. For example, a CheckBoxPreference creates a list item that shows a checkbox, and a ListPreference creates an item that opens a dialog with a list of choices.

Each Preference you add has a corresponding key-value pair that the system uses to save the setting in a default SharedPreferences file for your app's settings. When the user changes a setting, the system updates the corresponding value in the SharedPreferences file for you. The only time you should directly interact with the associated SharedPreferences file is when you need to read the value in order to determine your app's behavior based on the user's setting.

The value saved in SharedPreferences for each setting can be one of the following data types:

  • Boolean
  • Float
  • Int
  • Long
  • String
  • String Set

Because your app's settings UI is built using Preference objects instead of View objects, you need to use a specialized Activity or Fragment subclass to display the list settings:

  • If your app supports versions of Android older than 3.0 (API level 10 and lower), you must build the activity as an extension of the PreferenceActivity class.
  • On Android 3.0 and later, you should instead use a traditional Activity that hosts a PreferenceFragment that displays your app settings. However, you can also use PreferenceActivity to create a two-pane layout for large screens when you have multiple groups of settings.

带有您想要的子视图的 XML 示例:

<PreferenceScreen  xmlns:android="http://schemas.android.com/apk/res/android">
<!-- opens a subscreen of settings -->
<PreferenceScreen
    android:key="button_voicemail_category_key"
    android:title="@string/voicemail"
    android:persistent="false">
    <ListPreference
        android:key="button_voicemail_provider_key"
        android:title="@string/voicemail_provider" ... />
    <!-- opens another nested subscreen -->
    <PreferenceScreen
        android:key="button_voicemail_setting_key"
        android:title="@string/voicemail_settings"
        android:persistent="false">
        ...
    </PreferenceScreen>
    <RingtonePreference
        android:key="button_voicemail_ringtone_key"
        android:title="@string/voicemail_ringtone_title"
        android:ringtoneType="notification" ... />
    ...
</PreferenceScreen>
...
</PreferenceScreen>

您可以在 this link

中找到所有相关信息