从嵌套的 Android PreferenceScreen 返回到上一个 PreferenceScreen

发布于 2024-10-17 03:46:28 字数 227 浏览 3 评论 0原文

我正在以编程方式创建一组嵌套的 PreferenceScreen。当用户潜入其中一个屏幕并点击其中的某些内容时,我想返回到上一个 PreferenceScreen(即“父级”)。

我找不到任何相关信息,并且调用 finish() 并不能完成这项工作,而是从所有 PreferenceScreen 返回,而不是仅返回一个。

I am programmatically creating a nested set of PreferenceScreens. When the user dives into one of these screens, and taps something in there, I want to go back to the previous PreferenceScreen, (i.e. the "parent").

I couldn't find any information on this, and calling finish() doesn't do the job but rather returns from ALL the PreferenceScreens instead of just one.

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

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

发布评论

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

评论(3

卷耳 2024-10-24 03:46:28

我刚刚遇到问题并找到了解决方案。
您可以重写 PreferenceActivity 中的 onPreferenceTreeClick 方法。
调用参数preferenceScreen的getDialog方法,然后调用对话框的dismiss方法。
就这样,快乐!

I just meet the problem and just found the solution.
You can override the method onPreferenceTreeClick in PreferenceActivity.
call the param preferenceScreen's getDialog method, then call the dialog's dismiss method.
just like this, joy!

黒涩兲箜 2024-10-24 03:46:28

您可以将第一个打开,而不是在启动第二个的那个上调用 finish() 。如果这不起作用,您可以保存第一个屏幕的状态,而不是“返回”,而是通过 Intent 调用它并让它加载之前的状态。

You can either leave the first one open buy not calling finish() on the one that launched the second one. If this does not do it, you could save the state of the first screen and instead of 'going back' you call it by an Intent and have it load its previous state.

段念尘 2024-10-24 03:46:28

尝试类似的操作:

package com.justinbuser.preferences;

import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.os.Bundle;

public class DynamicSettingsActivity extends PreferenceActivity{

    public DynamicSettingsActivity(){
        super();
    }

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        PreferenceScreen defaultRoot = getPreferenceManager().createPreferenceScreen(this);
        defaultRoot.setKey("root");

        PreferenceScreen videoScreen = getPreferenceManager().createPreferenceScreen(this);
        videoScreen.setTitle(R.string.video_chooser_title);
        videoScreen.setSummary(R.string.video_chooser_summary);
        videoScreen.setKey(getString(R.string.video));
        //The following MUST be called before creating videoPref
        defaultRoot.addPreference(videoScreen);

        videoPref = new VideoChooserPreferenceCategory(mContext, videoScreen);
        videoPref.setTitle(R.string.video_chooser_dialog_title);
        videoPref.setSummary(R.string.video_chooser_dialog_summary);
        videoScreen.addPreference(videoPref);

        setPreferenceScreen(defaultRoot);
    }


}

使用自定义首选项类别,例如:

package com.justinbuser.preferences;

import android.content.Context;
import android.preference.Preference;
import android.preference.PreferenceCategory;
import android.preference.PreferenceScreen;
import android.preference.Preference.OnPreferenceClickListener;

public class VideoChooserPreferenceCategory extends PreferenceCategory implements OnPreferenceClickListener {

    private PreferenceScreen parentScreen;
    private final Context mContext;

    public VideoChooserPreferenceCategory(Context context) {
        super(context);
        mContext = context;
    }

    public VideoChooserPreferenceCategory(Context context, PreferenceScreen preferenceScreen) {
        super(context);
        parentScreen = preferenceScreen;
        mContext = context;
    }

    @Override
    protected void onAttachedToActivity() {

        if(this.getPreferenceCount() > 0)
        {
            this.removeAll();
        }
        Preference videoPref = new Preference(mContext);
        videoPref.setKey(videoIds[videoId]);
        videoPref.setTitle("Video Title");
        videoPref.setSummary("Video Description");
        videoPref.setOnPreferenceClickListener(this);
        addPreference(videoPref);
        super.onAttachedToActivity();
    }

    public boolean onPreferenceClick(Preference preference) {
        parentScreen.getDialog().dismiss();
        this.callChangeListener(preference.getKey());
        return true;
    }

}

Try something like:

package com.justinbuser.preferences;

import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.os.Bundle;

public class DynamicSettingsActivity extends PreferenceActivity{

    public DynamicSettingsActivity(){
        super();
    }

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        PreferenceScreen defaultRoot = getPreferenceManager().createPreferenceScreen(this);
        defaultRoot.setKey("root");

        PreferenceScreen videoScreen = getPreferenceManager().createPreferenceScreen(this);
        videoScreen.setTitle(R.string.video_chooser_title);
        videoScreen.setSummary(R.string.video_chooser_summary);
        videoScreen.setKey(getString(R.string.video));
        //The following MUST be called before creating videoPref
        defaultRoot.addPreference(videoScreen);

        videoPref = new VideoChooserPreferenceCategory(mContext, videoScreen);
        videoPref.setTitle(R.string.video_chooser_dialog_title);
        videoPref.setSummary(R.string.video_chooser_dialog_summary);
        videoScreen.addPreference(videoPref);

        setPreferenceScreen(defaultRoot);
    }


}

With a custom preference category like:

package com.justinbuser.preferences;

import android.content.Context;
import android.preference.Preference;
import android.preference.PreferenceCategory;
import android.preference.PreferenceScreen;
import android.preference.Preference.OnPreferenceClickListener;

public class VideoChooserPreferenceCategory extends PreferenceCategory implements OnPreferenceClickListener {

    private PreferenceScreen parentScreen;
    private final Context mContext;

    public VideoChooserPreferenceCategory(Context context) {
        super(context);
        mContext = context;
    }

    public VideoChooserPreferenceCategory(Context context, PreferenceScreen preferenceScreen) {
        super(context);
        parentScreen = preferenceScreen;
        mContext = context;
    }

    @Override
    protected void onAttachedToActivity() {

        if(this.getPreferenceCount() > 0)
        {
            this.removeAll();
        }
        Preference videoPref = new Preference(mContext);
        videoPref.setKey(videoIds[videoId]);
        videoPref.setTitle("Video Title");
        videoPref.setSummary("Video Description");
        videoPref.setOnPreferenceClickListener(this);
        addPreference(videoPref);
        super.onAttachedToActivity();
    }

    public boolean onPreferenceClick(Preference preference) {
        parentScreen.getDialog().dismiss();
        this.callChangeListener(preference.getKey());
        return true;
    }

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