은 여기 내을 radioGroup/RadioButton 구성 확장 (SoftRadioGroup/SoftRadioButton)입니다. 레이아웃 XML에 RadioGroup이 더 이상 필요하지 않습니다. group이라는 속성으로 RadioButton을 그룹화 할 수 있습니다.
SoftRadioButton :
이
import java.util.HashMap;
import java.util.Random;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.RadioButton;
public class SoftRadioButton extends RadioButton {
private static HashMap<String, SoftRadioGroup> GROUP_MAPPINGS = new HashMap<String, SoftRadioGroup>();
private String mGroupName;
public SoftRadioButton(Context context, AttributeSet attrs) {
super(context, attrs);
addToGroup(attrs);
}
public SoftRadioGroup getRadioGroup() {
return GROUP_MAPPINGS.get(mGroupName);
}
private void addToGroup(AttributeSet attrs) {
for (int i = 0; i < attrs.getAttributeCount(); i++) {
if (attrs.getAttributeName(i).equals("group")) {
String groupName = attrs.getAttributeValue(i);
SoftRadioGroup group;
if ((group = GROUP_MAPPINGS.get(groupName)) != null) {
// RadioGroup already exists
group.addView(this);
setOnClickListener(group);
mGroupName = groupName;
} else {
// this is the first RadioButton in the RadioGroup
group = new SoftRadioGroup();
group.addView(this);
mGroupName = groupName;
setOnClickListener(group);
GROUP_MAPPINGS.put(groupName, group);
}
return;
}
}
// group is not specified in the layout xml. Let's generate a random
// RadioGroup
SoftRadioGroup group = new SoftRadioGroup();
group.addView(this);
Random rn = new Random();
String groupName;
do {
groupName = Integer.toString(rn.nextInt());
} while (GROUP_MAPPINGS.containsKey(groupName));
GROUP_MAPPINGS.put(groupName, group);
mGroupName = groupName;
setOnClickListener(group);
}
}
SoftRadioGroup :
이
import java.util.ArrayList;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
public class SoftRadioGroup implements OnClickListener {
private ArrayList<RadioButton> buttons = new ArrayList<RadioButton>();
public void addView(RadioButton button) {
buttons.add(button);
}
@Override
public void onClick(View v) {
for (RadioButton button : buttons) {
button.setChecked(false);
}
RadioButton button = (RadioButton) v;
button.setChecked(true);
}
public RadioButton getCheckedRadioButton() {
for (RadioButton button : buttons) {
if (button.isSelected())
return button;
}
return null;
}
public int getChildCount() {
return buttons.size();
}
public RadioButton getChildAt(int i) {
return buttons.get(i);
}
public void check(SoftRadioButton button) {
if (buttons.contains(button)) {
for (RadioButton b : buttons) {
b.setChecked(false);
}
}
}
}
(그룹 당 2 개 버튼 2 그룹)을 테이블에 포함 된 XML 레이아웃 사용법 :
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Your.Package.SoftRadioButton
android:id="@+id/filterActivity_RadioButton_byDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:contentDescription="date"
android:text="@string/filterActivity_RadioButton_byDate"
fake:group="orderBy" />
<Your.Package.SoftRadioButton
android:id="@+id/filterActivity_RadioButton_byPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="price"
android:text="@string/filterActivity_RadioButton_byPrice"
fake:group="orderBy" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Your.Package.SoftRadioButton
android:id="@+id/filterActivity_RadioButton_asc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="down"
android:text="@string/filterActivity_RadioButton_asc"
fake:group="direction" />
<Your.Package.SoftRadioButton
android:id="@+id/filterActivity_RadioButton_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:contentDescription="up"
android:text="@string/filterActivity_RadioButton_desc"
fake:group="direction" />
</TableRow>
</TableLayout>
그래서 더있다 테이블로 버튼을 구성하고 라디오 그룹에 참여시키는 방법은? – Fizz
'RadioButton' 위젯이'RadioGroup'의 직계 하위 항목 인 경우에만. 따라서,'RadioButton' 위젯은 테이블의 단일 셀에있는 행이나 열에 있어야합니다. – CommonsWare
각 라디오 버튼을 프로그래밍 방식으로 그룹에 할당하면 어떨까요? – nobalG