三种:Dialog、Dialog主题活动、Toast
Dialog(Alert)
Dialog d = new Dialog(MyActivity.this); //使用新窗口为它遮挡的窗口着色,并模糊它 Window window = d.getWindow(); window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND, WindowManager.LayoutParams.FLAG_BLUR_BEHIND); //设置标题 d.setTitle("Dialog Title"); //扩充布局 d.setContentView(R.layout.dialog_view); //找出布局中使用的TextView并设置其文本值 TextView text = (TextView)d.findViewById(R.id.dialogTextView); text.setText("This is the text in my dialog");
d.show();
AlertDialog.Builder ad = new AlertDialog.Builder(context); ad.setTitle("title"); ad.setMessage("message"); ad.setPositiveButton("button1", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); ad.setNegativeButton("button2", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); ad.setCancelable(true); ad.setOnCancelListener(new OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { } }); ad.show();
Dialog主题活动
<activity android:name="MyDialogActivity" android:theme="@android:style/Theme.Dialog"> </activity>
Toast
Toast.makeText(this, String.format("名字 %s,性别 %s,年龄 %d", data.getUserName(),data.getSex(),data.getAge()), Toast.LENGTH_SHORT).show();
对话框的管理
在activity中的OnCreateDialog和onPrepareDialog事件处理程序可以保存和管理对话框。在第一次到onCreateDialog中创建对话框之后,之后每一次调用showDialog都会触发onPrepareDialog,在这方法内可以修改所要显示的值
static final private int TIME_DIALOG = 1; @Override protected Dialog onCreateDialog(int id) { switch (id) { case (TIME_DIALOG): AlertDialog.Builder timeDialog = new AlertDialog.Builder(this); timeDialog.setTitle("titile"); timeDialog.setMessage("message"); return timeDialog.create(); } return null; } @Override protected void onPrepareDialog(int id, Dialog dialog) { switch (id) { case (TIME_DIALOG): AlertDialog timeDialog = (AlertDialog) dialog; timeDialog.setMessage("new message"); break; } } showDialog(TIME_DIALOG);