自定义Dialog(图片,文字说明,单选按钮)----类ListPreference实现(2)

在上一篇 中,只是实现一个perference,但是点击以后没有响应事件,我们可以定义一个一个Dialog,Dialog选项里面需要有图片,文字说明,后面还需要一个单选按钮,所以自己写了一个demo,效果图如下:

        功能的完成是使用Dialog的addView()方法,把一个ListView添加进去。ListView控件里面使用了ImageView和CheckedTextView控件,CheckedTextView是一个提供文字和选择框的控件。如果对于CheckedTextView不熟悉,请自己查下文档,在这里就不在多说。

主要功能代码如下:

  1. public class ListViewActivityextends Activity {  
  2.   
  3.     /** Called when the activity is first created. */  
  4.   
  5.     @Override  
  6.   
  7.     public void onCreate(Bundle savedInstanceState){  
  8.   
  9.         super.onCreate(savedInstanceState);  
  10.   
  11.         setContentView(R.layout.main);  
  12.   
  13.         Button button=(Button)findViewById(R.id.button);  
  14.   
  15.        //获取ListView  
  16.   
  17.         final LayoutInflater factory = LayoutInflater.from(ListViewActivity.this);   
  18.   
  19.                  final View view = factory.inflate(   
  20.   
  21.                          R.layout.listview,null);  
  22.   
  23.         final ListView list = (ListView) view.findViewById(R.id.ListView01);   
  24.   
  25.         //把数据项添加到listItem里面  
  26.   
  27.         ArrayList<HashMap<String,Object>> listItem =newArrayList<HashMap<String, Object>>();   
  28.   
  29.         for(int i=0;i<5;i++)   
  30.   
  31.         {  
  32.   
  33.         if(i==0){  
  34.   
  35.              HashMap<String,Object> map =new HashMap<String,Object>();   
  36.   
  37.                 map.put("ItemImage", R.drawable.checked);  
  38.   
  39.                 map.put("ItemTitle""1");     
  40.   
  41.                 listItem.add(map);  
  42.   
  43.         }else if(i==1){  
  44.   
  45.              HashMap<String,Object> map =new HashMap<String,Object>();   
  46.   
  47.                 map.put("ItemImage", R.drawable.c);   
  48.   
  49.                 map.put("ItemTitle""2");   
  50.   
  51.                 listItem.add(map);  
  52.   
  53.         }else if(i==2){  
  54.   
  55.              HashMap<String,Object> map =new HashMap<String,Object>();   
  56.   
  57.                 map.put("ItemImage", R.drawable.d);   
  58.   
  59.                 map.put("ItemTitle""3");    
  60.   
  61.                 listItem.add(map);  
  62.   
  63.         }else if(i==3){  
  64.   
  65.              HashMap<String,Object> map =new HashMap<String,Object>();   
  66.   
  67.                 map.put("ItemImage", R.drawable.d);   
  68.   
  69.                 map.put("ItemTitle""4");     
  70.   
  71.                 listItem.add(map);  
  72.   
  73.         }else{  
  74.   
  75.              HashMap<String,Object> map =new HashMap<String,Object>();   
  76.   
  77.                 map.put("ItemImage", R.drawable.e);   
  78.   
  79.                 map.put("ItemTitle""5");    
  80.   
  81.                 listItem.add(map);  
  82.   
  83.         }  
  84.   
  85.         }  
  86.   
  87.        //获得SimpleAdapter,并且把它添加到listView中  
  88.   
  89.         SimpleAdapter listItemAdapter =new SimpleAdapter(this,listItem,   
  90.   
  91.                R.layout.item,           
  92.   
  93.                new String[] {"ItemImage","ItemTitle"},     
  94.   
  95.                new int[] {R.id.imageView,R.id.checkedTextView}   
  96.   
  97.            );   
  98.   
  99.                 
  100.   
  101.            list.setAdapter(listItemAdapter);   
  102.   
  103.            list.setOnItemClickListener(new OnItemClickListener(){   
  104.   
  105.        
  106.   
  107.                 public void onItemClick(AdapterView<?>arg0, View arg1,int arg2,   
  108.   
  109.                         long arg3) {  
  110.   
  111.               //把所有的单选全部设为非选中  
  112.   
  113.                  for(int i=0;i<arg0.getCount();i++)  
  114.   
  115.                  {  
  116.                     View v = list.getChildAt(i);  
  117.   
  118.                     CheckedTextViewcheckText=(CheckedTextView)v.findViewById(R.id.checkedTextView);  
  119.   
  120.                     checkText.setChecked(false);  
  121.   
  122.                  }  
  123.               //获得点击项的CheckedTextView,并设为选中  
  124.                  CheckedTextViewcheck=(CheckedTextView)arg1.findViewById(R.id.checkedTextView);             
  125.                   check.setChecked(true);  
  126.                }   
  127.            });  
  128.   
  129.            final AlertDialog.Builder builder=new AlertDialog.Builder(ListViewActivity.this);  
  130.            button.setOnClickListener(new View.OnClickListener() {  
  131.               public void onClick(View v) {                 
  132.                   builder.setTitle("Dialog");  
  133.                   builder.setView(list);  
  134.                builder.setNegativeButton("cencel",null);  
  135.                builder.create().show();                       
  136.               }  
  137.            });  
  138.     }  
  139. }  


其中item.xml代码如下

  1. <?xml version="1.0"encoding="utf-8"?>  
  2.   
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   
  5.     android:orientation="horizontal"  
  6.   
  7.     android:layout_width="fill_parent"  
  8.   
  9.     android:layout_height="fill_parent"  
  10.   
  11.     >  
  12.   
  13.     <ImageView  
  14.   
  15.        android:id="@+id/imageView"   
  16.   
  17.         android:layout_width="wrap_content"  
  18.   
  19.         android:layout_height="wrap_content"  
  20.   
  21.         />  
  22.   
  23.     <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"  
  24.   
  25.     android:id="@+id/checkedTextView"  
  26.   
  27.     android:layout_width="match_parent"  
  28.   
  29.         android:layout_height="?android:attr/listPreferredItemHeight"  
  30.   
  31.     android:textAppearance="?android:attr/textAppearanceLarge"  
  32.   
  33.     android:gravity="center_vertical"  
  34.   
  35.     android:checkMark="?android:attr/listChoiceIndicatorSingle"  
  36.   
  37.     android:paddingLeft="6dip"  
  38.   
  39.     android:paddingRight="6dip"  
  40.   
  41. />  
  42.   
  43. </LinearLayout>  

Listview.xml文件如下

  1. <?xml version="1.0"encoding="utf-8"?>     
  2.   
  3. <ListView xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   
  5.          android:layout_width="wrap_content"    
  6.   
  7.           android:layout_height="wrap_content"    
  8.   
  9.           android:id="@+id/ListView01"   
  10.   
  11.           />   

应该特别注意listview.xml不要把他写在一个父控件下如:LinearLayout等,如果这样会出现错误,。还有就是如果你listview添加过多选项,当单击的时候会出现空指针异常。

另外,Demo源代码可以在此下载。

http://download.csdn.net/source/3494251


2012年7月19号补充

           当listView中含有超过一屏幕的时候,采用以上方法会有空指针异常,是因为未显示的View为空,但是上面代码中要遍历所有代码,所以为空。如何优化,可以参考

ListView Item 选择问题解决之道

这篇文章。

posted on 2014-09-03 15:39  小尾巴猴子  阅读(227)  评论(0编辑  收藏  举报

导航