`
trygood
  • 浏览: 79149 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

listview添加radiobutton

阅读更多
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>
  • 大小: 152.6 KB
分享到:
评论

相关推荐

    自定义的ListView结合RadioButton显示对话框列表数据

    在Android开发中,自定义的ListView结合RadioButton显示对话框列表数据是一种常见的用户界面设计,用于在弹出的对话框中展示一系列可选的列表项。这样的设计能够为用户提供清晰的选择方式,同时保持屏幕的整洁。以下...

    带checkbox和radiobutton的listview(实现翻页保存和单选)

    为了在ListView中添加Checkbox和RadioButton,我们需要创建一个自定义的Adapter,例如继承自BaseAdapter或ArrayAdapter。 - 自定义Adapter需要重写`getView()`方法,该方法负责为ListView的每个条目生成视图。在...

    listview 里面加radiobutton 单选操作

    为了实现用户只能选择一个条目的功能,我们经常会在ListView的每一项中添加RadioButton。本文将详细讲解如何在ListView中实现RadioButton的单选操作,并提及与GridView的相似性。 首先,我们需要了解ListView的基本...

    Android开发中动态向ListView中添加RadioButton和CheckBox以及动态改变其Checked状态改变

    1、这个主要是针对于考试类程序用的,比如其中如何向ListView中动态添加多个RadioButton单选题选项以及多个CheckBox多选题选项,另外,多个填空题的话,也可以用类似的方法整。 2、解决ListView中添加Button、...

    ListViewAndRadioButton

    在这个方法中,为每一项ListView项添加RadioButton,并处理选中状态。 - 设置单选行为:由于ListView的复用机制,不能直接在RadioButton中使用RadioGroup,否则可能导致选中状态混乱。需要在Adapter中记录当前选中...

    android listview radiobutton

    在处理用户交互时,我们有时需要在ListView的每一项中添加选择功能。在本案例中,我们需要将原本的多选框(CheckBox)替换为单选按钮(RadioButton),以实现列表中的单选效果。以下是对这个主题的详细讲解: 1. **...

    listview添加多种类型items,可用于创建调查问卷,或者设计简单考题

    这个项目专注于在ListView中添加多种类型的items,使得开发者可以构建出具有丰富交互性的应用,例如创建调查问卷或设计简单的考题。以下是对这个项目的详细解读: 首先,我们要了解ListView的工作原理。ListView...

    防止RadioButton在RecyclerView中滑动出现错乱

    - **保存选择状态**:在Adapter的列表数据模型中,为每个item添加一个标识来记录其RadioButton的状态。 - **在onBindViewHolder()中设置状态**:根据数据模型的标识,设置每个RadioButton的checked属性。 - **...

    ListView实现单选多选

    每个ListView的列表项对应一个RadioButton,RadioGroup则可以管理这些RadioButton,确保同一时间只有一个被选中。在ListView的适配器中,我们需要创建包含RadioButton的列表项布局,并在适配器的getView()方法中设置...

    android之listview和checkbox

    2.2 自定义ListView项布局:在XML布局文件中,为ListView的每一项添加CheckBox,并设置相应的ID。确保CheckBox与数据项绑定时,每个CheckBox都有唯一的标识。 2.3 设置选择状态:在getView()方法中,根据数据项的...

    ListView中使用单选和多选效果

    每个ListView项中包含一个RadioButton,RadioGroup可以设置为线性或网格布局,以决定RadioButton的排列方式。 - 在Adapter中,为每个ListView项的View设置一个RadioButton,并在getView()方法中处理选中状态。可以...

    自定义RadioButton

    9. **动态添加RadioButton**:在代码中动态创建和添加RadioButton到RadioGroup,可以提供更大的灵活性,尤其是在选项数量不确定或需要动态加载的情况下。 10. **适配器和RecyclerView**:如果RadioButton是列表视图...

    listview单选实现

    在这个布局文件中,添加一个RadioButton作为选中标志。 ```xml &lt;RadioButton android:id="@+id/radioButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked=...

    android ListView中RadioinButton实现单选.zip

    在某些场景下,我们可能需要在ListView的每一项中添加RadioButton,以便让用户进行单选操作。本篇文章将详细讲解如何在Android的ListView中结合RadioButton实现单选功能。 首先,我们需要了解RadioButton的基本用法...

    Android 系统自带单选按钮的listView

    4. **监听ListView的点击事件**:为了响应用户点击列表项,我们可以为ListView添加一个OnItemClickListener。这将使我们能够获取被选中的项,并执行相应的操作。 ```java listView.setOnItemClickListener(new ...

    完美解决ListView单选和多选的问题

    在getView()方法中,为每个ListView项设置一个RadioButton,并将其添加到一个RadioGroup中。 2. **设置OnCheckedChangeListener**:在Adapter中,为RadioGroup设置一个OnCheckedChangeListener,当RadioButton的...

    listview中的单选按钮多选按钮

    在ListView中集成单选按钮(RadioButton)和多选按钮(CheckBox)是常见的需求,例如在创建选择列表、设置菜单等场景。本篇文章将深入探讨如何在ListView中实现单选与多选功能,并解决复用问题。 首先,我们需要...

    listview_单选多选

    在ListView的适配器中,每个列表项包含一个RadioButton,RadioGroup可以在整个ListView层级进行管理,确保一次只能选中一个项目。当RadioButton的状态改变时,可以通过监听CheckedChangeListener回调更新相应的数据...

    ListView中的item带单选

    在处理ListView中的单选冲突时,主要考虑的是防止同一时间有多个RadioButton被选中。通过在Adapter中维护选中位置,并在点击事件中更新状态,我们可以确保只有一个Item是选中的。同时,我们还需要注意在ListView的...

    listview实现单选功能

    1. **创建RadioButton布局**:为ListView的每一项创建一个XML布局文件,其中包含一个RadioButton。例如,一个名为`list_item_single_choice.xml`的布局文件: ```xml android:layout_width="match_parent" ...

Global site tag (gtag.js) - Google Analytics