2016-08-07 2 views
2

В настоящее время я пытаюсь применить систему «переключения», которая переключает текст кнопки в зависимости от логического значения, которое она представляет.Изменить текст кнопки немедленно

Например, когда boolean RequestingLU true, я хочу, чтобы кнопка говорила «stop», а если false, скажите «start».

В настоящее время я положил его в onStart(); так что, по крайней мере, обновление каждый раз, когда я захожу на экран,

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

} 

Я хочу, чтобы функционирование кнопки, так что изменение применяется сразу после нажатия клавиши «отпустить». Как я могу это достичь?

+1

как это JQuery? – Iceman

+0

Woops, я щелкнул неправильно,/Извините. –

ответ

0

положить StopButton.setText() внутри вашего onClicks

1

Я надеюсь, что он будет работать нормально для вас общественного недействительными OnStart() { super.onStart(); final SharedPreferences settings = getSharedPreferences (PREFS_NAME, 0); final SharedPreferences.Editor editor = settings.edit(); final Button StopButton = (кнопка) 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

Отформатировано неправильно. Пожалуйста, исправьте это. –

Смежные вопросы