1. 程式人生 > >ListView巢狀RadioButton導致的多選

ListView巢狀RadioButton導致的多選

1、首先ListView獲取點選事件需要設定RadioButton的焦點android:focusable=”false”
2、通過一個List集合管理所有的RadioButton,然後在點選事件中,清空所有的狀態。

private List<RadioButton> raList;
raList = new ArrayList<RadioButton>();
listView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View view, int
item, long arg3) { clearCheck(); RadioButton radioButton = (RadioButton) view.findViewById(R.id.radio_check); radioButton.setChecked(true); } }); /** * 清空所有選中狀態 */ private void clearCheck(){ for(RadioButton radioButton : raList){ radioButton.setChecked(false
); } } public View getView(int position, View convertView, ViewGroup parent) { convertView = inflater.inflate(R.layout.layout_alertdialog_item, null); TextView tv_content = (TextView) convertView.findViewById(R.id.context); tv_content.setText(listContent.get(position)); RadioButton radioButton = (RadioButton) convertView.findViewById(R.id.radio_check); raList.add(radioButton); return
convertView; }

同時需要注意,此時要把RadioButton設定為不可點選。android:clickable=”false”