我在操作栏中制作了一个刷新项目。当您单击它时,将显示一个对话框,要求检查互联网连接。问题是根本没有显示对话框。我想知道我的代码是否有问题。
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);
}
答案 0 :(得分:3)
您需要致电AlertDialog.show()
以在屏幕上显示警告:
AlertDialog alertDialog = dialog.create();
// show it
alertDialog.show();
答案 1 :(得分:2)
我认为你没有打电话给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);
dialog.show();//=> here is the change
}
答案 2 :(得分:2)
您忘记从AlertDialog
对象创建AlertDialog.Builder
实例并显示该对话框。
另一件事,您尝试在AlertDialog.Builder
方法中以if-else
条件创建Refresh()
对象,但您应该在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();
}
<强>更新强>
对于取消选项,您必须在对话框中添加另一个按钮,如下所示...
dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//add your code
dialog.dismiss();
}
});
答案 3 :(得分:2)
在ifelse
条件之后将此行添加到以下两行
AlertDialog alertDialog = dialog.createDialog();
alertDialog.show();
你完成代码......
答案 4 :(得分:1)
将代码编辑为
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();
}