标签:android-button android-dialog java android
我已经设置了一个自定义的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>
编辑:我已经使用以下代码创建了对话框:
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);
}
解决方法:
看到它按照这个工作7000
尝试
yourTimePickerDialog.getButton(whichButton).setText("New Text"); (put the null check if needed)
or
yourTimePickerDialog.setButton(BUTTON_POSITIVE, "Find", yourTimePickerDialog);
标签:android-button,android-dialog,java,android 来源: https://codeday.me/bug/20191201/2078656.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。