android 应用之listview添加radiobutton,获取textView

来源:互联网 发布:iphone剪辑音乐软件 编辑:程序博客网 时间:2024/05/23 22:46

程序效果:

点击一整行,更换radiobutton选择。

xml代码:

[java] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/layout"  
  4.     android:orientation="horizontal"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="fill_parent"  
  7.     >  
  8.    <TextView  android:id="@+id/list_text"  
  9.     android:layout_width="wrap_content"   
  10.     android:layout_height="wrap_content"   
  11.     android:layout_centerVertical="true"  
  12.     />  
  13.    <ImageView android:id="@+id/list_radioImg"  
  14.     android:layout_width="wrap_content"  
  15.     android:layout_height="wrap_content"  
  16.     android:layout_alignParentRight="true"/>  
  17. </RelativeLayout>  

 

java代码:

[java] view plaincopy
  1. import java.util.ArrayList;  
  2. import java.util.HashMap;  
  3. import java.util.List;  
  4. import java.util.Map;  
  5. import android.app.ListActivity;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.widget.ListView;  
  9. import android.widget.SimpleAdapter;  
  10. import android.widget.Toast;  
  11. public class listRadioBtn extends ListActivity {  
  12.     /** Called when the activity is first created. */  
  13.     private int balanceIndex = 0;  
  14.     SimpleAdapter adapter;  
  15.     List<Map<String, Object>> list;  
  16.       
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.           
  21.         adapter= new SimpleAdapter(this,getData(),R.layout.main,new String[]{"text","img"},new int[]{R.id.list_text,R.id.list_radioImg});   
  22.           
  23.         setListAdapter(adapter);  
  24.     }  
  25.           
  26.         private List<Map<String, Object>> getData(){  
  27.              list = new ArrayList<Map<String, Object>>();  
  28.             Map<String, Object> map_day = new HashMap<String, Object>();   
  29.             map_day.put("text""白天");    
  30.             map_day.put("img", R.drawable.setlist_radio_on);              
  31.             list.add(map_day);  
  32.               
  33.             Map<String, Object> map_clody = new HashMap<String, Object>();   
  34.             map_clody.put("text""阴天");    
  35.             map_clody.put("img", R.drawable.setlist_radio_off);               
  36.             list.add(map_clody);   
  37.               
  38.             Map<String, Object> map_clo = new HashMap<String, Object>();   
  39.             map_clo.put("text""微风");    
  40.             map_clo.put("img", R.drawable.setlist_radio_off);             
  41.             list.add(map_clo);   
  42.               
  43.             return list;  
  44.         }  
  45.           
  46.         protected void onListItemClick(ListView arg0, View arg1, int arg2, long arg3) {  
  47.          Toast t = Toast.makeText(this""+list.get(arg2).get("text"), Toast.LENGTH_LONG);  
  48.          t.show();  
  49.             
  50.              ChangeRadioImg(balanceIndex,false);  
  51.              ChangeRadioImg(arg2,true);  
  52.              balanceIndex=arg2;     
  53.                
  54.              list.get(arg2).get("text");  
  55.          }  
  56.           
  57.         private void ChangeRadioImg(int selectedItem, boolean b) {  
  58.             SimpleAdapter la = adapter;   
  59.             HashMap<String, Object> map = (HashMap<String, Object>)la.getItem(selectedItem);    
  60.             if(b)  
  61.                 map.put("img", R.drawable.setlist_radio_on);  
  62.             else  
  63.                 map.put("img", R.drawable.setlist_radio_off);  
  64.            la.notifyDataSetChanged();  
  65.               
  66.         }  
  67.          
  68. }  

 

另一个简单办法:

android系统中,提供了这样的方法

mylist.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

 

程序主代码:

[java] view plaincopy
  1. protected void onCreate(Bundle savedInstanceState) {  
  2.         // TODO Auto-generated method stub  
  3.         super.onCreate(savedInstanceState);  
  4.         setContentView(R.layout.list_layout);  
  5.         contentString = new String[] {   
  6.                 "示例""透明动画",  
  7.                 "伸缩动画""移动动画",  
  8.                 "旋转动画""透明_伸缩",  
  9.                 "透明_移动""透明_旋转"  
  10.                   
  11.     };  
  12.         arrayAdapter = new ArrayAdapter<String>(this,  
  13.                 android.R.layout.simple_list_item_single_choice,  
  14.                 contentString);  
  15.         mylist = (ListView) findViewById(R.id.ListView01);  
  16.         mylist.setAdapter(arrayAdapter);  
  17.         mylist.setOnItemClickListener(this);  
  18.         mylist.setOnItemSelectedListener(this);  
  19.         mylist.setChoiceMode(ListView.CHOICE_MODE_SINGLE);  
  20.         mylist.setItemChecked(0true);  
  21.     }  

其中,android.R.layout.simple_list_item_single_choice在framework/base/core/res/res/layout目录下,可参见源码

 

 

三 多选框

[java] view plaincopy
  1. import android.app.Activity;  
  2. import android.os.Bundle;  
  3. import android.view.View;  
  4. import android.widget.AdapterView;  
  5. import android.widget.AdapterView.OnItemClickListener;  
  6. import android.widget.AdapterView.OnItemSelectedListener;  
  7. import android.widget.ArrayAdapter;  
  8. import android.widget.ListView;  
  9. public class ListCheckbox extends Activity implements OnItemClickListener,OnItemSelectedListener{  
  10.     private String contentString[];  
  11.     ArrayAdapter arrayAdapter;  
  12.     ListView mylist;  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         // TODO Auto-generated method stub  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.main);  
  17.         contentString = new String[] {   
  18.                 "示例""透明动画",  
  19.                 "伸缩动画""移动动画",  
  20.                 "旋转动画""透明_伸缩",  
  21.                 "透明_移动""透明_旋转"  
  22.                   
  23.     };  
  24.         arrayAdapter = new ArrayAdapter<String>(this,  
  25.                 android.R.layout.simple_list_item_multiple_choice,//.simple_list_item_single_choice,  
  26.                 contentString);  
  27.         mylist = (ListView) findViewById(R.id.ListView01);  
  28.         mylist.setAdapter(arrayAdapter);  
  29.         mylist.setOnItemClickListener(this);  
  30.         mylist.setOnItemSelectedListener(this);  
  31.         mylist.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);//.CHOICE_MODE_SINGLE);  
  32.         mylist.setItemChecked(0true);  
  33.     }  
  34.     public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {  
  35.         mylist.setItemChecked(arg2, true);  
  36.           
  37.     }  
  38.     public void onNothingSelected(AdapterView<?> arg0) {  
  39.         // TODO Auto-generated method stub  
  40.           
  41.     }  
  42.     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {  
  43.         // TODO Auto-generated method stub  
  44.           
  45.     }      
  46. }  

 

main.xml

[xhtml] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <ListView android:id="@+id/ListView01"   
  8. android:layout_width="fill_parent"  
  9.  android:layout_height="fill_parent"/>  
  10. </LinearLayout>  

 

0 0