如何创建使用返回结果的现有活动的自定义首选项?

发布于 2024-10-06 04:03:38 字数 282 浏览 0 评论 0原文

Preference 类允许设置 Intent,以便在单击时激活另一个活动,但我无法找到使用此方法处理活动结果的方法。还有 DialogPreference,我可以在其中提供自定义视图,但我无法直接访问我想要使用的视图,只能访问一个活动。

进一步挖掘,看起来 RingtonePreference 使用 PreferenceManager 上的一些包内部方法来接收已启动的子活动的结果,但由于这些是包内部的,我无法执行相同的操作。

是否有其他方法可以通过返回结果的活动来处理自定义首选项(其中结果将保存为首选项的值)?

The Preference class allows for an Intent to be set, to have a preference activate another activity when clicked, but I'm unable to find a way to handle the result from an activity using this method. There's also the DialogPreference where I can provide a custom view, but I don't have direct access to the view I want to use, only an activity.

Digging a bit further, it looks like the RingtonePreference uses a few package internal methods on PreferenceManager to receive results from a started sub-activity, but as these are package internal I am unable to do the same.

Is there any other way of handling a custom preference with an activity that returns a result (where the result is to be saved as the value of the preference)?

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

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

发布评论

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

评论(1

云醉月微眠 2024-10-13 04:03:38

我还注意到 PreferenceActivity 不会返回 onActivityResult。话虽如此,您的子活动是否有原因无法直接保存首选项?如果您需要检查它的值,您可以在 PreferenceActivity 的 onResume 上检查它作为解决方法。

//SubActivity onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button btn = (Button) findViewById(R.id.Button01);
    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            SharedPreferences prefs = getSharedPreferences(TestPreferenceActivity.PREFS_FILE, MODE_WORLD_READABLE);
            prefs.edit().putString("mykey", "someValue").commit();
            finish();
        }});
}

//PreferenceActivity onResume
@Override
protected void onResume() {
    Log.d(TAG, "Preferences Resumed");
    //Check for new Preference Values
    SharedPreferences prefs = getSharedPreferences(PREFS_FILE, MODE_WORLD_READABLE);
    String value = prefs.getString("mykey", "defValue");
    Log.d(TAG, "Current value is: " + value);
    super.onResume();
}

I have also noticed PreferenceActivity does not return onActivityResult. That being said, is there a reason your SubActivity couldn't save the preference directly? If you need to check the value of it, you could check it at onResume of your PreferenceActivity as a workaround..

//SubActivity onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button btn = (Button) findViewById(R.id.Button01);
    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            SharedPreferences prefs = getSharedPreferences(TestPreferenceActivity.PREFS_FILE, MODE_WORLD_READABLE);
            prefs.edit().putString("mykey", "someValue").commit();
            finish();
        }});
}

//PreferenceActivity onResume
@Override
protected void onResume() {
    Log.d(TAG, "Preferences Resumed");
    //Check for new Preference Values
    SharedPreferences prefs = getSharedPreferences(PREFS_FILE, MODE_WORLD_READABLE);
    String value = prefs.getString("mykey", "defValue");
    Log.d(TAG, "Current value is: " + value);
    super.onResume();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文