2016-08-10 58 views
-1

我目前正在嘗試創建一個'切換'類型的按鈕,其中如果某個布爾值爲'true',則會彈出AlertDialog,而如果爲false,則不會彈出對話框。如何繼續收聽onClick?

但是,問題在於目前只有當活動開始時才改變按鈕的行爲,而不是實際改變布爾值時。

我認爲這是因爲我已經把這個方法放在了onStart上,儘管我不太確定把代碼放在哪裏。

我如何不斷收聽onClick,以便在用戶更改設置後立即更改按鈕?下面

是我當前的代碼:

public void onStart() { 
     super.onStart(); 
     final SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 
     final SharedPreferences.Editor editor = settings.edit(); 
     final Button StopButton = (Button) findViewById(R.id.StopButton); 
     boolean RequestingLU = settings.getBoolean("RequestingLU", true); 
     if (RequestingLU) { 
      StopButton.setText(R.string.Stop_Button); 
      StopButton.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
        AlertDialog.Builder StopDialog = new AlertDialog.Builder(MainMenu.this); 
        StopDialog.setTitle(R.string.Stop_Title); 
        StopDialog.setMessage(R.string.Stop_Message); 
        StopDialog.setPositiveButton(R.string.Stop_Button, new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int id) { 
          editor.putBoolean("RequestingLU", false); 
          editor.apply(); 
          Toast.makeText(MainMenu.this, "You have stopped the app", Toast.LENGTH_SHORT).show(); 
          dialog.dismiss(); 
         } 
        }); 
        StopDialog.setNeutralButton(R.string.Negative_Button, new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          //Closes box 
          dialog.dismiss(); 
         } 
        }); 
        StopDialog.create().show(); 
       } 
      }); 
     } 
     else { 
      StopButton.setText(R.string.Start_Button); 
      StopButton.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        editor.putBoolean("RequestingLU", true); 
        editor.apply(); 
        Toast.makeText(MainMenu.this, "You have started the app", Toast.LENGTH_SHORT).show(); 
       } 
      }); 
     } 

    } 
+0

嘗試在的onResume /的onCreate –

+0

的onResume? AlertDialog關閉時調用onResume嗎? –

+0

使其在oncreate –

回答

0

我認爲正確的做法是宣佈它裏面onCreate()

在對代碼進行簡短的檢查之後,我看不到您將RequestingLU布爾值聲明爲true的位置。

你需要聲明如下:

StopDialog.setPositiveButton(R.string.Stop_Button, new DialogInterface.OnClickListener() { 

@Override 
public void onClick(DialogInterface dialog, int id) { 

    editor.putBoolean("RequestingLU", true); 
    editor.apply(); 

    dialog.dismiss(); 
          } 
         }); 

StopDialog.setNeutralButton(R.string.Negative_Button, new DialogInterface.OnClickListener() { 
@Override 
public void onClick(DialogInterface dialog, int which) { 

    editor.putBoolean("RequestingLU", false); 
    editor.apply(); 
    //Closes box 
    dialog.dismiss(); 
          } 
         }); 
1

您可以嘗試像下面:

if (RequestingLU) { 
     StopButton.setText(R.string.Stop_Button); 
    } else { 
     StopButton.setText(R.string.Start_Button); 
    } 

    StopButton.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      String startText = getResources().getString(R.string.Start_Button); 
      String stopText = getResources().getString(R.string.Stop_Button); 

      String buttonText = StopButton.getText().toString(); 

      if(stopText.equals(buttonText)){ 
       AlertDialog.Builder StopDialog = new AlertDialog.Builder(MainMenu.this); 
       StopDialog.setTitle(R.string.Stop_Title); 
       StopDialog.setMessage(R.string.Stop_Message); 
       StopDialog.setPositiveButton(R.string.Stop_Button, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int id) { 
         editor.putBoolean("RequestingLU", false); 
         editor.apply(); 
         Toast.makeText(MainMenu.this, "You have stopped the app", Toast.LENGTH_SHORT).show(); 
         dialog.dismiss(); 
         StopButton.setText(R.string.Start_Button); 
        } 
       }); 
       StopDialog.setNeutralButton(R.string.Negative_Button, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         //Closes box 
         dialog.dismiss(); 
        } 
       }); 
       StopDialog.create().show(); 
      } else if(startText.equals(buttonText)){ 
       editor.putBoolean("RequestingLU", true); 
       editor.apply(); 
       Toast.makeText(MainMenu.this, "You have started the app", Toast.LENGTH_SHORT).show(); 
       StopButton.setText(R.string.Stop_Button); 
      } 
     } 
    }); 

希望它會幫助你。

+0

我在哪裏放置此代碼?的onCreate?的onResume? –

+0

'boolean RequestingLU = settings.getBoolean(「RequestingLU」,true);'line –

-1

好的,我找到了解決方案。

我只是將整個代碼塊移動到onClickListener。 也就是說,在onClickListener中放置布爾型RequestingLU檢查而不是其他方式。

全部代碼在這裏:

final SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 
     final SharedPreferences.Editor editor = settings.edit(); 
     final Button StopButton = (Button) findViewById(R.id.StopButton); 
     StopButton.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       boolean RequestingLU = settings.getBoolean("RequestingLU", true); 
       if (RequestingLU) { 
        StopButton.setText(R.string.Start_Button); 
        AlertDialog.Builder StopDialog = new AlertDialog.Builder(MainMenu.this); 
        StopDialog.setTitle(R.string.Stop_Title); 
        StopDialog.setMessage(R.string.Stop_Message); 
        StopDialog.setPositiveButton(R.string.Stop_Button, new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int id) { 
          editor.putBoolean("RequestingLU", false); 
          editor.apply(); 
          Toast.makeText(MainMenu.this, "You have stopped the app", Toast.LENGTH_SHORT).show(); 
          dialog.dismiss(); 
         } 
        }); 
        StopDialog.setNeutralButton(R.string.Negative_Button, new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          //Closes box 
          dialog.dismiss(); 
         } 
        }); 
        StopDialog.create().show(); 
       } 
       else { 
        StopButton.setText(R.string.Stop_Button); 
        editor.putBoolean("RequestingLU", true); 
        editor.apply(); 
        Toast.makeText(MainMenu.this, "You have started the app", Toast.LENGTH_SHORT).show(); 
       } 
      } 
     });