项目中遇到的问题,先简单说下:
CheckBox:Android中大多用到的就是APP登录时,自动登录,
记住密码,这样单个进行控制比较容易;但当项目中有一块儿是多个值以复选框的形式展现(可以一个一个写,但这里推荐使用GridView,其adapter填充的布局就是一个CheckBox,很方便的控制数据变动),那就需要进行控制取值了。
radiobutton:说到radiobutton,大家首先就会想到radiogroup,用radiogroup包裹radiobutton可以很好地控制单选情况;但是,radiogroup中排列radiobutton要么横向,要么纵向,要是有多个radiobutton并要求每行排两个的话,再用radiogroup就不合适了(因为需要每行都有个radiogroup,每个radiogroup里包含两个横向的radiobutton,并且每行之间是独立的,也就是活每行都可以选一个值,不符合要求,控制起来也挺麻烦),这里还是推荐使用GridView,其adapter填充的布局是一个radiobutton(这样每个radiobutton都可以被选中,也就需要进行取值控制了)。
下面以radiobutton为例:
GridView的adapter:
public class RadioGridAdapter extends BaseAdapter { private Context mContext; private List<String> mDatas; private LayoutInflater mInflater = null; private List<RadioButton> rdlist = new ArrayList<>(); public RadioGridAdapter(Context context, List<String> dataList) { mContext = context; mDatas = dataList; mInflater = LayoutInflater.from(mContext); } @Override public int getCount() { return mDatas.size(); } @Override public Object getItem(int position) { return mDatas.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { final ViewHolder holder; if (convertView == null) { holder = new ViewHolder(); convertView = mInflater.inflate(R.layout.item_radio_gridview, parent, false); holder.radioButton = (RadioButton) convertView.findViewById(R.id.item_radio_gridView_radioButton); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.radioButton.setText(mDatas.get(position)); rdlist.add(holder.radioButton); holder.radioButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.i("---点击了:", position + ""); for (int i = 0; i < rdlist.size(); i++) { rdlist.get(i).setChecked(false); } rdlist.get(position + 1).setChecked(true); } }); return convertView; } private static class ViewHolder { private RadioButton radioButton; } }item布局item_radio_gridview:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="left|center_vertical" android:orientation="horizontal"> <RadioButton android:id="@+id/item_radio_gridView_radioButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:clickable="false" android:text="复选框" android:textColor="@color/black" android:textSize="16sp" /> </LinearLayout>