Java AlertDialog.setButton可以';不更改按钮文本吗?

Java AlertDialog.setButton可以';不更改按钮文本吗?,java,android,android-button,android-dialog,Java,Android,Android Button,Android Dialog,我已经设置了一个自定义AlertDialog(实际上是一个自定义TimePickerDialog)。我正在使用OnShowListener,根据条件更新按钮文本。我将此代码放在一个单独的方法中: private void changeState(){ if(!SleepAlarm.isActive()){ Log.d("RPod_MainActivity","SleepAlarm is NOT active"); this.setButton(TimePi

我已经设置了一个自定义AlertDialog(实际上是一个自定义TimePickerDialog)。我正在使用OnShowListener,根据条件更新按钮文本。我将此代码放在一个单独的方法中:

private void changeState(){
    if(!SleepAlarm.isActive()){
        Log.d("RPod_MainActivity","SleepAlarm is NOT active");
        this.setButton(TimePickerDialog.BUTTON1,(CharSequence)MainActivity.currentContext.getResources().getText(R.string.sleep_enable), sleepEnable);
        this.setButton(TimePickerDialog.BUTTON2,(CharSequence)MainActivity.currentContext.getResources().getText(R.string.sleep_cancel), sleepDisable);
    }
    else{
        Log.d("RPod_MainActivity","SleepAlarm is active");
        this.setButton(TimePickerDialog.BUTTON1,(CharSequence)MainActivity.currentContext.getResources().getText(R.string.sleep_change), sleepEnable);
        this.setButton(TimePickerDialog.BUTTON2,(CharSequence)MainActivity.currentContext.getResources().getText(R.string.sleep_disable), sleepDisable);
    }
    this.setTitle(R.string.sleep_title);
}
按下TimePickerDialog.BUTTON1按钮后,当我再次显示对话框时,条件满足,文本应该更改。但事实并非如此

我已经和LogCat核实过情况是否正常。第一次打开对话框时,我看到“SleepAlarm未激活”。一旦我按下第一个按钮,当再次打开对话框时,我会看到“SleepAlarm处于活动状态”。。。但是按钮文本是一样的

以下是字符串:

<string name="sleep_enable">Enable</string>
<string name="sleep_disable">Disable</string>
<string name="sleep_cancel">Cancel</string>
<string name="sleep_change">Change</string>
onCreateDialog实现:

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case TIME_DIALOG_ID:
        // set time picker as current time
        timePicker = new SleepPickerDialog(this,this);
        return timePicker;
    }
    return null;
}
和SleepPickerDialog(扩展TimePickerDialog)构造函数:

public SleepPickerDialog(Context context, OnTimeSetListener listener){
    super(context, listener, MainActivity.sleep_hour, MainActivity.sleep_hour, true);
    changeState();
    this.setOnShowListener(showListener);
}
Algo,onShowListener:

private DialogInterface.OnShowListener showListener = new DialogInterface.OnShowListener() {
    @Override
    public void onShow(DialogInterface dialog) {
        // Cambiar botones
        changeState();
    }
};
我错过了一些重要的事情

有关解决方案,请参阅接受的答案评论。我在这里发布完整的代码更改:

public SleepPickerDialog(Context context, OnTimeSetListener listener){
    super(context, listener, MainActivity.sleep_hour, MainActivity.sleep_hour, true);
    this.setButton(BUTTON_POSITIVE,(CharSequence)MainActivity.currentContext.getResources().getText(R.string.sleep_enable), sleepEnable);
    this.setButton(BUTTON_NEGATIVE,(CharSequence)MainActivity.currentContext.getResources().getText(R.string.sleep_cancel), sleepDisable);
    this.setOnShowListener(showListener);
}


private void changeState(){
    if(!SleepAlarm.isActive()){
        this.getButton(BUTTON_POSITIVE).setText(R.string.sleep_enable);
        this.getButton(BUTTON_NEGATIVE).setText(R.string.sleep_cancel);
    }
    else{
        this.getButton(BUTTON_POSITIVE).setText(R.string.sleep_change);
        this.getButton(BUTTON_NEGATIVE).setText(R.string.sleep_disable);
    }
    this.setTitle(R.string.sleep_title);
}
看看这个,照这个它就行了

试一试


不,那不行。完全相同的结果。按钮保留我在其上设置的第一个文本。第二次设置文本将不起任何作用。不确定,但可以尝试此yourTimePickerDialog.getButton(whichButton)。setText(“新文本”);(如果需要的话,可以进行空检查)是的,效果非常好!:)我将用更正的代码更新问题。非常感谢。
public SleepPickerDialog(Context context, OnTimeSetListener listener){
    super(context, listener, MainActivity.sleep_hour, MainActivity.sleep_hour, true);
    this.setButton(BUTTON_POSITIVE,(CharSequence)MainActivity.currentContext.getResources().getText(R.string.sleep_enable), sleepEnable);
    this.setButton(BUTTON_NEGATIVE,(CharSequence)MainActivity.currentContext.getResources().getText(R.string.sleep_cancel), sleepDisable);
    this.setOnShowListener(showListener);
}


private void changeState(){
    if(!SleepAlarm.isActive()){
        this.getButton(BUTTON_POSITIVE).setText(R.string.sleep_enable);
        this.getButton(BUTTON_NEGATIVE).setText(R.string.sleep_cancel);
    }
    else{
        this.getButton(BUTTON_POSITIVE).setText(R.string.sleep_change);
        this.getButton(BUTTON_NEGATIVE).setText(R.string.sleep_disable);
    }
    this.setTitle(R.string.sleep_title);
}
yourTimePickerDialog.getButton(whichButton).setText("New Text"); (put the null check if needed)

or 

yourTimePickerDialog.setButton(BUTTON_POSITIVE, "Find", yourTimePickerDialog);