我的单选按钮工作正常,但选择的第二个按钮变得很粘。其他人可以选择,他们切换,但显示屏显示第二个按钮卡住。
请参阅下面的自包含代码示例。我正在使用4.0.3并使用eclipse在模拟器中运行。
感谢
package small.example;
import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
public class SmallActivity extends Activity {
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout main=new LinearLayout(this);
main.setOrientation(LinearLayout.VERTICAL);
int question=1;
button=new Button(this);
button.setText("Make a choice");
main.addView(button);
button.setId(question);
setContentView(main);
int answers=4;
radioButtons=new RadioButton[answers];
RadioGroup radioGroup=addRadioButtonsToGroup(question,radioButtons);
LinearLayout linearLayout=new LinearLayout(this);
linearLayout.addView(radioGroup);
linearLayout.setId(question);
// radioButtons[0].setChecked(true);
main.addView(linearLayout);
}
private RadioGroup addRadioButtonsToGroup(int question,RadioButton[] radioButtons) {
RadioGroup radioGroup=new RadioGroup(this);
for(int i=0;i<radioButtons.length;i++) {
radioButtons[i]=new RadioButton(this);
radioButtons[i].setText("Question "+question+" Choice "+(i+1));
radioButtons[i].setId(i);
radioGroup.addView(radioButtons[i]);
}
radioGroup.setOnCheckedChangeListener(radioGroupOnCheckedChangeListener);
radioGroup.setId(question);
return radioGroup;
}
RadioButton[] radioButtons;
Button button;
RadioGroup.OnCheckedChangeListener radioGroupOnCheckedChangeListener=new RadioGroup.OnCheckedChangeListener(){
public void onCheckedChanged(RadioGroup group,int checkedId) {
int id=checkedId;
button.setText(radioButtons[id].getText());
}
};
}
答案 0 :(得分:0)
似乎违规按钮与radioGroup的id共享一个id,由行引起
radioGroup.setId(question);
为什么这会导致按钮变粘,我不清楚。如果您将该行更改为
radioGroup.setId(radioGroup.getChildCount());
因此给它一个非冲突的id,所有按钮都可以正常工作。