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

所以会发生错误警报显示,但它会立即关闭并出现下一个活动。我希望活动在选择“好的”之后才开始。

4

3 回答 3

0

根据您的需要,将您startActivity(i);的 if(或)else(或)两个块移至内部。

于 2012-11-28T21:29:06.443 回答
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();
    }
于 2012-11-29T00:53:29.873 回答
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();
}
于 2012-11-29T06:13:15.337 回答