加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

在AlertDialog中的item里添加图片,弹出带图片与文字的选项列表框

(2012-02-02 16:35:09)
标签:

alertdialog

带图片与文字

选项列表

分类: Android

用xml布局来实现,不多说,直接贴代码

 

Test4Activity.java 代码:

public class Test4Activity extends Activity {
   
 
 private Button button;
 private TextView textView;
 private ArrayList<HashMap<String, Object>> listItem;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        button = (Button)findViewById(R.id.button);
        textView = (TextView)findViewById(R.id.textView);
        button.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    openAlertDialog(); //打开选项窗口
   }
         
        });
    }
   
    private void openAlertDialog() {
  // TODO Auto-generated method stub
     AlertDialog  ad = new AlertDialog.Builder(Test4Activity.this)
      .setTitle("添加附件")
      .setAdapter(getAdapter(), new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
     // TODO Auto-generated method stub

     //获取选中项的内容
     textView.setText(listItem.get(which).get("ItemManager").toString());
    }
     }).show();
 }
    //获取设配器内容(要显示的效果)
    private ListAdapter getAdapter(){
        listItem = new ArrayList<HashMap<String, Object>>();
        HashMap<String, Object> map1 = new HashMap<String, Object>();
        map1.put("ImageManager", R.drawable.ic_launcher);
        map1.put("ItemManager",  "图片");
        listItem.add(map1);
        HashMap<String, Object> map2 = new HashMap<String, Object>();
        map2.put("ImageManager", R.drawable.ic_launcher);
        map2.put("ItemManager", "音频");
        listItem.add(map2);
        HashMap<String, Object> map3 = new HashMap<String, Object>();
        map3.put("ImageManager", R.drawable.ic_launcher);
        map3.put("ItemManager", "文档");
        listItem.add(map3);
       
        SimpleAdapter listItemAdapter= new SimpleAdapter(this,listItem,R.layout.list_item,
          new String[]{"ImageManager","ItemManager"},
          new int[]{R.id.image,R.id.text});
       return listItemAdapter;
       }

}

main.xml 代码:

<?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"
    android:gravity="center_vertical">

    <TextView android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Button android:id="@+id/button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        />
</LinearLayout>

 

要显示的效果布局 list_item.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="horizontal"
    android:gravity="center_vertical"
    >

    <ImageView android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <TextView android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</LinearLayout>

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有