Android - 使用 PreferenceScreen 显示 ContentProvider 以及将设置保存到 ContentProvider
我有自己的自定义内容提供程序,它加载一个数据库,其中包含我的应用程序的设置信息。
我在创建“设置”活动时从 ContentProvider 加载设置。
我的设置活动由 PreferenceScreen 和基于 EditText 的对话框组成。
以下代码显示了我如何使用首选项屏幕和编辑文本。
正如您从第一张图片中看到的那样,它可以工作并显示带有下面信息的菜单。
问题出在图二中,当我单击菜单中的选项时,会弹出对话框,但它是空的,我希望能够将内容提供程序中的数据加载到对话框中的编辑文本中,因此在图像中第一个显示“Donal”作为用户名,因此在图像中第二个“Donal”也应该出现在对话框的编辑文本中。
我还希望能够收听对话框中的“确定”按钮,以便当用户更改设置时我可以更新内容提供程序中的数据。
谁能帮我做我想做的事吗?
代码
public class PreferencesApp extends PreferenceActivity {
String name;
EditTextListener etl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loadSettingsFromProvider();
etl = new EditTextListener(this);
setPreferenceScreen(createPreferenceHierarchy());
}
private PreferenceScreen createPreferenceHierarchy() {
// Root
PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);
// Dialog based preferences
PreferenceCategory dialogBasedPrefCat = new PreferenceCategory(this);
dialogBasedPrefCat.setTitle(R.string.dialog_based_preferences);
root.addPreference(dialogBasedPrefCat);
// Edit text preference
EditTextPreference editTextPref = new EditTextPreference(this);
editTextPref.setDialogTitle(R.string.dialog_title_edittext_preference);
editTextPref.setKey("edittext_preference");
editTextPref.setTitle(R.string.title_edittext_preference);
editTextPref.setSummary(name);
dialogBasedPrefCat.addPreference(editTextPref);
return root;
}
public class EditTextListener extends EditTextPreference{
public EditTextListener(Context context){
super(context);
}
@Override
//When the dialog is closed, perform the relevant actions
protected void onDialogClosed(boolean positiveResult) {
if (positiveResult) {
String text=getEditText().getText().toString();
Log.d("DIALOG CLOSED", "OK");
}
else {
// cancel hit
Log.d("DIALOG CLOSED", "CANCEL");
}
}
}
I have my own custom Content Provider that loads a database which contains the settings information for my application.
I load the settings from the ContentProvider on the creation of my Settings activity.
My Settings activity is made up of a PreferenceScreen and Dialog based EditText's.
The following code shows how I use the preference screen and edit texts.
So as you can see from the first image this works and displays the menu with the information underneath.
The problem is in image two, when I click on a choice in the menu the dialog pops up but it is empty, I would like to be able to load the data from my content provider into the edit text in the dialog, so in image one it shows "Donal" as the user name so in image two "Donal" should also appear in the edit text in the dialog.
I would also like to be able to listen to the OK button in the dialog so when a user changes a setting I can update the data in my content provider.
Can anyone help me with what I'm trying to do?
Code
public class PreferencesApp extends PreferenceActivity {
String name;
EditTextListener etl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loadSettingsFromProvider();
etl = new EditTextListener(this);
setPreferenceScreen(createPreferenceHierarchy());
}
private PreferenceScreen createPreferenceHierarchy() {
// Root
PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);
// Dialog based preferences
PreferenceCategory dialogBasedPrefCat = new PreferenceCategory(this);
dialogBasedPrefCat.setTitle(R.string.dialog_based_preferences);
root.addPreference(dialogBasedPrefCat);
// Edit text preference
EditTextPreference editTextPref = new EditTextPreference(this);
editTextPref.setDialogTitle(R.string.dialog_title_edittext_preference);
editTextPref.setKey("edittext_preference");
editTextPref.setTitle(R.string.title_edittext_preference);
editTextPref.setSummary(name);
dialogBasedPrefCat.addPreference(editTextPref);
return root;
}
public class EditTextListener extends EditTextPreference{
public EditTextListener(Context context){
super(context);
}
@Override
//When the dialog is closed, perform the relevant actions
protected void onDialogClosed(boolean positiveResult) {
if (positiveResult) {
String text=getEditText().getText().toString();
Log.d("DIALOG CLOSED", "OK");
}
else {
// cancel hit
Log.d("DIALOG CLOSED", "CANCEL");
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在文档中查找可用方法。
您可以订阅onDialogClosed
来获取“确定”或“取消”点击事件。另请查看
getEditText
方法,该方法将返回对话框的编辑文本,您可以在那里设置值。或者甚至查看setText
,您也许可以用它来设置值。这后来未经我测试。编辑 1
抱歉,您无法订阅。您必须子类化
EditTextPreference
并重写该方法。一个复杂的示例是 这里但你不需要你对此感兴趣的所有内容:编辑2
你必须放弃
你必须实现你的新类
,这样代码就会变成
Lookup the available methods in the Documentation.
You can subscribe toonDialogClosed
to get the OK or Cancel click event.Also look into
getEditText
method that will return the edittext of the Dialog, and you can set the value there. Or even look intosetText
, you maybe can set the value with it. This later untested by me.EDIT 1
Sorry, you can't subscribe. You have to subclass the
EditTextPreference
and override the method. A complex example is here but you don't need all the stuff You are interested in this:EDIT 2
You have to drop
You have to implement your new class
so the code will become