1

如果用户在输出对话框中选择“确定”,我想更改班级中的首选项。为此,我在课堂上实现了这个方法:

if (basalcor != basal) {

                    FragmentManager fragmentManager = getFragmentManager();
                    ChangeBasalDialog dialogo = new ChangeBasalDialog(basal);
                    dialogo.show(fragmentManager, "tagbasal");
                    if (dialogo.check == true) {
                        String ok= "Basal factor changed succesfully";
                        SharedPreferences.Editor prefsEditor = myprefs.edit();
                        if (basalMorFrame == true) {
                            prefsEditor.putString("basalMor", String.valueOf(basal));
                            prefsEditor.commit();
                            Toast toast=Toast.makeText(Mymeasures.this, ok, Toast.LENGTH_SHORT);
                            toast.show();
                        }   
                        if (basalNiFrame == true) {
                            prefsEditor.putString("basalNi",
                                    String.valueOf(basal));
                            prefsEditor.commit();
                            Toast toast=Toast.makeText(Mymeasures.this, ok, Toast.LENGTH_SHORT);
                            toast.show();
                        }
                    }
                    }

这是 ChangeBasalDialog 类:

    public class ChangeBasalDialog extends DialogFragment{
     double basal=0;
     boolean check=false;
     @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {

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

            builder.setMessage("The system recommends you now a new Basal factor ("+String.valueOf(basal)+"). Do you want to set the new one as default?")
            .setTitle("New Basal recommendation avaliable")
            .setPositiveButton("Yes, i want", new DialogInterface.OnClickListener()  {
                   public void onClick(DialogInterface dialog, int id) {
                            check=true;
                            dialog.cancel();
                       }
                   })
            .setNegativeButton("No, I'll keep my old value", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                       }
                   });

            return builder.create();
        }



     public ChangeBasalDialog (double a){
     basal=a;
     }
}

问题是即使我按确定,dialogo.check 总是错误的,然后我永远无法更改我的偏好。当我按下 OK 按钮时,我只想让它返回 dialogo.check=true 但我已经尝试了一切,但我根本看不到它。提前致谢

4

1 回答 1

1

当你这样做时: dialog.cancel();你重置所有的变量。

我建议您在 ChangeBasalDialog 之外使用布尔检查(如果您在另一个活动中有此 ChangeBasalDialog)。

或者为您的 DialogFragment 实现一个侦听器,是一个有据可查的示例。

于 2013-09-03T16:58:04.243 回答