gpt4 book ai didi

java - AlertDialog.setButton 无法更改按钮文本?

转载 作者:太空狗 更新时间:2023-10-29 12:52:13 25 4
gpt4 key购买 nike

我设置了自定义 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 is NOT active”。按下第一个按钮后,再次打开对话框时,我会看到“SleepAlarm is active”...但是按钮文本是一样的!

这是字符串:

<string name="sleep_enable">Enable</string>
<string name="sleep_disable">Disable</string>
<string name="sleep_cancel">Cancel</string>
<string name="sleep_change">Change</string>

编辑:我使用以下代码创建了对话框:

showDialog(TIME_DIALOG_ID);

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

算法,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);
}

最佳答案

看这个link按照这个它的工作原理

尝试

yourTimePickerDialog.getButton(whichButton).setText("New Text"); (put the null check if needed)

or

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

关于java - AlertDialog.setButton 无法更改按钮文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11176879/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com