在AlertDialog中调用同一个AlertDialog
我有一个 AlertDialog
提示用户是否要发送数据。我正在做的是检查是否有互联网连接,如果没有,我将再次显示该对话框。 该对话框显示,但当我单击“是”时,连接断开时它不会显示相同的对话框。
public void sendData(){
boolean connected = checkConnectivity(getApplicationContext());
//connected is false, but dialog doesnt show the second time.
if(connected==false){
//show dialog
showDialog(0);
}else{
//connected, send data
}
}
@Override
protected Dialog onCreateDialog( int id )
{
return
new AlertDialog.Builder( this )
.setTitle( "Send data?" )
.setPositiveButton( "Yes", new DialogButtonClickHandler() )
.setNegativeButton( "No", new DialogButtonClickHandler() )
.create();
}
public class DialogButtonClickHandler implements DialogInterface.OnClickListener
{
public void onClick( DialogInterface dialog, int clicked )
{
switch( clicked )
{
case DialogInterface.BUTTON_POSITIVE:
//Problem occurs here. sendData() gets called but dialog not displayed the second time
sendData();
break;
case DialogInterface.BUTTON_NEGATIVE:
return;
}
}
}
有人可以帮忙吗?
I have an AlertDialog
that prompts user if they want to send data. What I am doing is that I check if there is internet connection, if not I will display the dialog again.
The dialog displays but when I click 'yes', it does not display the same dialog when the connection is down.
public void sendData(){
boolean connected = checkConnectivity(getApplicationContext());
//connected is false, but dialog doesnt show the second time.
if(connected==false){
//show dialog
showDialog(0);
}else{
//connected, send data
}
}
@Override
protected Dialog onCreateDialog( int id )
{
return
new AlertDialog.Builder( this )
.setTitle( "Send data?" )
.setPositiveButton( "Yes", new DialogButtonClickHandler() )
.setNegativeButton( "No", new DialogButtonClickHandler() )
.create();
}
public class DialogButtonClickHandler implements DialogInterface.OnClickListener
{
public void onClick( DialogInterface dialog, int clicked )
{
switch( clicked )
{
case DialogInterface.BUTTON_POSITIVE:
//Problem occurs here. sendData() gets called but dialog not displayed the second time
sendData();
break;
case DialogInterface.BUTTON_NEGATIVE:
return;
}
}
}
Can anyone help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
隔了这么久终于找到答案了!在
sendData()
方法中,您需要重建对话框,而不是调用showDialog()
Figured out the answer after so long! In the
sendData()
method, instead of callingshowDialog()
, you need to rebuild the dialog