本文介绍了关于 http://stackoverflow.com/questions/2381560/how-to-group-a-3x3-grid-of-radio-buttons的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试使用提供的代码作为标题中出现的网站上的第一个答案.尽管我尝试对其进行修改,但我无法使其工作.问题是,当我选中第一个单选按钮以外的单选按钮时,它们都保持选中状态.
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.
问题是:addView 方法什么时候被调用?
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;
}
推荐答案
修改setChildrenOnClickListener即可:
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!