对话框不起作用 - Android

时间:2014-03-05 04:02:34

标签: android dialog alert android-alertdialog

我在操作栏中制作了一个刷新项目。当您单击它时,将显示一个对话框,要求检查互联网连接。问题是根本没有显示对话框。我想知道我的代码是否有问题。

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


    }

5 个答案:

答案 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();
    }