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();
	}

显示效果

android中的常见对话框

显示单选对话框

	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();
	}

显示效果

android中的常见对话框

显示多选对话框

	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();
	}

显示效果

android中的常见对话框

显示进度对话框

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

显示效果

android中的常见对话框

显示进度条对话框

	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();
	}

显示效果

android中的常见对话框




android中的常见对话框,布布扣,bubuko.com

android中的常见对话框

上一篇:android之ExpandableListActivity


下一篇:对于框架整合的总结(全注解)