2016-08-07 5 views
2

現在、私は、ボタンのテキストを、それが表すブール値に応じて切り替える「トグル」タイプのシステムを適用しようとしています。ボタンのテキストを直ちに変更する

たとえば、ブール値RequestingLU trueの場合、ボタンに「停止」と、偽の場合は「開始」とします。

現在、私はそれを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

? – Iceman

+0

Woops、私は間違ったものをクリックした; /すみません。 –

答えて

0

は、私はそれはあなた ます。public void ONSTART(){ super.onStart()のために正常に動作します願っていますあなたのonClicks内

1

をごStopButton.setText()を置きます。 final SharedPreferences設定= getSharedPreferences(PREFS_NAME、0); final SharedPreferences.Editor editor = settings.edit(); 最終ボタンStopButton =(ボタン)findViewById(R.id.StopButton); ブール値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); 

}このjqueryのですか

+0

これは正しくフォーマットされていません。それを修正してください。 –

関連する問題