您的位置:首页 > 移动开发 > Android开发

android中的常见对话框

2014-04-19 13:33 155 查看
在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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: