Android - 使用 PreferenceScreen 显示 ContentProvider 以及将设置保存到 ContentProvider

发布于 2024-08-26 12:29:34 字数 2088 浏览 10 评论 0原文

我有自己的自定义内容提供程序,它加载一个数据库,其中包含我的应用程序的设置信息。

我在创建“设置”活动时从 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 技术交流群。

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

发布评论

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

评论(1

溺孤伤于心 2024-09-02 12:29:34

文档中查找可用方法。
您可以订阅onDialogClosed来获取“确定”或“取消”点击事件。

另请查看 getEditText 方法,该方法将返回对话框的编辑文本,您可以在那里设置值。或者甚至查看 setText,您也许可以用它来设置值。这后来未经我测试。

编辑 1

抱歉,您无法订阅。您必须子类化 EditTextPreference 并重写该方法。一个复杂的示例是 这里但你不需要你对此感兴趣的所有内容:

@Override
//When the dialog is closed, perform the relevant actions
protected void onDialogClosed(boolean positiveResult) {

    if (positiveResult) {
     String text=getEditText().getText().toString();
     }
    else {
     // cancel hit
    }
}

编辑2

你必须放弃

etl = new EditTextListener(this);

你必须实现你的新类

EditTextPreference editTextPref = new EditTextListener(this);

,这样代码就会变成

// Edit text preference
EditTextPreference editTextPref = new EditTextListener(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);

Lookup the available methods in the Documentation.
You can subscribe to onDialogClosed 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 into setText, 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:

@Override
//When the dialog is closed, perform the relevant actions
protected void onDialogClosed(boolean positiveResult) {

    if (positiveResult) {
     String text=getEditText().getText().toString();
     }
    else {
     // cancel hit
    }
}

EDIT 2

You have to drop

etl = new EditTextListener(this);

You have to implement your new class

EditTextPreference editTextPref = new EditTextListener(this);

so the code will become

// Edit text preference
EditTextPreference editTextPref = new EditTextListener(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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文