PreferenceActivity 显示/编辑域对象的值

发布于 2024-10-16 02:07:01 字数 1836 浏览 5 评论 0原文

给定此代码,如何从复选框和文本首选项获取值并将它们存储在域对象中?

public class MonitorPreferences extends PreferenceActivity {
    private PersistenceManager pm;
    private Monitor monitor;
    private boolean mActive;
    private String mName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pm = new PersistenceManager(getApplicationContext());
        addPreferencesFromResource(R.xml.monitors_pref);

        fetchDomainObject();
    }

    private void fetchDomainObject() {  
        monitor =  pm.fetchMonitor(getIntent().getLongExtra(SuperListActivity.EXTRA_KEY_MONITOR_ID, -1));
    }

    private void persistDomainObject(Monitor monitor) {
        pm.persist(monitor);
    }
}

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory android:title="General">
        <CheckBoxPreference
            android:key="active_chkbox"
            android:title="Active"
            android:defaultValue="true"
            android:persistent="false"/>
        <EditTextPreference
            android:key="name_txt"
            android:dependency="active_chkbox"
            android:title="Name"
            android:summary="Enter a name"
            android:dialogTitle="Enter a name"
            android:dialogMessage="Enter a name"
            android:defaultValue="John Doe"
            android:persistent="false"/>
    </PreferenceCategory>
</PreferenceScreen>

原始问题:创建一个具有 PreferenceActivity 外观和感觉的普通 Activity 我的目标是从 Activity 编辑域对象的变量,并具有普通 android 首选项的外观和感觉。实现这一目标的最简单方法是什么?

是否可以创建一个 PreferenceActivity 并以某种方式修改它以显示/编辑域对象的值而不是 SharedPreferences 中的值?

Given this code, how do I get the values from the checkbox- and textpreference and store them in the domain object?

public class MonitorPreferences extends PreferenceActivity {
    private PersistenceManager pm;
    private Monitor monitor;
    private boolean mActive;
    private String mName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pm = new PersistenceManager(getApplicationContext());
        addPreferencesFromResource(R.xml.monitors_pref);

        fetchDomainObject();
    }

    private void fetchDomainObject() {  
        monitor =  pm.fetchMonitor(getIntent().getLongExtra(SuperListActivity.EXTRA_KEY_MONITOR_ID, -1));
    }

    private void persistDomainObject(Monitor monitor) {
        pm.persist(monitor);
    }
}

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory android:title="General">
        <CheckBoxPreference
            android:key="active_chkbox"
            android:title="Active"
            android:defaultValue="true"
            android:persistent="false"/>
        <EditTextPreference
            android:key="name_txt"
            android:dependency="active_chkbox"
            android:title="Name"
            android:summary="Enter a name"
            android:dialogTitle="Enter a name"
            android:dialogMessage="Enter a name"
            android:defaultValue="John Doe"
            android:persistent="false"/>
    </PreferenceCategory>
</PreferenceScreen>

Original question: Creating a normal Activity with the look and feel of a PreferenceActivity
My goal is to edit the variables of a domain object from an Activity with the look and feel of stock android preferences. What's the easiest way of accomplishing this?

Would it be possible to create a a PreferenceActivity and somehow modify it to display/edit the values of a domain object instead of the values from SharedPreferences?

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

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

发布评论

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

评论(1

吃素的狼 2024-10-23 02:07:01

当然。只需添加代码将值存储到数据库中,并且不要忘记在您的首选项 xml 中提及 android:persistent=false 属性。

final CheckBoxPreference soundcb = (CheckBoxPreference) findPreference("active_chkbox");

此外,您可以使用 soundcb 对象执行许多操作:读取值、设置 onClickListeners 等。

Of course. Just add the code to store the values into the database and don't forget to mension android:persistent=false attribute in your preferences xml.

final CheckBoxPreference soundcb = (CheckBoxPreference) findPreference("active_chkbox");

Further you can do many things with soundcb object: read the value, set onClickListeners and so on.

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