当前位置: 代码迷 >> Android >> android listview每个item里面有个radiobutton,如何弄radibutton的单选事件
  详细解决方案

android listview每个item里面有个radiobutton,如何弄radibutton的单选事件

热度:100   发布时间:2016-05-01 10:05:43.0
android listview每个item里面有个radiobutton,怎么弄radibutton的单选事件
如题   android listview每个item里面有个radiobutton,怎么弄radibutton的单选事件,点击某个radio的时候它选中,其他的取消,  牛人们,,,请给出具体的代码
  
在网上找的 都没有效果,要么就是只能点击一个不能换成其他的了,要么就是都选上了。。。

final RadioButton mRadioButton = (RadioButton) view
.findViewById(R.id.report_item_check);
mRadioButton.setChecked(false);//先把所有的设置为false

if(mRadioButton.getId()<=position){
mRadioButton.setId(position);
}


mRadioButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//在这儿实现整个listView item 中的radioButton的单选
if(isChecked)
{
if(temp!=-1)
{
RadioButton tempRadio =(RadioButton)view.findViewById(temp);
if(tempRadio !=null)
{
tempRadio.setChecked(false);
}
}
temp = buttonView.getId();
}

}
});
        if (position == mRadioButton.getId()) {
           mRadioButton.setChecked(true);
        } else {
            mRadioButton.setChecked(false);
        }         

// radioButton.setId(position);
// radioButton.setChecked(false);
// radioButton
// .setOnCheckedChangeListener(new OnCheckedChangeListener() {
//
// public void onCheckedChanged(
// CompoundButton buttonView, boolean isChecked) {
// if (radioButton.isChecked()==true) {
// temp=btn_id;
// btn_id = radioButton.getId();
// if (temp != -1 && temp != btn_id) {
// RadioButton tempButton = (RadioButton) view
// .findViewById(temp);
// if (tempButton != null) {
// tempButton.setChecked(false);
// }
// }
// }
//
// }
// });
// if (btn_id == position) {
// radioButton.setChecked(true);
// checked_map=map;
// } else {
// radioButton.setChecked(false);
// }
//
  相关解决方案