您好我想在我的广播组中创建一个多行单选按钮。如果我垂直使用收音机组,我的设计很奇怪,看起来不太好。我有7个radio按钮,我想连续显示2个。如果有人知道,请帮助我。
答案 0 :(得分:0)
添加你的xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.example.app"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.example.app.GroupedRadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Option 1"
custom:group="group1" />
<com.example.app.GroupedRadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Option 2"
custom:group="group1" />
<com.example.app.GroupedRadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Option 3"
custom:group="group1" />
<com.example.app.GroupedRadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Option A"
custom:group="group2" />
<com.example.app.GroupedRadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Option B"
custom:group="group2" />
</LinearLayout>
//在attr.xml文件中添加此行
<declare-styleable name="GroupedRadioButton">
<attr name="group" format="string" />
</declare-styleable>
public class RadioGroupedButton extends RadioButton {
private static Map<String, WeakReference<RadioGroupedButton>> buttonMap;
private OnClickListener externalListener;
private String groupName;
static {
buttonMap = new HashMap<String, WeakReference<RadioGroupedButton>>();
}
public RadioGroupedButton(Context context, AttributeSet attrs) {
super(context, attrs);
processAttributes(context, attrs);
setOnClickListener(internalListener, true);
}
private void processAttributes(Context context, AttributeSet attrs) {
TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.GroupedRadioButton);
int attributeCount = attributes.getIndexCount();
for (int i = 0; i < attributeCount; ++i) {
int attr = attributes.getIndex(i);
switch (attr) {
case R.styleable.GroupedRadioButton_group:
this.groupName = attributes.getString(attr);
break;
}
}
attributes.recycle();
}
private OnClickListener internalListener = new OnClickListener() {
@Override
public void onClick(View view) {
processButtonClick(view);
}
};
private void setOnClickListener(OnClickListener listener, boolean internal) {
if (internal)
super.setOnClickListener(internalListener);
else
this.externalListener = listener;
}
@Override
public void setOnClickListener(OnClickListener listener) {
setOnClickListener(listener, false);
}
private void processButtonClick(View view) {
if (!(view instanceof RadioGroupedButton))
return;
RadioGroupedButton clickedButton = (RadioGroupedButton) view;
String groupName = clickedButton.groupName;
WeakReference<RadioGroupedButton> selectedButtonReference = buttonMap.get(groupName);
RadioGroupedButton selectedButton = selectedButtonReference == null ? null
: selectedButtonReference.get();
if (selectedButton != clickedButton) {
if (selectedButton != null)
selectedButton.setChecked(false);
clickedButton.setChecked(true);
buttonMap.put(groupName, new WeakReference<RadioGroupedButton>(clickedButton));
}
if (externalListener != null)
externalListener.onClick(view);
}
}