了解 PreferenceActivity

发布于 2024-12-26 02:49:12 字数 3055 浏览 1 评论 0原文

public class SettingsActivity extends PreferenceActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    /* Some initializations */
    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);

    ListView listView = new ListView(this);
    listView.setId(android.R.id.list);
    listView.setLayoutParams(new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.FILL_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT, 1));
    layout.addView(listView);

    this.setContentView(layout);
    /* Preferences time! (we build the preferences) */
    Preference version = getPreference("My School Manager", "Version 2.0",
            null);
    Preference author = getPreference("Author", "Simone Casagranda", null);
    Preference marketLink = getPreference("Android market",
            "View all my apps :)",
            new Intent(Intent.ACTION_VIEW, Uri
                    .parse("http://market.android.com/details?id="
                            + "it.trento.alchemiasoft.casagranda.simone")));

    CheckBoxPreference check = new CheckBoxPreference(this);
    check.setTitle("Checkbox");
    check.setSummary("Example of checkbox");

    DialogPreference license = new MyDialogPreference(this, "License",
            "This is the license for...bla bla");

    /* Now we add the preferences to the preference screen */
    PreferenceScreen preferenceScreen = this.getPreferenceManager()
            .createPreferenceScreen(this);
    addPreferenceCategory(preferenceScreen, "Preferences Tutorial",
            version, author, marketLink, check, license);
    this.setPreferenceScreen(preferenceScreen);
}

private boolean addPreferenceCategory(PreferenceScreen preferenceScreen,
        String titleCategory, Preference... preferences) {
    boolean addPreference = false;
    for (Preference preference : preferences) {
        if (preference != null)
            addPreference = true;
    }
    if (addPreference) {
        PreferenceCategory preferenceCategory = new PreferenceCategory(this);
        preferenceCategory.setTitle(titleCategory);
        preferenceScreen.addPreference(preferenceCategory);
        for (Preference preference : preferences) {
            if (preference != null)
                preferenceCategory.addPreference(preference);
        }
        return true;
    } else
        return false;
}

private Preference getPreference(String title, String summary, Intent intent) {
    Preference pref = new Preference(this);
    pref.setTitle(title);
    pref.setSummary(summary);
    if (intent != null)
        pref.setIntent(intent);
    return pref;
}

public class MyDialogPreference extends DialogPreference {
    public MyDialogPreference(Context context, String title, String text) {
        super(context, null);
        this.setTitle(title);
        this.setDialogMessage(text);
    }
}

在这段代码

中,首选项列在列表视图中。不需要适配器吗?我没有看到数据被输入到列表视图中。这个首选项屏幕怎么样?是否需要进行偏好设置?

public class SettingsActivity extends PreferenceActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    /* Some initializations */
    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);

    ListView listView = new ListView(this);
    listView.setId(android.R.id.list);
    listView.setLayoutParams(new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.FILL_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT, 1));
    layout.addView(listView);

    this.setContentView(layout);
    /* Preferences time! (we build the preferences) */
    Preference version = getPreference("My School Manager", "Version 2.0",
            null);
    Preference author = getPreference("Author", "Simone Casagranda", null);
    Preference marketLink = getPreference("Android market",
            "View all my apps :)",
            new Intent(Intent.ACTION_VIEW, Uri
                    .parse("http://market.android.com/details?id="
                            + "it.trento.alchemiasoft.casagranda.simone")));

    CheckBoxPreference check = new CheckBoxPreference(this);
    check.setTitle("Checkbox");
    check.setSummary("Example of checkbox");

    DialogPreference license = new MyDialogPreference(this, "License",
            "This is the license for...bla bla");

    /* Now we add the preferences to the preference screen */
    PreferenceScreen preferenceScreen = this.getPreferenceManager()
            .createPreferenceScreen(this);
    addPreferenceCategory(preferenceScreen, "Preferences Tutorial",
            version, author, marketLink, check, license);
    this.setPreferenceScreen(preferenceScreen);
}

private boolean addPreferenceCategory(PreferenceScreen preferenceScreen,
        String titleCategory, Preference... preferences) {
    boolean addPreference = false;
    for (Preference preference : preferences) {
        if (preference != null)
            addPreference = true;
    }
    if (addPreference) {
        PreferenceCategory preferenceCategory = new PreferenceCategory(this);
        preferenceCategory.setTitle(titleCategory);
        preferenceScreen.addPreference(preferenceCategory);
        for (Preference preference : preferences) {
            if (preference != null)
                preferenceCategory.addPreference(preference);
        }
        return true;
    } else
        return false;
}

private Preference getPreference(String title, String summary, Intent intent) {
    Preference pref = new Preference(this);
    pref.setTitle(title);
    pref.setSummary(summary);
    if (intent != null)
        pref.setIntent(intent);
    return pref;
}

public class MyDialogPreference extends DialogPreference {
    public MyDialogPreference(Context context, String title, String text) {
        super(context, null);
        this.setTitle(title);
        this.setDialogMessage(text);
    }
}

}

In this piece of code, the preference are listed in the listview. Doesn't it needs a adapter? I don't see the data is being fed into the listview. What about this Preference screen? Is it necessary for preference setting?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

披肩女神 2025-01-02 02:49:12

PreferenceActivity 自行处理首选项。该适配器内置于 PreferenceActivity 中,因此您不必担心它。此外,您还可以使用 XML 来执行相同的操作,而不是以编程方式创建首选项。查看本教程

偏好活动可以消除很多痛苦。想象一下编写代码来连接所有 UI 操作,例如显示对话框、更改首选项的状态并保存它们。偏好活动可以消除你的痛苦。

The PreferenceActivity handles the preferences all by itself. The adapter is built into the PreferenceActivity so that you don't have to bother about it. Also instead of creating your preferences programmatically you can use a XML to do the same. Check this tutorial.

The preference activity takes out a lot of pain. Imagine writing code for wiring all the UI actions like showing dialog, changing the states of your preferences and to save them. Preference activity takes that pain away from you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文