listview添加radiobutton

来源:互联网 发布:触摸屏软件开发工具 编辑:程序博客网 时间:2024/05/13 01:39
 
android 应用之listview添加radiobutton
程序效果:



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

xml代码:

view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/layout" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    >  
   <TextView  android:id="@+id/list_text" 
    android:layout_width="wrap_content"   
    android:layout_height="wrap_content"   
    android:layout_centerVertical="true" 
    />  
   <ImageView android:id="@+id/list_radioImg" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true"/>  
</RelativeLayout> 
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
   <TextView  android:id="@+id/list_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    />
   <ImageView android:id="@+id/list_radioImg"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignParentRight="true"/>
</RelativeLayout>

java代码:

view plaincopy to clipboardprint?
import java.util.ArrayList;  
import java.util.HashMap;  
import java.util.List;  
import java.util.Map;  
import android.app.ListActivity;  
import android.os.Bundle;  
import android.view.View;  
import android.widget.ListView;  
import android.widget.SimpleAdapter;  
import android.widget.Toast;  
public class listRadioBtn extends ListActivity {  
    /** Called when the activity is first created. */ 
    private int balanceIndex = 0;  
    SimpleAdapter adapter;  
    List<Map<String, Object>> list;  
      
    @Override 
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
          
        adapter= new SimpleAdapter(this,getData(),R.layout.main,new String[]{"text","img"},new int[]{R.id.list_text,R.id.list_radioImg});   
          
        setListAdapter(adapter);  
    }  
          
        private List<Map<String, Object>> getData(){  
             list = new ArrayList<Map<String, Object>>();  
            Map<String, Object> map_day = new HashMap<String, Object>();   
            map_day.put("text", "白天");    
            map_day.put("img", R.drawable.setlist_radio_on);              
            list.add(map_day);  
              
            Map<String, Object> map_clody = new HashMap<String, Object>();   
            map_clody.put("text", "阴天");    
            map_clody.put("img", R.drawable.setlist_radio_off);               
            list.add(map_clody);   
              
            Map<String, Object> map_clo = new HashMap<String, Object>();   
            map_clo.put("text", "微风");    
            map_clo.put("img", R.drawable.setlist_radio_off);             
            list.add(map_clo);   
              
            return list;  
        }  
          
        protected void onListItemClick(ListView arg0, View arg1, int arg2, long arg3) {  
         Toast t = Toast.makeText(this, ""+list.get(arg2).get("text"), Toast.LENGTH_LONG);  
         t.show();  
            
             ChangeRadioImg(balanceIndex,false);  
             ChangeRadioImg(arg2,true);  
             balanceIndex=arg2;     
               
             list.get(arg2).get("text");  
         }  
          
        private void ChangeRadioImg(int selectedItem, boolean b) {  
            SimpleAdapter la = adapter;   
            HashMap<String, Object> map = (HashMap<String, Object>)la.getItem(selectedItem);    
            if(b)  
                map.put("img", R.drawable.setlist_radio_on);  
            else 
                map.put("img", R.drawable.setlist_radio_off);  
           la.notifyDataSetChanged();  
              
        }  
         

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class listRadioBtn extends ListActivity {
    /** Called when the activity is first created. */
private int balanceIndex = 0;
SimpleAdapter adapter;
List<Map<String, Object>> list;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        adapter= new SimpleAdapter(this,getData(),R.layout.main,new String[]{"text","img"},new int[]{R.id.list_text,R.id.list_radioImg});
       
        setListAdapter(adapter);
    }
       
        private List<Map<String, Object>> getData(){
        list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map_day = new HashMap<String, Object>();
        map_day.put("text", "白天"); 
        map_day.put("img", R.drawable.setlist_radio_on);        
        list.add(map_day);
       
        Map<String, Object> map_clody = new HashMap<String, Object>();
        map_clody.put("text", "阴天"); 
        map_clody.put("img", R.drawable.setlist_radio_off);        
        list.add(map_clody);
       
        Map<String, Object> map_clo = new HashMap<String, Object>();
        map_clo.put("text", "微风"); 
        map_clo.put("img", R.drawable.setlist_radio_off);        
        list.add(map_clo);
       
        return list;
        }
       
        protected void onListItemClick(ListView arg0, View arg1, int arg2, long arg3) {
         Toast t = Toast.makeText(this, ""+list.get(arg2).get("text"), Toast.LENGTH_LONG);
         t.show();
         
        ChangeRadioImg(balanceIndex,false);
        ChangeRadioImg(arg2,true);
        balanceIndex=arg2;  
       
        list.get(arg2).get("text");
         }
       
