0
private void showDialog() {
               final Dialog dialog = new Dialog(Notification_Settings_Activity.this);
               dialog.setContentView(R.layout.custom_notification_dilogue);
               myradiogroup = (RadioGroup)dialog.findViewById(R.id.myRadioGroup);

               dialog.setTitle("Vibrate");
               dialog.show();

               System.out.print("Dialog...");

               btn_cancel = (Button)dialog.findViewById(R.id.btn_cancel);

               off = (RadioButton) dialog.findViewById(R.id.off);
               vibrate_default = (RadioButton) dialog.findViewById(R.id.vibrate_default);
               vibrate_short = (RadioButton)dialog.findViewById(R.id.vibrate_short);
               vibrate_long = (RadioButton)dialog.findViewById(R.id.vibrate_long);

               notificationSettings = JalpiApp.getSharedPreferenceNotificationSettings();


               boolean VibrateOff = notificationSettings.getBoolean("VibrateOff", true);
               if(VibrateOff)
                   off.setChecked(true);
               else
                   off.setChecked(false);

               boolean VibrateDefault = notificationSettings.getBoolean("VibrateDefault", true);
               if (VibrateDefault){
                   vibrate_default.setChecked(true);
               }
               else {
                   vibrate_default.setChecked(false);
               }

               boolean VibrateShort = notificationSettings.getBoolean("VibrateShort", true);
               if (VibrateShort){
                   vibrate_short.setChecked(true);
               }
               else {
                   vibrate_short.setChecked(false);
               }

               boolean VibrateLong = notificationSettings.getBoolean("VibrateLong", true);
               if (VibrateLong){
                   vibrate_long.setChecked(true);
               }
               else {
                   vibrate_long.setChecked(false);
               }
       //        if (selectedID1 < 1){
       //            vibrate_default.setSelected(true);
       //        }
       //        else{
       //            vibrate_default.setSelected(false);
       //        }
       //
       //        if (selectedID2 < 2){
       //            vibrate_short.setSelected(true);
       //        }else {
       //            vibrate_short.setSelected(false);
       //        }
       //
       //        if (selectedID3 < 3){
       //            vibrate_long.setSelected(true);
       //        }else {
       //            vibrate_long.setSelected(false);
       //        }

               btn_cancel.setOnClickListener(new OnClickListener() {
                   @Override
                   public void onClick(View v) {
                       dialog.dismiss();
                   }
               });

               myradiogroup.setOnCheckedChangeListener(

                       new RadioGroup.OnCheckedChangeListener() {
                           @Override
                           public void onCheckedChanged(RadioGroup group, int checkedId) {

                               int selectedId = myradiogroup.getCheckedRadioButtonId();

                               if (selectedId == off.getId()){
                                   Toast.makeText(getBaseContext(), "You Selected Off",
                                           Toast.LENGTH_LONG).show();

                                   SharedPreferences.Editor editor = notificationSettings.edit();
                                   editor.putBoolean("VibrateOff", true);
                                   editor.commit();

                                   Vibrator vibrator = (Vibrator) getSystemService(Notification_Settings_Activity.VIBRATOR_SERVICE);
                                   vibrator.vibrate(0);
                               }else {
                                   SharedPreferences.Editor editor = notificationSettings.edit();
                                   editor.putBoolean("VibrateOff", false);
                                   editor.commit();
                               }

                               if (selectedId == vibrate_default.getId()) {

                                   Toast.makeText(getBaseContext(), "You Selected vibrate_default",
                                          Toast.LENGTH_LONG).show();

                                   SharedPreferences.Editor editor = notificationSettings.edit();
                                   editor.putBoolean("VibrateDefault", true);
                                   editor.commit();

                                   Vibrator vibrator1 = (Vibrator) getSystemService(Notification_Settings_Activity.VIBRATOR_SERVICE);
                                   vibrator1.vibrate(400);
                               }
                               else {
                                   SharedPreferences.Editor editor = notificationSettings.edit();
                                   editor.putBoolean("VibrateDefault", true);
                                   editor.commit();
                               }
                               if (selectedId == vibrate_short.getId()) {
                                   SharedPreferences.Editor editor = notificationSettings.edit();
                                   editor.putBoolean("VibrateShort", true);
                                   editor.commit();

                                   Vibrator vibrator3 = (Vibrator) getSystemService(Notification_Settings_Activity.VIBRATOR_SERVICE);
                                   vibrator3.vibrate(300);
                               }else {
                                   SharedPreferences.Editor editor = notificationSettings.edit();
                                   editor.putBoolean("VibrateShort", false);
                                   editor.commit();

                               }
                               if (selectedId == vibrate_long.getId()) {

                                   Toast.makeText(getBaseContext(), "You Selected vibrate_long",
                                           Toast.LENGTH_LONG).show();

                                   SharedPreferences.Editor editor = notificationSettings.edit();
                                   editor.putBoolean("VibrateLong",true);
                                   editor.commit();

                                   Vibrator vibrator3 = (Vibrator) getSystemService(Notification_Settings_Activity.VIBRATOR_SERVICE);
                                   vibrator3.vibrate(600);
                               }else {
                                   SharedPreferences.Editor editor = notificationSettings.edit();
                                   editor.putBoolean("VibrateLong",false);
                                   editor.commit();
                               }
                           }
                       }
               );

           }
