在我的应用程序中,我试图在活动开始时打开一个对话框,并且它有一个肯定按钮。单击该按钮将打开一个新活动。使用以下代码可以正常工作。但是当我长按菜单按钮或搜索按钮时,对话框会消失。即使按菜单或搜索按钮,如何使此对话框保持不变。谢谢。
public class Sam3Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView title = new TextView(this);
title.setText("DM2");
title.setBackgroundColor(Color.DKGRAY);
title.setPadding(10, 10, 10, 10);
title.setGravity(Gravity.CENTER);
title.setTextColor(Color.WHITE);
title.setTextSize(20);
/* alert message */
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCustomTitle(title);
builder.setMessage("dialog with message").setCancelable(false).setPositiveButton(
"Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
Log.d("choose","on dissmiss");
startActivity(new Intent(getApplicationContext(),
StatisticDisplay.class));//second activity
}
});
AlertDialog alert = builder.create();
alert.show();
}
}