对话框在未单击的情况下消失

时间:2012-11-28 21:27:42

标签: java android android-intent dialog

public void onClick(View v) {

            if(a){


Intent i = new Intent();
if(type.equals("x")){
showErrorAlert("string");

                    i = new Intent(Activity1.this, Activity2.class);
                    i.putExtra("label", var);
                    i.putExtra("label1", var2);
                    startActivity(i);
                }
                else if(type.equals("y")){
                    i = new Intent(Activity1.this, Activity3.class);
                    i.putExtra("label2", var3);
                }

                //startActivity(i);
            }

            else startActivity(new Intent(Activity1.this, Activity4.class));
        }
    });

private void showErrorAlert(String errorMsg){           
    AlertDialog errorDialog = new AlertDialog.Builder(this).create();
    errorDialog.setTitle("title");
    errorDialog.setMessage(errorMsg);
    errorDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "Okay", new  DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

        }
    });

    errorDialog.show();
}

所以会发生错误提示,但会立即关闭并显示下一个活动。我希望活动在选择“Okay”之后才能开始。

3 个答案:

答案 0 :(得分:0)

startActivity(i);移至内部if(或)else(或)两个块,具体取决于您需要的位置。

答案 1 :(得分:0)

放置startActivity(i);在对话框中的onClick方法中,如下所示:

private void showErrorAlert(String errorMsg){           
        AlertDialog errorDialog = new AlertDialog.Builder(this).create();
        errorDialog.setTitle("title");
        errorDialog.setMessage(errorMsg);
        errorDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "Okay", new  DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                startActivity(i);

            }
        });

        errorDialog.show();
    }

答案 2 :(得分:0)

试试这个

public void onClick(View v) {

            if(a){
                Intent i = new Intent();
                if(type.equals("x")){
                 showErrorAlert("string");

               }
                else if(type.equals("y")){
                    i = new Intent(Activity1.this, Activity3.class);
                    i.putExtra("label2", var3);
                }

                //startActivity(i);
            }

            else startActivity(new Intent(Activity1.this, Activity4.class));
        }
    });

private void showErrorAlert(String errorMsg){           
    AlertDialog errorDialog = new AlertDialog.Builder(this).create();
    errorDialog.setTitle("title");
    errorDialog.setMessage(errorMsg);
    errorDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "Okay", new  DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

             Intent i = new Intent(Activity1.this, Activity2.class);
                    i.putExtra("label", var);
                    i.putExtra("label1", var2);
                    startActivity(i);
        }
    });

    errorDialog.show();
}