Android学习——Dialog对话框

一、简单的提示信息:

Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ShowDialg("标题", "提示信息", DialogActivity.this);
			}
		});
private void ShowDialg(String strTitle, CharSequence strInfo, Activity activity) {
		Dialog dialog = new AlertDialog.Builder(activity)
		        .setTitle(strTitle) // 设置标题
				.setMessage(strInfo) // 设置对话框中的内容
				.setPositiveButton("确认", new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
                                            // 确认按钮事件处理
					}
				}).create(); // 创建对话框
		dialog.show(); // 显示对话框
	}

二、自定义对话框:

自定义布局dialog_password.xml:

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

    <RelativeLayout
        android:id="@+id/machine_div"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:layout_margin="5dip"
        android:padding="5dip" >

        <TextView
            android:id="@+id/label_dialogpwssword_new"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:text="输入密码:"
            android:textColor="@color/white" />

        <EditText
            android:id="@+id/txt_dialogpwssword_new"
            android:layout_width="300px"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_toRightOf="@+id/label_dialogpwssword_new"
            android:inputType="textPassword"
            android:singleLine="true" />

        <TextView
            android:id="@+id/label_dialogpwssword_new1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/label_dialogpwssword_new"
            android:layout_marginTop="15dp"
            android:text="确认密码:"
            android:textColor="@color/white" />

        <EditText
            android:id="@+id/txt_dialogpwssword_new1"
            android:layout_width="300px"
            android:layout_height="wrap_content"
            android:layout_below="@+id/label_dialogpwssword_new"
            android:layout_marginTop="20dp"
            android:layout_toRightOf="@+id/label_dialogpwssword_new1"
            android:inputType="textPassword"
            android:singleLine="true" />
    </RelativeLayout>

</LinearLayout>

java代码:

private void showDailog() {
		LayoutInflater inflater = getLayoutInflater();
		//指定布局dialog_password
		final View view = inflater.inflate(R.layout.dialog_password, null);
		Dialog dialog = new AlertDialog.Builder(this)
				.setTitle("修改密码")	//设置标题
				.setView(view)	//设置对话框布局
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						// 确定按钮事件处理
					}
				})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						// 取消按钮事件处理
					}
				}).create();
		dialog.show(); // 显示对话框
	}

三、列表对话框:

private void ShowListDialg() {
		final CharSequence[] items = {"Red", "Green", "Blue"};   //定义列表项
		Dialog dialog = new AlertDialog.Builder(this)
		        .setTitle("列表对话框") // 创建标题
				.setIcon(R.drawable.small1) // 设置LOGO
				.setItems(items, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int item) {
						// 单击选项事件处理
						Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
					}
				}).create(); // 创建对话框
		dialog.show(); // 显示对话框
	}

四、单选列表对话框:

private void ShowSingleDialg() {
		final CharSequence[] items = {"Red", "Green", "Blue"};   
		Dialog dialog = new AlertDialog.Builder(this)
		        .setTitle("单选列表对话框") // 创建标题
				.setIcon(R.drawable.small1) // 设置LOGO
				// setSingleChoiceItems方法创建单选列表项,第二个参数为默认被选中的选项位置,"-1"表示默认情况下不选中任何选项。
				.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// 单击选项事件处理
						selectedIndex = which;
						Toast.makeText(DialogActivity.this, items[which], Toast.LENGTH_SHORT).show();
					}
				})
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// 确定按钮事件
						Toast.makeText(DialogActivity.this, items[selectedIndex], Toast.LENGTH_SHORT).show();
					}
				})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// 取消按钮事件
					}
				})
				.create(); // 创建对话框
		dialog.show(); // 显示对话框
	}

五、多选列表对话框:

private void ShowMultiDialg() {
		final CharSequence[] items = { "Red", "Green", "Blue", "White" };
		final boolean[] bolSelected = { false, false, false ,false };
		Dialog dialog = new AlertDialog.Builder(this)
				.setTitle("多选列表对话框")
				// 创建标题
				.setIcon(R.drawable.small1)
				// 设置LOGO
				.setMultiChoiceItems(items, bolSelected,
						new DialogInterface.OnMultiChoiceClickListener() {
					        @Override
							public void onClick(DialogInterface dialog, int which, boolean isChecked) {
					        	// 单击选项事件处理
								bolSelected[which] = isChecked;
							}
						})
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int whichButton) {
						// 确定按钮事件处理
						StringBuilder stringBuilder = new StringBuilder(); 
                        for (int i = 0; i < bolSelected.length; i++) { 
                            if (bolSelected[i] == true) 
                            { 
                                stringBuilder.append(items[i] + "/"); 
                            } 
                        } 
                        Toast.makeText(DialogActivity.this, stringBuilder.toString(), Toast.LENGTH_SHORT).show();
					}
				})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int whichButton) {
						// 取消按钮事件处理
					}
				})
				.create(); // 创建对话框
		dialog.show(); // 显示对话框
	}

六、日期对话框:

private void showDateDialog() {
		txtDate = (TextView) findViewById(R.id.txtDate);
		Calendar  c = Calendar.getInstance();
		Dialog dialog = new DatePickerDialog(this,
				new DatePickerDialog.OnDateSetListener() {
					public void onDateSet(DatePicker dp, int year, int month, int dayOfMonth) {
						Calendar c = Calendar.getInstance();
						// 将当前的设置的时间赋值到文本框中
						c.set(Calendar.YEAR, year);
						c.set(Calendar.MONTH, month);
						c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
						SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
						txtDate.setText(df.format(c.getTime()));
					}
				}, 
				c.get(Calendar.YEAR), // 传入年份
				c.get(Calendar.MONTH), // 传入月份
				c.get(Calendar.DAY_OF_MONTH)); // 传入天数
		dialog.show();
	}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值