[英]Dialog box not working - Android
I made a refresh item in action bar. 我在操作栏中做了一个刷新项目。 When u click it a dialog box will show asking to check the internet connection.
当您单击它时,将显示一个对话框,要求您检查互联网连接。 The problem is there's no dialog box showing up at all.
问题是根本没有对话框出现。 I wanna know if there's something wrong with my code.
我想知道我的代码是否有问题。
private void Refresh() {
if(IsparkLib.isInternetConnected(InquiryMainActivity.this)){
nameOfMethod();
AlertDialog.Builder dialog = new AlertDialog.Builder(InquiryMainActivity.this);
dialog.setTitle("Are you sure you want to update?");
dialog.setCancelable(true);
dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int arg1) {
// TODO Auto-generated method stub
Intent intent = new Intent(InquiryMainActivity.this,InquiryMainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
});
}
else{
AlertDialog.Builder dialog = new AlertDialog.Builder(InquiryMainActivity.this);
dialog.setTitle("Warning");
dialog.setPositiveButton("OK",null);
final TextView mes = new TextView(InquiryMainActivity.this);
mes.setTextColor(Color.BLACK);
mes.setText("Please check your internet connection");
mes.setTextSize(20);
}
You will need to call AlertDialog.show()
to show alert on screen do it as: 您将需要调用
AlertDialog.show()
在屏幕上显示警报,方法如下:
AlertDialog alertDialog = dialog.create();
// show it
alertDialog.show();
I think you are not calling dialog.show();
我想你不是在打电话
dialog.show();
. 。 Check once.
检查一次。
In else block try like this... 在其他块中尝试这样...
else{
AlertDialog.Builder dialog = new AlertDialog.Builder(InquiryMainActivity.this);
dialog.setTitle("Warning");
dialog.setPositiveButton("OK",null);
final TextView mes = new TextView(InquiryMainActivity.this);
mes.setTextColor(Color.BLACK);
mes.setText("Please check your internet connection");
mes.setTextSize(20);
dialog.show();//=> here is the change
}
You forgot to create the AlertDialog
instance from AlertDialog.Builder
object and to show that dialog. 您忘记了从
AlertDialog.Builder
对象创建AlertDialog
实例并显示该对话框。
Another thing , you have tried to create the AlertDialog.Builder
object in both if-else
condition in Refresh()
method but you should create outside the if-else
condition as below... 另一件事 ,您尝试在
Refresh()
方法中的两个if-else
条件下创建AlertDialog.Builder
对象,但是您应该在if-else
条件之外创建,如下所示...
private void Refresh() {
AlertDialog.Builder dialog = new AlertDialog.Builder(InquiryMainActivity.this);
if(IsparkLib.isInternetConnected(InquiryMainActivity.this)){
nameOfMethod();
dialog.setTitle("Are you sure you want to update?");
dialog.setCancelable(true);
dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int arg1) {
// TODO Auto-generated method stub
Intent intent = new Intent(InquiryMainActivity.this,InquiryMainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
});
} else {
dialog.setTitle("Warning");
dialog.setPositiveButton("OK",null);
final TextView mes = new TextView(InquiryMainActivity.this);
mes.setTextColor(Color.BLACK);
mes.setText("Please check your internet connection");
mes.setTextSize(20);
}
AlertDialog alertDialog = dialog.createDialog();
alertDialog.show();
}
Update: 更新:
For Cancel option, you have to add another button to the dialog as below... 对于“ 取消”选项,您必须向对话框添加另一个按钮,如下所示...
dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//add your code
dialog.dismiss();
}
});
Add this below two line after ifelse
condition.. 在
ifelse
条件之后,将其添加到两行以下。
AlertDialog alertDialog = dialog.createDialog();
alertDialog.show();
And you complete with your code.... 然后完成代码。
edit your code as 将您的代码编辑为
private void Refresh() {
if(IsparkLib.isInternetConnected(InquiryMainActivity.this)){
nameOfMethod();
AlertDialog.Builder dialog = new AlertDialog.Builder(InquiryMainActivity.this);
dialog.setTitle("Are you sure you want to update?");
dialog.setCancelable(true);
dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int arg1) {
// TODO Auto-generated method stub
Intent intent = new Intent(InquiryMainActivity.this,InquiryMainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
});
dialog.show();
}
else{
AlertDialog.Builder dialog = new AlertDialog.Builder(InquiryMainActivity.this);
dialog.setTitle("Warning");
dialog.setPositiveButton("OK",null);
/*final TextView mes = new TextView(InquiryMainActivity.this);
mes.setTextColor(Color.BLACK);
mes.setText("Please check your internet connection");
mes.setTextSize(20);*/ It's not necessary
dialog.setMessage("Please check your internet connection");//
dialog.show();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.