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”之后才能开始。
答案 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();
}