private void ChangeRadioImg(int selectedItem, boolean b) {
SimpleAdapter la = adapter;
        HashMap<String, Object> map = (HashMap<String, Object>)la.getItem(selectedItem); 
        if(b)
        map.put("img", R.drawable.setlist_radio_on);
        else
        map.put("img", R.drawable.setlist_radio_off);
       la.notifyDataSetChanged();

}
      
}



另一个简单办法:

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

mylist.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

程序主代码:

view plaincopy to clipboardprint?
protected void onCreate(Bundle savedInstanceState) {  
        // TODO Auto-generated method stub  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.list_layout);  
        contentString = new String[] {   
                "示例", "透明动画",  
                "伸缩动画", "移动动画",  
                "旋转动画", "透明_伸缩",  
                "透明_移动", "透明_旋转" 
                  
    };  
        arrayAdapter = new ArrayAdapter<String>(this,  
                android.R.layout.simple_list_item_single_choice,  
                contentString);  
        mylist = (ListView) findViewById(R.id.ListView01);  
        mylist.setAdapter(arrayAdapter);  
        mylist.setOnItemClickListener(this);  
        mylist.setOnItemSelectedListener(this);  
        mylist.setChoiceMode(ListView.CHOICE_MODE_SINGLE);  
        mylist.setItemChecked(0, true);  
    } 
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.list_layout);
contentString = new String[] {
"示例", "透明动画",
"伸缩动画", "移动动画",
"旋转动画", "透明_伸缩",
"透明_移动", "透明_旋转"

};
arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice,
contentString);
mylist = (ListView) findViewById(R.id.ListView01);
mylist.setAdapter(arrayAdapter);
mylist.setOnItemClickListener(this);
mylist.setOnItemSelectedListener(this);
mylist.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
mylist.setItemChecked(0, true);
}

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

三 多选框


view plaincopy to clipboardprint?
import android.app.Activity;  
import android.os.Bundle;  
import android.view.View;  
import android.widget.AdapterView;  
import android.widget.AdapterView.OnItemClickListener;  
import android.widget.AdapterView.OnItemSelectedListener;  
import android.widget.ArrayAdapter;  
import android.widget.ListView;  
public class ListCheckbox extends Activity implements OnItemClickListener,OnItemSelectedListener{  
    private String contentString[];  
    ArrayAdapter arrayAdapter;  
    ListView mylist;  
    protected void onCreate(Bundle savedInstanceState) {  
        // TODO Auto-generated method stub  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        contentString = new String[] {   
                "示例", "透明动画",  
                "伸缩动画", "移动动画",  
                "旋转动画", "透明_伸缩",  
                "透明_移动", "透明_旋转" 
                  
    };  
        arrayAdapter = new ArrayAdapter<String>(this,  
                android.R.layout.simple_list_item_multiple_choice,//.simple_list_item_single_choice,  
                contentString);  
        mylist = (ListView) findViewById(R.id.ListView01);  
        mylist.setAdapter(arrayAdapter);  
        mylist.setOnItemClickListener(this);  
        mylist.setOnItemSelectedListener(this);  
        mylist.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);//.CHOICE_MODE_SINGLE);  
        mylist.setItemChecked(0, true);  
    }  
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {  
        mylist.setItemChecked(arg2, true);  
          
    }  
    public void onNothingSelected(AdapterView<?> arg0) {  
        // TODO Auto-generated method stub  
          
    }  
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {  
        // TODO Auto-generated method stub  
          
    }      

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ListCheckbox extends Activity implements OnItemClickListener,OnItemSelectedListener{
    private String contentString[];
    ArrayAdapter arrayAdapter;
    ListView mylist;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
contentString = new String[] {
"示例", "透明动画",
"伸缩动画", "移动动画",
"旋转动画", "透明_伸缩",
"透明_移动", "透明_旋转"

};
arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice,//.simple_list_item_single_choice,
contentString);
mylist = (ListView) findViewById(R.id.ListView01);
mylist.setAdapter(arrayAdapter);
mylist.setOnItemClickListener(this);
mylist.setOnItemSelectedListener(this);
mylist.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);//.CHOICE_MODE_SINGLE);
mylist.setItemChecked(0, true);
}
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
mylist.setItemChecked(arg2, true);

}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub

}   
}

main.xml

view plaincopy to clipboardprint?
<?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" 
    > 
<ListView android:id="@+id/ListView01"   
android:layout_width="fill_parent" 
android:layout_height="fill_parent"/> 
</LinearLayout> 
<?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"
    >
<ListView android:id="@+id/ListView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
原创粉丝点击