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

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

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

主要功能代码如下:

public class ListViewActivityextends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button=(Button)findViewById(R.id.button); //获取ListView final LayoutInflater factory = LayoutInflater.from(ListViewActivity.this); final View view = factory.inflate( R.layout.listview,null); final ListView list = (ListView) view.findViewById(R.id.ListView01); //把数据项添加到listItem里面 ArrayList<HashMap<String,Object>> listItem =newArrayList<HashMap<String, Object>>(); for(int i=0;i<5;i++) { if(i==0){ HashMap<String,Object> map =new HashMap<String,Object>(); map.put("ItemImage", R.drawable.checked); map.put("ItemTitle", "1"); listItem.add(map); }else if(i==1){ HashMap<String,Object> map =new HashMap<String,Object>(); map.put("ItemImage", R.drawable.c); map.put("ItemTitle", "2"); listItem.add(map); }else if(i==2){ HashMap<String,Object> map =new HashMap<String,Object>(); map.put("ItemImage", R.drawable.d); map.put("ItemTitle", "3"); listItem.add(map); }else if(i==3){ HashMap<String,Object> map =new HashMap<String,Object>(); map.put("ItemImage", R.drawable.d); map.put("ItemTitle", "4"); listItem.add(map); }else{ HashMap<String,Object> map =new HashMap<String,Object>(); map.put("ItemImage", R.drawable.e); map.put("ItemTitle", "5"); listItem.add(map); } } //获得SimpleAdapter,并且把它添加到listView中 SimpleAdapter listItemAdapter =new SimpleAdapter(this,listItem, R.layout.item, new String[] {"ItemImage","ItemTitle"}, new int[] {R.id.imageView,R.id.checkedTextView} ); list.setAdapter(listItemAdapter); list.setOnItemClickListener(new OnItemClickListener(){ public void onItemClick(AdapterView<?>arg0, View arg1,int arg2, long arg3) { //把所有的单选全部设为非选中 for(int i=0;i<arg0.getCount();i++) { View v = list.getChildAt(i); CheckedTextViewcheckText=(CheckedTextView)v.findViewById(R.id.checkedTextView); checkText.setChecked(false); } //获得点击项的CheckedTextView,并设为选中 CheckedTextViewcheck=(CheckedTextView)arg1.findViewById(R.id.checkedTextView); check.setChecked(true); } }); final AlertDialog.Builder builder=new AlertDialog.Builder(ListViewActivity.this); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { builder.setTitle("Dialog"); builder.setView(list); builder.setNegativeButton("cencel",null); builder.create().show(); } }); } }


其中item.xml代码如下

<?xml version="1.0"encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/checkedTextView" android:layout_width="match_parent" android:layout_height="?android:attr/listPreferredItemHeight" android:textAppearance="?android:attr/textAppearanceLarge" android:gravity="center_vertical" android:checkMark="?android:attr/listChoiceIndicatorSingle" android:paddingLeft="6dip" android:paddingRight="6dip" /> </LinearLayout>

Listview.xml文件如下

<?xml version="1.0"encoding="utf-8"?> <ListView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ListView01" />

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

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

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