关于http://stackoverflow.com/questions/2381560/how-to-group-a-3x3-grid-of-radio-buttons

时间:2014-03-04 00:41:08

标签: android radio-button grouping

我尝试在标题中显示的网站上使用作为第一个答案提供的代码。虽然我试图修改它,但我无法使它工作。问题是,当我检查第一个单选按钮以外的单选按钮时,它们都会保持检查状态。

问题是:什么时候调用addView方法?

此外,这是我的代码版本,我希望有人可以告诉我我的错误:

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;
}

2 个答案:

答案 0 :(得分:5)

只需修改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;
        }
    }

}

答案 1 :(得分:0)

我知道这个问题刚问过,但无论如何我想我知道答案.. 您正在经历的问题是因为您有更多的radioButtons没有或相同的ID。您需要为它们设置不同的ID才能使用它们。

希望这会对某人有所帮助。