首页 > 代码库 > android中的常见对话框

android中的常见对话框

在android中对话框是一种常见的操作,常见的对话框有下面几种:

技术分享

以下是xml布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="click1"
        android:text="显示通知对话框" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="click2"
        android:text="显示单选对话框" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="click3"
        android:text="显示多选对话框" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="click4"
        android:text="显示运行进度对话框" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="click5"
        android:text="显示运行进度条对话框" />

</LinearLayout>

实现代码:

显示对话框

	public void click1(View view) {
		AlertDialog.Builder builder = new Builder(this);
		builder.setTitle("显示对话框");
		builder.setIcon(R.drawable.ic_launcher);
		builder.setMessage("你确定吗?");
		builder.setPositiveButton("确定", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(MainActivity.this, "确定被点击了", Toast.LENGTH_LONG)
						.show();

			}
		});
		builder.setNegativeButton("取消", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub

			}
		});
		builder.show();
	}

显示效果

技术分享

显示单选对话框

	public void click2(View view) {
		AlertDialog.Builder builder = new Builder(this);
		builder.setTitle("单选对话框");
		final String[] items = new String[] { "条目1", "条目2", "条目3" };
		builder.setSingleChoiceItems(items, 1, new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				Toast.makeText(MainActivity.this, items[which] + "被选中了",
						Toast.LENGTH_LONG).show();
				dialog.dismiss();
			}
		});
		builder.show();
	}

显示效果

技术分享

显示多选对话框

	public void click3(View view) {
		AlertDialog.Builder builder = new Builder(this);
		builder.setTitle("显示多选对话框");
		final String[] items = new String[] { "条目1", "条目2", "条目3", "条目4" };
		builder.setMultiChoiceItems(items, new boolean[] { true, false, false,
				true }, new OnMultiChoiceClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which,
					boolean isChecked) {
				// TODO Auto-generated method stub
				Toast.makeText(MainActivity.this, items[which] + isChecked,
						Toast.LENGTH_LONG).show();

			}
		});
		builder.setNegativeButton("取消", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub

			}
		});
		builder.show();
	}

显示效果

技术分享

显示运行进度对话框

	public void click4(View view) {
		ProgressDialog dialog = new ProgressDialog(this);
		dialog.setTitle("提醒");
		dialog.setMessage("正在上传中");
		dialog.show();
	}

显示效果

技术分享

显示运行进度条对话框

	public void click5(View view) {
		final ProgressDialog dialog = new ProgressDialog(this);
		dialog.setTitle("载入进度");
		dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		dialog.setMax(100);
		dialog.show();
		new Thread() {
			public void run() {
				for (int i = 0; i < 100; i++) {
					dialog.setProgress(i);
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				dialog.dismiss();
			};
		}.start();
	}

显示效果

技术分享

最后补充一点假设是自己定义dialog的话假设你用的是

dialog.setContentView(R.layout.dialog_wait);那么就没有什么问题。

假设你是通过inflater来创建一个view

 dialog.setContentView(view)

这样的方式来设置view的话。会遇到设置的dialog大小无效,view占满整个屏幕的情况。

这个时候就须要在代码中设置view的大小。

 WindowManager.LayoutParams params=dialog.getWindow().getAttributes();
        params.width=400;
        params.height=400;
        dialog.getWindow().setAttributes(params);


自己定义对话框显示在底部

看到非常多应用在分享到第三方平台的时候。屏幕底部显示一个对话框

技术分享

这样就须要自己定义Dialog的属性了,让dialog显示在屏幕的底部。

    public static void showShareDialog(final Activity activity) {
        final AlertDialog dialog = new AlertDialog.Builder(activity).create();
        dialog.show();


        View view = View.inflate(activity, R.layout.view_dialog_share, null);
        TextView tvWeixinFriend = (TextView) view.findViewById(R.id.share_wexin);
        tvWeixinFriend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                ShareManager.getInstance().shareToWeixinFriend(activity);
            }
        });
        TextView tvWeixinCircle = (TextView) view.findViewById(R.id.share_pyq);
        tvWeixinCircle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                ShareManager.getInstance().shareToWeixinCircle(activity);
            }
        });
        TextView tvWeibo = (TextView) view.findViewById(R.id.share_weibo);
        tvWeibo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                ShareManager.getInstance().shareToWeibo(activity);
            }
        });
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
        lp.gravity = Gravity.BOTTOM;
        dialog.getWindow().setAttributes(lp);
        dialog.setContentView(view);
        
    }

以下是

view_dialog_share
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="@color/white"
    android:orientation="horizontal"
    android:paddingBottom="16dp"
    android:paddingTop="16dp">

    <TextView
        android:id="@+id/share_wexin"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:drawablePadding="4dp"
        android:drawableTop="@drawable/icon_share_weixin_friend"
        android:gravity="center_horizontal"
        android:text="微信好友" />

    <TextView
        android:id="@+id/share_pyq"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:drawablePadding="4dp"
        android:drawableTop="@drawable/icon_share_weixin_circle"
        android:gravity="center_horizontal"
        android:text="朋友圈" />

    <TextView
        android:id="@+id/share_weibo"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:drawablePadding="4dp"
        android:drawableTop="@drawable/icon_share_weibo"
        android:gravity="center_horizontal"
        android:text="微博" />
</LinearLayout>





android中的常见对话框