关于http://stackoverflow.com/questions/2381560/how-to-group-a-3x3-grid-of-radio-buttons [英] Regarding http://stackoverflow.com/questions/2381560/how-to-group-a-3x3-grid-of-radio-buttons
本文介绍了关于http://stackoverflow.com/questions/2381560/how-to-group-a-3x3-grid-of-radio-buttons的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图用作为出现在标题的网站上的第一个答案提供的code。我不能让它工作,尽管我试图修改它。问题是,当我检查比第一个单选按钮以外的单选按钮,它们都保持选中。
问题是:?办时,addView方法被调用
此外,这里是我的版本的code,我希望有人能告诉我我的错误:
公共类ToggleButtonGroupTableLayout扩展TableLayout实现OnClickListener {私有静态最后弦乐TAG =ToggleButtonGroupTableLayout;
私人ToggleButtonGroupTableLayout radGroup;
私人单选activeRadioButton;/ **
* @参数方面
* /
公共ToggleButtonGroupTableLayout(上下文的背景下){
超级(上下文);
// TODO自动生成构造函数存根
}/ **
* @参数方面
* @参数ATTRS
* /
公共ToggleButtonGroupTableLayout(上下文的背景下,ATTRS的AttributeSet){
超(背景下,ATTRS);
// TODO自动生成构造函数存根
}@覆盖
公共无效的onClick(视图v){
最后单选RB =(单选)V;
radGroup =(ToggleButtonGroupTableLayout)findViewById(R.id.radGroup1);
INT activeRadioButtonId = radGroup.getCheckedRadioButtonId(V);
Log.i(TAG,FIRST+ activeRadioButtonId);
//如果(!activeRadioButtonId = -1&放大器;&安培;!activeRadioButton = NULL){
// activeRadioButton.setChecked(假);
//}
rb.setChecked(真);
// activeRadioButton = RB;
如果(!activeRadioButtonId = -1&放大器;&安培;!activeRadioButton = NULL){
activeRadioButton =(单选)findViewById(activeRadioButtonId);
activeRadioButton.setChecked(假);
}
其他
Log.i(TAG,+ activeRadioButton ++ activeRadioButtonId);
}/ *(非Javadoc中)
* @see android.widget.TableLayout#addView(android.view.View,INT,android.view.ViewGroup.LayoutParams)
* /
@覆盖
公共无效addView(查看孩子,INT指数,
android.view.ViewGroup.LayoutParams PARAMS){
super.addView(儿童,指数,则params);
setChildrenOnClickListener((TableLayout)的孩子);
}
/ *(非Javadoc中)
* @see android.widget.TableLayout#addView(android.view.View,android.view.ViewGroup.LayoutParams)
* /
@覆盖
公共无效addView(查看孩子android.view.ViewGroup.LayoutParams PARAMS){
super.addView(孩子,则params);
setChildrenOnClickListener((TableLayout)的孩子);
}
私人无效setChildrenOnClickListener(TableLayout TL){
最终诠释C = tl.getChildCount();
的for(int i = 0;我c为C;我++){
最后的视图V = tl.getChildAt(I)
如果(V的instanceof的TableRow){
最终诠释C1 =((的TableRow)V).getChildCount();
对于(INT J = 0; J< C1; J ++){
查看V1 =((的TableRow)V).getChildAt(J);
如果(V1的instanceof单选)
v1.setOnClickListener(本);
}
}
}
}公众诠释getCheckedRadioButtonId(视图v){
如果(activeRadioButton!= NULL){
返回activeRadioButton.getId();
} 返回-1;
}
解决方案
只需修改setChildrenOnClickListener:
私人无效setChildrenOnClickListener(TR的TableRow){
最终诠释C = tr.getChildCount();
的for(int i = 0;我c为C;我++){
最后的视图V = tr.getChildAt(I)
如果(V的instanceof单选){
v.setOnClickListener(本);
如果(((单选按钮)ⅴ).isChecked())
activeRadioButton =(单选)V;
}
}}
I tried to use the code provided as the first answer on the website that appears in the title. I can't make it work, although I tried to modify it. The problem is that when I check a radio button other than the first radio button, they both stay checked.
Question is: when do the addView methods get called?
Also, here is my version of the code, I hope somebody can show me my mistakes:
public class ToggleButtonGroupTableLayout extends TableLayout implements OnClickListener {
private static final String TAG = "ToggleButtonGroupTableLayout";
private ToggleButtonGroupTableLayout radGroup;
private RadioButton activeRadioButton;
/**
* @param context
*/
public ToggleButtonGroupTableLayout(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
/**
* @param context
* @param attrs
*/
public ToggleButtonGroupTableLayout(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
public void onClick(View v) {
final RadioButton rb = (RadioButton) v;
radGroup = (ToggleButtonGroupTableLayout) findViewById(R.id.radGroup1);
int activeRadioButtonId = radGroup.getCheckedRadioButtonId(v);
Log.i(TAG,"FIRST " + activeRadioButtonId);
// if (activeRadioButtonId != -1 && activeRadioButton != null ) {
// activeRadioButton.setChecked(false);
// }
rb.setChecked(true);
//activeRadioButton = rb;
if (activeRadioButtonId != -1 && activeRadioButton != null ) {
activeRadioButton = (RadioButton) findViewById(activeRadioButtonId);
activeRadioButton.setChecked(false);
}
else
Log.i(TAG,"" + activeRadioButton + " " + activeRadioButtonId);
}
/* (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((TableLayout)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((TableLayout)child);
}
private void setChildrenOnClickListener(TableLayout tl) {
final int c = tl.getChildCount();
for (int i=0; i < c; i++) {
final View v = tl.getChildAt(i);
if ( v instanceof TableRow ) {
final int c1 = ((TableRow) v).getChildCount();
for (int j = 0; j < c1; j++) {
View v1 = ((TableRow) v).getChildAt(j);
if (v1 instanceof RadioButton)
v1.setOnClickListener(this);
}
}
}
}
public int getCheckedRadioButtonId(View v) {
if ( activeRadioButton != null ) {
return activeRadioButton.getId();
}
return -1;
}
解决方案
Just modify setChildrenOnClickListener:
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);
if (((RadioButton) v).isChecked())
activeRadioButton = (RadioButton) v;
}
}
}
这篇关于关于http://stackoverflow.com/questions/2381560/how-to-group-a-3x3-grid-of-radio-buttons的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文