Android 立即更改按钮的文本

Android 立即更改按钮的文本,android,Android,我目前正在尝试应用一种“切换”系统,根据按钮所代表的布尔值切换按钮的文本 例如,当boolean RequestingLU为true时,我希望按钮显示“stop”,如果为false,则显示“start” 目前,我已将其放在onStart()中;这样我每次访问屏幕时它都会更新 public void onStart() { super.onStart(); final SharedPreferences settings = getSharedPreferences(PREFS_N

我目前正在尝试应用一种“切换”系统,根据按钮所代表的布尔值切换按钮的文本

例如,当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();
            }
        });
    }

}

我希望该按钮能够正常工作,以便在用户按下“Disclose”后立即应用更改。我怎样才能做到这一点

把你的
StopButton.setText()
放在你的鼠标里

我希望它能对你起作用 public void onStart(){ super.onStart(); 最终SharedReferences设置=GetSharedReferences(首选项名称,0); final sharedReferences.Editor=settings.edit(); 最终按钮StopButton=(按钮)findViewById(R.id.StopButton); boolean RequestingLU=settings.getBoolean(“RequestingLU”,true)


}

这个jquery怎么样?哇,我点击错了;/很抱歉
    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);