
根据之前学的AndroID对话框技术,来实现下面一个效果:界面有一个"打开设置对话框"按钮,将d出选择项目的对话框,单击任意列表项,都将关闭该对话框,并通过消息提示框显示选择的列表内容。
效果如图所示:
实现代码:
res/layout/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" androID:ID="@+ID/layout1" > <button androID:ID="@+ID/button1" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="打开设置对话框"/> </linearLayout>
编写用于布局列表项内容的XML布局文件items.xml,在该文件中,采用水平线形布局管理器,并在该布局管理器中添加ImageVIEw组件和一个TextVIEw组件,分别用于显示列表项中的图标和文字。
res/layout/items.xml:
<?xml version="1.0" enCoding="utf-8"?> <linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" > <ImageVIEw androID:ID="@+ID/image" androID:paddingleft="10px" androID:paddingtop="20px" androID:paddingBottom="20px" androID:adjustVIEwBounds="true" androID:maxWIDth="72px" androID:maxHeight="72px" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content"/> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:padding="10px" androID:layout_gravity="center" androID:ID="@+ID/Title"/> </linearLayout>
MainActivity:
package com.example.test; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import androID.app.Activity; import androID.app.AlertDialog; import androID.app.AlertDialog.Builder; import androID.content.DialogInterface; import androID.os.Bundle; import androID.vIEw.VIEw; import androID.vIEw.VIEw.OnClickListener; import androID.Widget.button; import androID.Widget.SimpleAdapter; import androID.Widget.Toast; public class MainActivity extends Activity { @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); int[] imageID=new int[]{R.drawable.in,R.drawable.stop,R.drawable.setting,R.drawable.music,R.drawable.exit}; final String[] Title=new String []{"程序管理","保密设置","安全设置","邮件设置","铃声设置"}; List<Map<String,Object>> ListItems=new ArrayList<Map<String,Object>>(); //通过for循环将图片ID和列表项文字放到map中,并添加到List集合中 for (int i = 0; i < imageID.length; i++) { Map<String,Object> map=new HashMap<String,Object>(); map.put("image",imageID[i]); map.put("Title",Title[i]); ListItems.add(map); } final SimpleAdapter adapter=new SimpleAdapter(this,ListItems,R.layout.item,new String[]{"Title","image"},new int[]{R.ID.Title,R.ID.image}); button button=(button)findVIEwByID(R.ID.button1); button.setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw arg0) { Builder builder=new AlertDialog.Builder(MainActivity.this); builder.setIcon(R.drawable.music);//设置对话框的图标 builder.setTitle("设置:");//设置对话框的标题 //添加列表项 builder.setAdapter(adapter,new DialogInterface.OnClickListener() { @OverrIDe public voID onClick(DialogInterface dialog,int which) { Toast.makeText(MainActivity.this,"您选择了【"+Title[which]+"】",Toast.LENGTH_SHORT).show(); } }); builder.create().show();//创建对话框并显示 } }); } } 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android实现带图标的列表对话框全部内容,希望文章能够帮你解决Android实现带图标的列表对话框所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)