PreferenceActivity 显示/编辑域对象的值
给定此代码,如何从复选框和文本首选项获取值并将它们存储在域对象中?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然。只需添加代码将值存储到数据库中,并且不要忘记在您的首选项 xml 中提及
android:persistent=false
属性。此外,您可以使用 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.Further you can do many things with
soundcb
object: read the value, set onClickListeners and so on.