android学习笔记33:自定义对话框

48 篇文章 0 订阅

在android中使用对话框时,可以使用系统自带的对话框,也可以自己定义一个对话框。本程序使用了一个自定义的单选带图片的对话框。单击“请选择您最喜欢的专辑”按钮,就会弹出这个对话框。

定义对话框中单选列表的样式

<?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="horizontal" >

    <!-- 定义一个ImageView,用于作为列表项的一部分。 -->

    <ImageView
        android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp" />
    <!-- 定义一个TextView,用于作为列表项的一部分。 -->

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:paddingLeft="10dp"
        android:textColor="#ff000066"
        android:textSize="16dp" />

</LinearLayout>

重写onCreateDialog方法,定义我们自己的对话框。

public class CustomListDialog extends Activity
{
	final int LIST_DIALOG = 0x113;
	//定义3个列表项的名称
	private String[] names = new String[]
	{ "风筝", "完美的一天", "是时候"};
	//定义3个列表项对应的图标
	private int[] imageIds = new int[]
	{ R.drawable.sunyz_1 , R.drawable.sunyz_6
		, R.drawable.sunyz_7
	};
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Button bn = (Button)findViewById(R.id.bn);
		//为按钮绑定事件监听器
		bn.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View source)
			{
				//显示对话框
				showDialog(LIST_DIALOG);
			}
		});
	}
	//重写onCreateDialog方法创建对话框
	@Override
	public Dialog onCreateDialog(int id, Bundle state)
	{
		//判断需要生成哪种类型的对话框
		switch (id)
		{
			case LIST_DIALOG:
				Builder b = new AlertDialog.Builder(this);
				// 设置对话框的图标
				b.setIcon(R.drawable.tools);
				// 设置对话框的标题
				b.setTitle("单选列表对话框");
				//创建一个List对象,List对象的元素是Map
				List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
				for (int i = 0; i < names.length; i++)
				{
					Map<String, Object> listItem = new HashMap<String, Object>();
					listItem.put("CDs", imageIds[i]);
					listItem.put("CDname", names[i]);
					listItems.add(listItem);
				}
				//创建一个SimpleAdapter
				SimpleAdapter simpleAdapter = new SimpleAdapter(this
					, listItems 
					, R.layout.row
					, new String[]{ "CDname", "CDs" }
					, new int[]{R.id.name , R.id.header});
				
				// 为对话框设置多个列表
				b.setAdapter(simpleAdapter				
					//为列表项的单击事件设置监听器
					, new DialogInterface.OnClickListener()
					{
						@Override
						public void onClick(DialogInterface dialog,
							int which)
						{
							TextView show = (TextView) findViewById(R.id.show);
							// which代表哪个列表项被单击了
							show.setText("您最喜欢的专辑为:" + names[which]);

						}
					});
				// 创建对话框
				return b.create();
		}
		return null;
	}
}



  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值