AlertDialog和AlertDialog.builder(二)

本文详细介绍如何在Android应用中实现列表对话框,包括通过硬编码及XML配置两种方式,并展示了单选及复选框的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、列表对话框。

列表对话框的实现可以使用两种方式,一种可以使用硬编码的方法,另外一种是使用XML配置

第一种的情况需要定义一个显示数组,然后使用AlertDialog进行展示的时候使用数组里面的内容进行显示

首先还是需要在activity_main.xml里面定义两个组件

    <TextView 
        android:id="@+id/showPhone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/show_phone"/>
    <Button 
        android:id="@+id/showPhoneBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/show_phone_btn"/>

接着在MainActivity.java里面定义一个手机品牌的数组

	// 喜欢的手机品牌
	private String[] tel = new String[] { "MOTO", "SAMSUNG", "APPLE", "NOKIA",
			"HTC", "MEIZU" };

以下为详细实现

		// 取得组件
		showPhone = (TextView) findViewById(R.id.showPhone);
		showPhoneBtn = (Button) findViewById(R.id.showPhoneBtn);
		// 设置监听
		showPhoneBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				new AlertDialog.Builder(MainActivity.this)
						.setTitle("请选择您喜欢的品牌").setIcon(R.drawable.dialog)
						// 这里使用setItems(items, listener)方法
						.setItems(tel, new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								showPhone.setText("您喜欢的手机品牌是:" + tel[which]);

							}
						}).show();

			}
		});

需要说明的是,这里使用的是AlertDialog.builder里面的 setItems ( CharSequence[]  items,  DialogInterface.OnClickListener  listener)方法,里面的参数也很简单,不需要再加以赘述。

下图为运行的结果图


选择后的结果




第二种方法是进行XML配置

首先需要建立一个相关的XML文件进行存放数据color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string-array name="color">
        <item>红色</item>
        <item>绿色</item>
        <item>蓝色</item>
    </string-array>

</resources>

然后在activity_main.xml里面定义两个组件

    <TextView
        android:id="@+id/showColor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/show_color" />

    <Button
        android:id="@+id/showColorBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/show_color_btn" />

那么详细的代码实现为下

		// 取得组件
		showColor = (TextView) findViewById(R.id.showColor);
		showColorBtn = (Button) findViewById(R.id.showColorBtn);

		// 设置监听
		showColorBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				new AlertDialog.Builder(MainActivity.this)
						.setTitle("请选择您喜欢的颜色")
						.setIcon(R.drawable.dialog)
						.setItems(R.array.color,
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										// 取得资源的文件需要使用getResources()方法
										showColor
												.setText("您喜欢的颜色是:"
														+ getResources()
																.getStringArray(
																		R.array.color)[which]);
									}
								}).show();
			}
		});

运行结果如下

选择之后的结果



单选以及复选框

复选框的实现

同样的,首先先在activity_main.xml里面定义组件

    <TextView
        android:id="@+id/showBooks"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/show_books" />


    <Button
        android:id="@+id/showBooksBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/show_books" />

然后定义一个显示的信息列表

	// 书籍列表
	private String[] books = new String[] { "Android开发", "thinking in java",
			"android游戏", "SSH实战经典" };
	// 相对应的boolean型(这里的数组内容多少必须跟书籍列表的相同)
	// true表示默认选中,false表示不选中
	private boolean[] booksChoice = new boolean[]{false,false,false,false};

以下为详细的实现代码

		// 取得组件
		showBooksBtn = (Button) findViewById(R.id.showBooksBtn);
		showBooks = (TextView) findViewById(R.id.showBooks);
		// 设置监听
		showBooksBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				new AlertDialog.Builder(MainActivity.this)
						.setTitle("请选择书籍")
						.setIcon(R.drawable.dialog)
						// 复选框
						.setMultiChoiceItems(
								// 列表数组
								books,
								// 是否选中
								booksChoice,
								new DialogInterface.OnMultiChoiceClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which, boolean isChecked) {
										// 循环是否已被选中
										for (int i = 0; i < books.length; i++) {
											// 如果选中则打印
											if (i == which && isChecked) {
												showBooks.append(books[i]+"、");
											}
										}

									}
									// 确定按钮
								}).setPositiveButton("确定", new DialogInterface.OnClickListener() {
									
									@Override
									public void onClick(DialogInterface dialog, int which) {
										
									}
								}).show();
			}
		});

setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, DialogInterface.OnMultiChoiceClickListener listener)方法为设置

复选框,setSingleChoiceItems(CharSequence[] items, int checkedItem, DialogInterface.OnClickListener listener)则为设置单选框的方法

两者的实现均差不多,则不必赘述。

而也可以在确定按钮的时候进行各种操作

下图为运行结果



选择选项之后的结果如下:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值