2016-08-07 15 views
2

Ich versuche derzeit, eine "Toggle" Art von System anzuwenden, die den Text der Schaltfläche in Abhängigkeit von der Boolean es darstellt.Ändern Sie den Text der Schaltfläche sofort

Zum Beispiel, wenn Boolean RequestingLU true, möchte ich die Schaltfläche zu sagen 'Stop', und wenn falsch, sagen 'Start'.

Derzeit habe ich es in onStart(); so dass es zumindest zu aktualisieren, wenn ich den Bildschirm besuchen,

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(); 
      } 
     }); 
    } 

} 

ich die Taste Funktion will, so dass die Änderung sofort, nachdem der Benutzer drückt gilt ‚entlassen‘. Wie kann ich das erreichen?

+1

wie diese jquery ist? – Iceman

+0

Woops, ich habe das falsche angeklickt;/Sorry. –

Antwort

0

setzen Sie Ihre StopButton.setText() in Ihrem onClicks

1

Ich hoffe, es wird für Sie public void onStart() funktionieren { super.onStart(); final SharedPreferences settings = getSharedPreferences (PREFS_NAME, 0); final SharedPreferences.Editor Editor = settings.edit(); final Button StopButton = (Schaltfläche) findViewById (R.id.StopButton); Boolean RequestingLU = settings.getBoolean ("RequestingLU", true);

StopButton.setText(RequestingLU?R.string.Stop_Button:R.string.Start_Button); 
    StopButton.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      final boolean RequestingLUSwitch = settings.getBoolean("RequestingLU", true); 
      if(RequestingLUSwitch) { 
       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) { 
         updatePreference(!RequestingLUSwitch); 
         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{ 
       updatePreference(!RequestingLUSwitch); 
       Toast.makeText(MainMenu.this, "You have started the app", Toast.LENGTH_SHORT).show(); 
      } 
     } 
    }); 


} 

private void updatePreference(boolean requestingSwitch){ 
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 
SharedPreferences.Editor editor = settings.edit(); 
editor.putBoolean("RequestingLU", !RequestingLUSwitch); 
editor.apply(); 
StopButton.setText(!RequestingLUSwitch ? R.string.Stop_Button : R.string.Start_Button); 

}

+0

Dies ist falsch formatiert. Bitte repariere es. –

Verwandte Themen