其他分享
首页 > 其他分享> > Android – 更改首选项时确认对话框

Android – 更改首选项时确认对话框

作者:互联网

任何人都可以建议我在更改CheckboxPreference或SwitchPrerence时执行AlertDialog以确认更改,
恩.如果我想取消选中Preference,AlertDialog应警告他们“你确定要取消选中吗?”加上对话框应该是可自定义的,因为我将使用自己的布局.我不想使用Dialog的Default Bluestrip Header.

SwitchPreference pref = (SwitchPreference) findPreference("prefkey_use_passcode");
    pref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

        @Override
        public boolean onPreferenceChange(Preference arg0, Object arg1) {
            DialogPreference dialog = new DialogPreference(getParent());

            return false;
        }
    });

解决方法:

这样的事情应该有效:

      AlertDialog.Builder builder = new AlertDialog.Builder (this);
      builder.setMessage (getString (R.string.are_you_sure));
      builder.setNegativeButton (getString(android.R.string.cancel),
                                 new DialogInterface.OnClickListener()
                                 {
                                   @Override
                                   public void onClick (DialogInterface dialog, int which)
                                   {
                                     finish();
                                   }
                                 });
      builder.setPositiveButton (getString(android.R.string.ok),
                                 new DialogInterface.OnClickListener()
                                 {
                                   @Override
                                   public void onClick (DialogInterface dialog, int which)
                                   {
                                     --- do the work to update the preference ---
                                     finish();
                                   }
                                 });
      builder.create().show();
      return;

标签:android-alertdialog,android,preference,customdialog
来源: https://codeday.me/bug/20190831/1774198.html