如何从首选项屏幕打开 AlertDialog?

发布于 2024-10-27 09:36:28 字数 78 浏览 3 评论 0原文

我正在android中开发应用程序 如果用户从首选项屏幕中检查复选框首选项,我想显示 AlertDialog。 那么我怎样才能做到这一点..?

I am developing Application in android
I want to show AlertDialog if user check the checkboxpreference from preference screen.
so how i can do that..?

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

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

发布评论

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

评论(3

南渊 2024-11-03 09:36:28

试试这个...

public class MyPreferences extends PreferenceActivity implements OnSharedPreferenceChangeListener {

      public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
                final Preference preference) {

         if(preference.equals("MyCheckboxPreferenceKey")) {

             AlertDialog.Builder builder = new AlertDialog.Builder(this);

         builder.setMessage("Your Message");

         builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

           public void onClick(DialogInterface dialog, int id) {

             //action on dialog close
           }

         });

         builder.show();

      }


}

Try this one ...

public class MyPreferences extends PreferenceActivity implements OnSharedPreferenceChangeListener {

      public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
                final Preference preference) {

         if(preference.equals("MyCheckboxPreferenceKey")) {

             AlertDialog.Builder builder = new AlertDialog.Builder(this);

         builder.setMessage("Your Message");

         builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

           public void onClick(DialogInterface dialog, int id) {

             //action on dialog close
           }

         });

         builder.show();

      }


}
久夏青 2024-11-03 09:36:28

重写 PreferenceActivity 类中的 onSharedPreferenceChanged

public class MyPreferences extends PreferenceActivity implements OnSharedPreferenceChangeListener {
    ...
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if (key.equals("MyCheckboxPreferenceKey")) {
            //Show your AlertDialog here!
        }
    }

Override onSharedPreferenceChanged in your PreferenceActivity class:

public class MyPreferences extends PreferenceActivity implements OnSharedPreferenceChangeListener {
    ...
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if (key.equals("MyCheckboxPreferenceKey")) {
            //Show your AlertDialog here!
        }
    }
裂开嘴轻声笑有多痛 2024-11-03 09:36:28
public class MyPreferences extends PreferenceActivity{
 ...
//getting current context for builder
AlertDialog.Builder build = new AlertDialog.Builder(this);
//setting some title text
build.setTitle("SomeTitle");
//setting radiobuttons list
build.setSingleChoiceItems(new String[]{"One", "Two"}, 0, new OnClickListener() {                               

            @Override
            public void onClick(DialogInterface dialog, int which) {
                //Some behavior here    
            }
        });
        build.setNegativeButton("Cancel", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                //Might be empty
            }
        });
                    //creating dialog and showing
        AlertDialog dialog = build.create();
        dialog.show();
}

就这样。

public class MyPreferences extends PreferenceActivity{
 ...
//getting current context for builder
AlertDialog.Builder build = new AlertDialog.Builder(this);
//setting some title text
build.setTitle("SomeTitle");
//setting radiobuttons list
build.setSingleChoiceItems(new String[]{"One", "Two"}, 0, new OnClickListener() {                               

            @Override
            public void onClick(DialogInterface dialog, int which) {
                //Some behavior here    
            }
        });
        build.setNegativeButton("Cancel", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                //Might be empty
            }
        });
                    //creating dialog and showing
        AlertDialog dialog = build.create();
        dialog.show();
}

That's all.

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