html单选按钮分组如何划分,如何分组3x3网格的单选按钮?

在上面的https://stackoverflow.com/a/2383978/5567009回答之后我得到了另一个解决这个问题的方法,我添加了一些其他功能,例如,保存组的状态以及清除检查功能的功能,如收音机组。

import android.content.Context;

import android.os.Parcel;

import android.os.Parcelable;

import android.support.annotation.IdRes;

import android.util.AttributeSet;

import android.view.View;

import android.widget.RadioButton;

import android.widget.TableLayout;

import android.widget.TableRow;

public class RadioGridGroup extends TableLayout implements View.OnClickListener {

private static final String TAG = "ToggleButtonGroupTableLayout";

private int checkedButtonID = -1;

/**

* @param context

*/

public RadioGridGroup(Context context) {

super(context);

// TODO Auto-generated constructor stub

}

/**

* @param context

* @param attrs

*/

public RadioGridGroup(Context context, AttributeSet attrs) {

super(context, attrs);

// TODO Auto-generated constructor stub

}

@Override

public void onClick(View v) {

if (v instanceof RadioButton) {

int id = v.getId();

check(id);

}

}

private void setCheckedStateForView(int viewId, boolean checked) {

View checkedView = findViewById(viewId);

if (checkedView != null && checkedView instanceof RadioButton) {

((RadioButton) checkedView).setChecked(checked);

}

}

/* (non-Javadoc)

* @see android.widget.TableLayout#addView(android.view.View, int, android.view.ViewGroup.LayoutParams)

*/

@Override

public void addView(View child, int index,

android.view.ViewGroup.LayoutParams params) {

super.addView(child, index, params);

setChildrenOnClickListener((TableRow) child);

}

/* (non-Javadoc)

* @see android.widget.TableLayout#addView(android.view.View, android.view.ViewGroup.LayoutParams)

*/

@Override

public void addView(View child, android.view.ViewGroup.LayoutParams params) {

super.addView(child, params);

setChildrenOnClickListener((TableRow) child);

}

private void setChildrenOnClickListener(TableRow tr) {

final int c = tr.getChildCount();

for (int i = 0; i < c; i++) {

final View v = tr.getChildAt(i);

if (v instanceof RadioButton) {

v.setOnClickListener(this);

}

}

}

/**

* @return the checked button Id

*/

public int getCheckedRadioButtonId() {

return checkedButtonID;

}

/**

* Check the id

*

* @param id

*/

public void check(@IdRes int id) {

// don't even bother

if (id != -1 && (id == checkedButtonID)) {

return;

}

if (checkedButtonID != -1) {

setCheckedStateForView(checkedButtonID, false);

}

if (id != -1) {

setCheckedStateForView(id, true);

}

setCheckedId(id);

}

/**

* set the checked button Id

*

* @param id

*/

private void setCheckedId(int id) {

this.checkedButtonID = id;

}

public void clearCheck() {

check(-1);

}

@Override

protected void onRestoreInstanceState(Parcelable state) {

if (!(state instanceof SavedState)) {

super.onRestoreInstanceState(state);

return;

}

SavedState ss = (SavedState) state;

super.onRestoreInstanceState(ss.getSuperState());

this.checkedButtonID = ss.buttonId;

setCheckedStateForView(checkedButtonID, true);

}

@Override

protected Parcelable onSaveInstanceState() {

Parcelable superState = super.onSaveInstanceState();

SavedState savedState = new SavedState(superState);

savedState.buttonId = checkedButtonID;

return savedState;

}

static class SavedState extends BaseSavedState {

int buttonId;

/**

* Constructor used when reading from a parcel. Reads the state of the superclass.

*

* @param source

*/

public SavedState(Parcel source) {

super(source);

buttonId = source.readInt();

}

/**

* Constructor called by derived classes when creating their SavedState objects

*

* @param superState The state of the superclass of this view

*/

public SavedState(Parcelable superState) {

super(superState);

}

@Override

public void writeToParcel(Parcel out, int flags) {

super.writeToParcel(out, flags);

out.writeInt(buttonId);

}

public static final Parcelable.Creator CREATOR =

new Parcelable.Creator() {

public SavedState createFromParcel(Parcel in) {

return new SavedState(in);

}

public SavedState[] newArray(int size) {

return new SavedState[size];

}

};

}

}

并在XML中使用它如下

android:layout_width="wrap_content"

android:layout_height="wrap_content">

android:id="@+id/rad1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Button1" />

android:id="@+id/rad2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Button2" />

android:id="@+id/rad3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Button3" />

android:id="@+id/rad4"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Button4" />

android:id="@+id/rad5"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Button5" />

android:id="@+id/rad6"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Button6" />

android:id="@+id/rad7"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Button7" />

android:id="@+id/rad8"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Button8" />

如有任何其他改进,请发表评论。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值