ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

对话框详解0

2021-05-19 23:55:34  阅读:214  来源: 互联网

标签:对话框 int DialogInterface AlertDialog 详解 DIALOG new id


对话框是人机交互的重要组成部分,android中使用AlertDialog.Builder类来创建对话框,本文详解了各种对话框的创建方法:

ps:本文采用activity托管的方式来创建对话框,即使用onCreateDialog方法来创建。当调用Activity类的showDialog方法时,系统会调用onCreateDialog方法来返回一个dialog,即showDialog将参数传进onCreateDialog方法。如果使用一般的创建方法,则和程序方法里面的做法相同。





main.xml布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<Button android:id="@+id/btnDeleteFile" android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:text="显示确认对话框" />
	<Button android:id="@+id/btnSimpleList" android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:text="显示简单列表对话框" />
	<Button android:id="@+id/btnSingleChoiceList"
		android:layout_width="fill_parent" android:layout_height="wrap_content"
		android:text="显示单选列表对话框" />
	<Button android:id="@+id/btnMultiChoiceList"
		android:layout_width="fill_parent" android:layout_height="wrap_content"
		android:text="显示多选列表对话框" />
	<Button
	    android:id="@+id/customerDialog"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:text="显示自定义对话框" />

</LinearLayout>

自定义布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户名:"
            android:textSize="20dp" />

        <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密    码:"
            android:textSize="20dp" />

        <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:password="true" />
    </LinearLayout>

</LinearLayout>

主程序:

public class Main extends Activity implements OnClickListener {
	private final int DIALOG_TWO_BUTTON = 1;
	private final int DIALOG_SIMPLE_LIST = 2;
	private final int DIALOG_SINGLE_CHOICE_LIST = 3;
	private final int DIALOG_MULTI_CHOICE_LIST = 4;
	private final int DIALOG_CUSTOMER_DIALOG = 5;
	private ListView lv = null;
	private String[] provinces = new String[] { "辽宁省", "山东省", "河北省", "福建省",
			"广东省", "黑龙江省" };
	//setSingleChoiceItems的响应事件
	private ButtonOnClick buttonOnClick = new ButtonOnClick(1);

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.btnDeleteFile:
			showDialog(DIALOG_TWO_BUTTON);
			break;
		case R.id.btnSimpleList:
			showDialog(DIALOG_SIMPLE_LIST);
			break;
		case R.id.btnSingleChoiceList:
			showDialog(DIALOG_SINGLE_CHOICE_LIST);
			break;
		case R.id.btnMultiChoiceList:
			showDialog(DIALOG_MULTI_CHOICE_LIST);
			break;
		case R.id.customerDialog:
			showDialog(DIALOG_CUSTOMER_DIALOG);
			break;
		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_TWO_BUTTON:
			return new AlertDialog.Builder(this)
					.setIcon(R.drawable.question)
					.setTitle("是否删除文件")
					//三个button  setNeutralButton()
					.setPositiveButton("确定",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
									new AlertDialog.Builder(Main.this)
											.setMessage("文件已经被删除.").create()
											.show();
								}
							})
					.setNegativeButton("取消",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {

									new AlertDialog.Builder(Main.this)
											.setMessage("您已经选择了取消按钮,该文件未被删除.")
											.create().show();
								}
							}).create();

		case DIALOG_SIMPLE_LIST:
			return new AlertDialog.Builder(this).setTitle("选择省份")
					.setItems(provinces, new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							final AlertDialog ad = new AlertDialog.Builder(
									Main.this)
									.setMessage(
											"您已经选择了: " + which + ":"
													+ provinces[which]).show();
							android.os.Handler hander = new android.os.Handler();
							hander.postDelayed(new Runnable() {
								public void run() {
									ad.dismiss();

								}
							}, 5 * 1000);

						}
					}).create();
		case DIALOG_SINGLE_CHOICE_LIST:
			return new AlertDialog.Builder(this).setTitle("选择省份")
					.setSingleChoiceItems(provinces, 1, buttonOnClick)
					.setPositiveButton("确定", buttonOnClick)
					.setNegativeButton("取消", buttonOnClick).create();
		case DIALOG_MULTI_CHOICE_LIST:
			AlertDialog ad = new AlertDialog.Builder(this)
					.setIcon(R.drawable.image)
					.setTitle("选择省份")
					.setMultiChoiceItems(
							provinces,
							new boolean[] { false, true, false, true, false,
									false },
							new DialogInterface.OnMultiChoiceClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton, boolean isChecked) {

								}
							})
					.setPositiveButton("确定",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
									int count = lv.getCount();
									String s = "您选择了:";
									for (int i = 0; i < provinces.length; i++) {

										if (lv.getCheckedItemPositions().get(i))
											s += i
													+ ":"
													+ lv.getAdapter()
															.getItem(i) + "  ";

									}
									if (lv.getCheckedItemPositions().size() > 0) {
										new AlertDialog.Builder(Main.this)
												.setMessage(s).show();
									} else {
										new AlertDialog.Builder(Main.this)
												.setMessage("您未选择任何省份").show();

									}

								}
							}).setNegativeButton("取消", null).create();
			lv = ad.getListView();
			return ad;

		case DIALOG_CUSTOMER_DIALOG:
			LinearLayout loginLayout = (LinearLayout) getLayoutInflater()
					.inflate(R.layout.login, null);
			new AlertDialog.Builder(this)
					.setIcon(R.drawable.image)
					.setTitle("用户登录")
					//通过setView方法设置自定义视图
					.setView(loginLayout)
					.setPositiveButton("登录",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
									// 编写处理用户登录的代码
								}
							})
					.setNegativeButton("取消",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
									// 取消用户登录,退出程序

								}
							}).show();
			break;
		}

		return null;
	}

	private class ButtonOnClick implements DialogInterface.OnClickListener
	{
		private int index;

		public ButtonOnClick(int index)
		{
			this.index = index;
		}

		public void onClick(DialogInterface dialog, int whichButton)
		{
			if (whichButton >= 0)
			{
				index = whichButton;
			}
			else
			{
				if (whichButton == DialogInterface.BUTTON_POSITIVE)
				{
					new AlertDialog.Builder(Main.this).setMessage(
							"您已经选择了: " + index + ":" + provinces[index]).show();
				}
				else if (whichButton == DialogInterface.BUTTON_NEGATIVE)
				{
					new AlertDialog.Builder(Main.this).setMessage("您什么都未选择.")
							.show();
				}
			}
		}
	}
	//调用show方法之前调用
	@Override
	protected void onPrepareDialog(int id, Dialog dialog)
	{
		super.onPrepareDialog(id, dialog);
	}

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Button btnDeleteFile = (Button) findViewById(R.id.btnDeleteFile);
		Button btnSimpleList = (Button) findViewById(R.id.btnSimpleList);
		Button btnSingleChoiceList = (Button) findViewById(R.id.btnSingleChoiceList);
		Button btnMultiChoiceList = (Button) findViewById(R.id.btnMultiChoiceList);
		Button customerDialog = (Button)findViewById(R.id.customerDialog);
		btnDeleteFile.setOnClickListener(this);
		btnSimpleList.setOnClickListener(this);
		btnSingleChoiceList.setOnClickListener(this);
		btnMultiChoiceList.setOnClickListener(this);
		customerDialog.setOnClickListener(this);
	}
}


标签:对话框,int,DialogInterface,AlertDialog,详解,DIALOG,new,id
来源: https://blog.51cto.com/u_9894631/2789287

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有