我有7个复选框,在选中一个复选框时,应该取消选中
我需要activity
中的多个复选框,其功能与radio button
获取listener
。
答案 0 :(得分:1)
使用Listview进行此
adapter_radio_buttons.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<CheckBox
android:id="@+id/radios"
android:layout_width="wrap_content"
android:text="@string/app_name"
android:button="@drawable/custom_checkbox"
android:padding="10dp"
android:textColor="#000000"
android:layout_height="wrap_content" />
将此适配器设置为Listview
private int selectedPosition = -1;
private class SampleAdapter extends BaseAdapter {
@Override
public int getCount() {
return 7;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout. , parent, false);
}
CheckBox radios = (CheckBox) convertView.findViewById(R.id.radios);
if (selectedPosition == position) {
radios.setChecked(true);
} else {
radios.setChecked(false);
}
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
radios.performClick();
}
});
radios.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (position == selectedPosition) {
selectedPosition = -1;
radios.setChecked(false);
} else {
radios.setChecked(true);
selectedPosition = position;
}
vL_apor_reasonsList.invalidateViews();
}
});
return convertView;
}
}