4

1 回答 1

0

由于您有一些重复的代码,我允许自己更改您的代码以提高可读性,希望这也能纠正您的错误。如果没有,请发表评论,我会尝试与您一起修复它。

private void showDialog() {
           final Dialog dialog = new Dialog(Notification_Settings_Activity.this);
           dialog.setContentView(R.layout.custom_notification_dilogue);
           myradiogroup = (RadioGroup)dialog.findViewById(R.id.myRadioGroup);

           dialog.setTitle("Vibrate");
           dialog.show();

           System.out.print("Dialog...");

           btn_cancel = (Button)dialog.findViewById(R.id.btn_cancel);

           off = (RadioButton) dialog.findViewById(R.id.off);
           vibrate_default = (RadioButton) dialog.findViewById(R.id.vibrate_default);
           vibrate_short = (RadioButton)dialog.findViewById(R.id.vibrate_short);
           vibrate_long = (RadioButton)dialog.findViewById(R.id.vibrate_long);

           notificationSettings = JalpiApp.getSharedPreferenceNotificationSettings();


           boolean VibrateOff = notificationSettings.getBoolean("VibrateOff", true);
           off.setChecked(VibrateOff);

           boolean VibrateDefault = notificationSettings.getBoolean("VibrateDefault", true);
           vibrate_default.setChecked(VibrateDefault);

           boolean VibrateShort = notificationSettings.getBoolean("VibrateShort", true);
               vibrate_short.setChecked(VibrateShort);

           boolean VibrateLong = notificationSettings.getBoolean("VibrateLong", true);
           vibrate_long.setChecked(VibrateLong);

           btn_cancel.setOnClickListener(new OnClickListener() {
               @Override
               public void onClick(View v) {
                   dialog.dismiss();
               }
           });

           myradiogroup.setOnCheckedChangeListener(

                   new RadioGroup.OnCheckedChangeListener() {
                       @Override
                       public void onCheckedChanged(RadioGroup group, int checkedId) {

                           //int selectedId = myradiogroup.getCheckedRadioButtonId(); - you already get your checkedRadioButtonId through the Listener

                           switch(checkedID){
                               case R.id.off:
                                   Toast.makeText(getBaseContext(), "You Selected Off",
                                   Toast.LENGTH_LONG).show();

                                   SharedPreferences.Editor editor = notificationSettings.edit();
                                   editor.putBoolean("VibrateOff", true);
                                   editor.putBoolean("VibrateDefault", false);
                                   editor.putBoolean("VibrateShort", false);
                                   editor.putBoolean("VibrateLong", false);
                                   editor.commit();

                                   Vibrator vibrator = (Vibrator) getSystemService(Notification_Settings_Activity.VIBRATOR_SERVICE);
                                   vibrator.vibrate(0);
                                   break;
                               case R.id.vibrate_default:
                                   //insert code similiar to first case
                                   break;
                               case R.id.vibrate_short:
                                   //insert code similiar to first case
                                   break;
                               case R.id.vibrate_long:
                                   //insert code similiar to first case
                                   break;
                               case -1:
                                   //choices were cleared, added to improve stability
                                   break;
                           }
                       }
                   }
           );

       }
于 2015-12-07T08:05:17.697 回答