2016-08-07 96 views
2

我目前正試圖應用一種「切換」類型的系統,根據它代表的布爾值來切換按鈕的文本。立即更改按鈕的文本

例如,當布爾型RequestingLU爲true時,我希望按鈕說'停止',如果爲false,則說'開始'。

目前我已經把它放在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設置= getSharedPreferences(PREFS_NAME,0); final SharedPreferences.Editor editor = settings.edit(); Final Button StopButton =(Button)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

這是格式不正確。請修復它。 –