1.普通的Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("今天下雨了吗?") .setPositiveButton("yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { Toast.makeText(Demo7Activity.this,"你点击了yes",Toast.LENGTH_SHORT).show(); } }) .setNegativeButton("no", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { Toast.makeText(Demo7Activity.this,"你点击了no",Toast.LENGTH_SHORT).show(); } }); builder.show();
2.单选Dialog
final String []str = {"香蕉","橘子","苹果","橙子","西瓜","凤梨"}; AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("请选择一种水果") .setItems(str, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Toast.makeText(Demo7Activity.this,"你喜欢"+str[which]+"水果",Toast.LENGTH_SHORT).show(); } }); builder.show();
3.多选Dialog
final String []str = {"香蕉","橘子","苹果","橙子","西瓜","凤梨"}; final ArrayList<String>mSelectedItems = new ArrayList<String>(); AlertDialog.Builder builder = new AlertDialog.Builder(this); // Set the dialog title builder.setTitle("请选择一种水果") // Specify the list array, the items to be selected by default (null for none), // and the listener through which to receive callbacks when items are selected .setMultiChoiceItems(str, null, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { if (isChecked) { // If the user checked the item, add it to the selected items mSelectedItems.add(str[which]); } else if (mSelectedItems.contains(which)) { // Else, if the item is already in the array, remove it mSelectedItems.remove(str[which]); } } }) // Set the action buttons .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { if(mSelectedItems.size() <= 0){ Toast.makeText(Demo7Activity.this,"你没有喜欢的水果",Toast.LENGTH_SHORT).show(); return; } StringBuffer buffer = new StringBuffer(); for (int i=0;i<mSelectedItems.size();i++){ buffer.append(mSelectedItems.get(i)+" "); } Toast.makeText(Demo7Activity.this,"你喜欢的水果有"+buffer.toString(),Toast.LENGTH_SHORT).show(); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { Toast.makeText(Demo7Activity.this,"你取消了选择",Toast.LENGTH_SHORT).show(); } }); builder.show();
4. 自定义Dialog
demo7_custom_alert_view.xml代码
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:src="@drawable/ic_launcher" android:layout_width="match_parent" android:layout_height="64dp" android:scaleType="center" android:background="#FFFFBB33" android:contentDescription="@string/app_name" /> <EditText android:id="@+id/username" android:inputType="textEmailAddress" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:layout_marginLeft="4dp" android:layout_marginRight="4dp" android:layout_marginBottom="4dp" android:hint="请输入用户名" /> <EditText android:id="@+id/password" android:inputType="textPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="4dp" android:layout_marginLeft="4dp" android:layout_marginRight="4dp" android:layout_marginBottom="16dp" android:fontFamily="sans-serif" android:hint="请输入密码"/> </LinearLayout>
主要代码:
AlertDialog.Builder builder = new AlertDialog.Builder(this); // Get the layout inflater LayoutInflater inflater = this.getLayoutInflater(); // Inflate and set the layout for the dialog // Pass null as the parent view because its going in the dialog layout builder.setView(inflater.inflate(R.layout.demo7_custom_alert_view, null)) // Add action buttons .setPositiveButton("登录", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { Toast.makeText(Demo7Activity.this,"登录",Toast.LENGTH_SHORT).show(); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { Toast.makeText(Demo7Activity.this,"取消",Toast.LENGTH_SHORT).show(); } }); builder.show();
与之相对:ProgressDialog
方式一:new Dialog
final ProgressDialog dialog = new ProgressDialog(this); dialog.show();
方式二:使用静态方式创建并显示,这种进度条只能是圆形条,设置title和Message提示内容
ProgressDialog dialog2 = ProgressDialog.show(this, "提示", "正在登陆中");
// 方式三 使用静态方式创建并显示,这种进度条只能是圆形条,这里最后一个参数boolean indeterminate设置是否是不明确的状态 ProgressDialog dialog3 = ProgressDialog .show(this, "提示", "正在登陆中", false);
// 方式四 使用静态方式创建并显示,这种进度条只能是圆形条,这里最后一个参数boolean cancelable 设置是否进度条是可以取消的 ProgressDialog dialog4 = ProgressDialog.show(this, "提示", "正在登陆中", false, true);
// 方式五 使用静态方式创建并显示,这种进度条只能是圆形条,这里最后一个参数 DialogInterface.OnCancelListener // cancelListener用于监听进度条被取消 ProgressDialog dialog5 = ProgressDialog.show(this, "提示", "正在登陆中", true, true, cancelListener);
private OnCancelListener cancelListener = new OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "进度条被取消", Toast.LENGTH_LONG) .show(); } };
ProgressDialog dialog = new ProgressDialog(this); dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置进度条的形式为圆形转动的进度条 dialog.setCancelable(true);// 设置是否可以通过点击Back键取消 dialog.setCanceledOnTouchOutside(false);// 设置在点击Dialog外是否取消Dialog进度条 dialog.setIcon(R.drawable.ic_launcher);// // 设置提示的title的图标,默认是没有的,如果没有设置title的话只设置Icon是不会显示图标的 dialog.setTitle("提示");