根据我的经验,使用这些定制的RadioGroups有点麻烦.我已经为你准备了一些代码,这些代码将是我解决你想要完成的事情的方法.希望这对你有用!
首先,您必须在onCreate()(或创建视图的任何位置)中调用此函数
private void addRadioButtons() {
LinearLayout llGroup = (LinearLayout) findViewById(R.id.linearLayoutGroup);
for(int i=0; i<10; i++){
MyRadioButton mrb = new MyRadioButton(this);
mrb.setText(String.valueOf(i));
llGroup.addView(mrb.getView());
}
}
这堂课应该是
private static class MyRadioButton implements View.OnClickListener{
private ImageView iv;
private TextView tv;
private RadioButton rb;
private View view;
public MyRadioButton(Context context) {
view = View.inflate(context, R.layout.my_radio_button, null);
rb = (RadioButton) view.findViewById(R.id.radioButton1);
tv = (TextView) view.findViewById(R.id.textView1);
iv = (ImageView) view.findViewById(R.id.imageView1);
view.setOnClickListener(this);
rb.setOnCheckedChangeListener(null);
}
public View getView() {
return view;
}
@Override
public void onClick(View v) {
boolean nextState = !rb.isChecked();
LinearLayout lGroup = (LinearLayout)view.getParent();
if(lGroup != null){
int child = lGroup.getChildCount();
for(int i=0; i
//uncheck all
((RadioButton)lGroup.getChildAt(i).findViewById(R.id.radioButton1)).setChecked(false);
}
}
rb.setChecked(nextState);
}
public void setImage(Bitmap b){
iv.setImageBitmap(b);
}
public void setText(String text){
tv.setText(text);
}
public void setChecked(boolean isChecked){
rb.setChecked(isChecked);
}
}
并且xml要膨胀,例如:
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal" >
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />