Android 单选按钮点击事件3行,最后检查最终的单选按钮
我想做的是 我头上有三个单选按钮 说是,不是,娜 然后是3个Roows中的2个单选按钮 我想要的是,如果所有的“是”都在3行中选中 标题“是”已选中 如果在3行中的任一行中选择了“否”,则 标题编号已选中 我该怎么做 我为此制作了3个广播组 请给我一张照片 我试过的是 对于标题行Android 单选按钮点击事件3行,最后检查最终的单选按钮,android,radio-group,Android,Radio Group,我想做的是 我头上有三个单选按钮 说是,不是,娜 然后是3个Roows中的2个单选按钮 我想要的是,如果所有的“是”都在3行中选中 标题“是”已选中 如果在3行中的任一行中选择了“否”,则 标题编号已选中 我该怎么做 我为此制作了3个广播组 请给我一张照片 我试过的是 对于标题行 vctOptionPoint=new Vector<String>(); vctAnswerID=new Vector<String&
vctOptionPoint=new Vector<String>();
vctAnswerID=new Vector<String>();
int iOpCount=3;
RadioGroup rg=new RadioGroup(this);
rg.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
rg.setId(idRadioGroup);
rg.setOrientation(android.widget.RadioGroup.HORIZONTAL);
rg.setPadding(5, 0, 0, 0);
for(int j=0;j<iOpCount;j++){
String sOption="";
if(j==0){
sOption="Yes";
vctOptionPoint.addElement(sQuestionPoints);
}
else if(j==1){
sOption="No";
vctOptionPoint.addElement("0");
}
if(j==2){
sOption="NA";
vctOptionPoint.addElement("0");
}
vctAnswerID.addElement(String.valueOf(j));
RadioButton rb=new RadioButton(this);
rb.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
rb.setText(sOption);
if(sAnswerText.toString().equals(sOption)){
rb.setChecked(true);
}
rb.setId(idRadioButton);
idRadioButton++;
rg.addView(rb);
}
rg.setOnCheckedChangeListener(rb_onCheckedChange);
ll.addView(rg);
vTopionPoint=新向量();
vctAnswerID=新向量();
int iOpCount=3;
放射组rg=新放射组(本);
rg.setLayoutParams(新的LayoutParams(LayoutParams.WRAP_内容,LayoutParams.WRAP_内容));
rg.setId(idRadioGroup);
rg.setOrientation(android.widget.RadioGroup.HORIZONTAL);
rg.setPadding(5,0,0,0);
对于(int j=0;jres/values/strings.xml)(请始终使用strings.xml文件)
你的代码工作到什么程度了?为什么你要使用向量来进行同步
哦,伙计,为什么你要把它复杂化这么多?你可以做同样的事情,让代码更简短,更容易理解…为什么你要直接设置单选按钮ID而不是使用静态的R类引用名?给我一点时间,我会告诉你如果你不介意的话,这不是一个更好的解决方案…谢谢你的帮助,但我会动态地做这件事谢谢你的帮助!!不客气。你可以在initViews
方法的开头替换Java代码的XML布局设计,就这样。总之…你有主意了!
vctOptionPoint2=new Vector<String>();
vctAnswerID2=new Vector<String>();
int iOpCount=2;
RadioGroup rg1=new RadioGroup(this);
rg1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
rg1.setId(idRadioGroup);
rg1.setOrientation(android.widget.RadioGroup.HORIZONTAL);
rg1.setPadding(5, 0, 0, 0);
for(int j=0;j<iOpCount;j++){
String sOption="";
if(j==0){
sOption="Yes";
vctOptionPoint2.addElement(sQuestionPoints);
}
else if(j==1){
sOption="No";
vctOptionPoint2.addElement("0");
}
vctAnswerID2.addElement(String.valueOf(j));
RadioButton rb1=new RadioButton(this);
rb1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
rb1.setText(sOption);
rb1.setChecked(false);
if(sAnswerText.toString().equals(sOption)){
//rb1.setChecked(true);
}
rb1.setId(idRadioButton2);
idRadioButton2++;
rg1.addView(rb1);
}
rg1.setOnCheckedChangeListener(rb_onCheckedChange_New);
OnCheckedChangeListener rb_onCheckedChange_New=new OnCheckedChangeListener(){
public void onCheckedChanged(RadioGroup rg,int arg1){
idRadioButton=4;
idRadioButton2=15;
idRadioButton1=13;
idRadioButton11=14;
int iRGChildCount=rg.getChildCount();
for(int yy=0;yy<iRGChildCount;yy++){
RadioButton rb=(RadioButton)rg.findViewById(idRadioButton);
RadioButton rb1=(RadioButton)rg.findViewById(idRadioButton2);
RadioButton rb2=(RadioButton)rg.findViewById(idRadioButton1);
RadioButton rb3=(RadioButton)rg.findViewById(idRadioButton11);
if((rb1!=null)){
if(rb1.isChecked() && rb2.isChecked() && rb3.isChecked()){
//int iQusPoints=Integer.parseInt((String)vctOptionPoint2.elementAt(idRadioButton-4));
String sOption1=rb1.getText().toString();
String sOption2=rb2.getText().toString();
String sOption3=rb3.getText().toString();
if(sOption1.equals("No") || sOption2.equals("No") || sOption3.equals("No") ){
tvSecCount1.setText("Points : 0\nScored : 0");
}
else if(sOption1.equals("Yes") || sOption2.equals("No") || sOption3.equals("No") ){
int iQusPoints=0;
tvSecCount1.setText("Points : "+sQuestionPoints+"\nScored : "+iQusPoints);
}
else if(sOption1.equals("No") || sOption2.equals("Yes") || sOption3.equals("No") ){
int iQusPoints=0;
tvSecCount1.setText("Points : "+sQuestionPoints+"\nScored : "+iQusPoints);
}
else if(sOption1.equals("No") || sOption2.equals("No") || sOption3.equals("Yes") ){
int iQusPoints=0;
tvSecCount1.setText("Points : "+sQuestionPoints+"\nScored : "+iQusPoints);
}
else if(sOption1.equals("Yes") || sOption2.equals("Yes") || sOption3.equals("No") ){
int iQusPoints=0;
tvSecCount1.setText("Points : "+sQuestionPoints+"\nScored : "+iQusPoints);
}
else if(sOption1.equals("No") || sOption2.equals("Yes") || sOption3.equals("Yes") ){
int iQusPoints=0;
tvSecCount1.setText("Points : "+sQuestionPoints+"\nScored : "+iQusPoints);
}
else{
int iQusPoints=2;
tvSecCount1.setText("Points : "+sQuestionPoints+"\nScored : "+iQusPoints);
if(sOption1.equals("Yes") && sOption2.equals("Yes") && sOption3.equals("Yes") ){
//fnNext();
}
}
}
}
idRadioButton++;
}
}
};
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="yes">Yes</string>
<string name="no">No</string>
<string name="na">NA</string>
</resources>
<RadioGroup
android:id="@+id/radioHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="14dp" >
<RadioButton
android:id="@+id/radioHeaderYes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/yes" />
<RadioButton
android:id="@+id/radioHeaderNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no" />
<RadioButton
android:id="@+id/radioHeaderNA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/na" />
</RadioGroup>
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radio1Yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/yes" />
<RadioButton
android:id="@+id/radio1No"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no" />
</RadioGroup>
<RadioGroup
android:id="@+id/radioGroup2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radio2Yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/yes" />
<RadioButton
android:id="@+id/radio2No"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no" />
</RadioGroup>
<RadioGroup
android:id="@+id/radioGroup3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radio3Yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/yes" />
<RadioButton
android:id="@+id/radio3No"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no" />
</RadioGroup>
public class MainActivity extends Activity {
Boolean[] radioGroups;
RadioGroup radioHeader, radioGroup1, radioGroup2, radioGroup3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroups = new Boolean[] {true, true, true};
initViews();
}
private void initViews() {
radioHeader = (RadioGroup) this.findViewById(R.id.radioHeader);
radioGroup1 = (RadioGroup) this.findViewById(R.id.radioGroup1);
radioGroup1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
radioGroups[0] = checkedId == R.id.radio1Yes;
refreshHeader();
}
});
radioGroup2 = (RadioGroup) this.findViewById(R.id.radioGroup2);
radioGroup2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
radioGroups[1] = checkedId == R.id.radio2Yes;
refreshHeader();
}
});
radioGroup3 = (RadioGroup) this.findViewById(R.id.radioGroup3);
radioGroup3.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
radioGroups[2] = checkedId == R.id.radio3Yes;
refreshHeader();
}
});
}
private void refreshHeader() {
radioHeader.check(radioGroups[0] & radioGroups[1] & radioGroups[2] ? R.id.radioHeaderYes : R.id.radioHeaderNo);
}