Java 单个活动中的多个无线组
我正在开发一个android应用程序,其中问卷活动包含每个问题的单选按钮。我已经以线性布局设计了活动。因此,现在我想检查用户是否回答了所有问题。我面临的问题是在public void OnClick(View v)中帮助我修改java代码。Java 单个活动中的多个无线组,java,android,button,radio-button,onclicklistener,Java,Android,Button,Radio Button,Onclicklistener,我正在开发一个android应用程序,其中问卷活动包含每个问题的单选按钮。我已经以线性布局设计了活动。因此,现在我想检查用户是否回答了所有问题。我面临的问题是在public void OnClick(View v)中帮助我修改java代码。 在此处更改xml文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" androi
在此处更改xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RadioGroup
android:id="@+id/radio"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MQ1_1_view"
android:textAppearance="?android:attr/textAppearanceSmall" />
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MQ1_1_rb1" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MQ1_1_rb2" />
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MQ1_1_rb3" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MQ1_2_view"
android:textAppearance="?android:attr/textAppearanceSmall" />
<RadioButton
android:id="@+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MQ1_2_rb1" />
<RadioButton
android:id="@+id/radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MQ1_2_rb2" />
<RadioButton
android:id="@+id/radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MQ1_2_rb3" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MQ1_3_view"
android:textAppearance="?android:attr/textAppearanceSmall" />
<RadioButton
android:id="@+id/radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MQ1_3_rb1" />
<RadioButton
android:id="@+id/radioButton8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MQ1_3_rb2" />
<RadioButton
android:id="@+id/radioButton9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MQ1_3_rb3" />
</RadioGroup>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="44dp" >
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="MQ1_next" />
</RelativeLayout>
并在java类中为radioGroup定义id。始终建议用户在radioGroup中使用单选按钮 像这样:
祝贺你!你在这里得到了答案转到这个链接我在网站出现之前检查了这些网站,它们都显示为两个单选按钮。我的是多个单选按钮,我想通过更改对齐方式对它们进行射线分组。但是如果我只使用一个射线分组,这就足够了吗?正如您在上面的xml.ya中所使用的那样,通过使用它,您的布局不会改变#Sarkar您的xml代码非常有用。你能帮我学习java代码吗?#Steve关于java代码,你在寻求什么样的帮助?使用按钮。在onClick上处理onClick,对每个问题执行以下代码:String answer1=((RadioButton)findViewById(question1.getCheckedRadioButtonId()).getText().toString();在循环之后,将此答案1添加到答案数组中,您将获得用户填写的所有答案。。。将它们与正确答案数组进行比较。。你得分了。。玩得开心,如果你发现这个有用的话,把它标记为答案是的,听起来我明白了,但我不知道我要在代码中实现它到什么程度。我会尽我最大的努力和更新你。无论如何,谢谢你的时间和关注萨卡..萨卡,检查我的帖子我修改了我的java代码。你能帮我弄清楚吗。
public class ManagerQuestionnaire1 extends Activity
{
Button next;
RadioGroup rg1;
RadioGroup rg2;
RadioGroup rg3;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manager_questionnaire1);
final RadioGroup rg1=(RadioGroup)findViewById(R.id.Mquestion1);
final RadioGroup rg2=(RadioGroup)findViewById(R.id.Mquestion2);
final RadioGroup rg3=(RadioGroup)findViewById(R.id.Mquestion3);
Button next=(Button)findViewById(R.id.button1);
next.setOnClickListener(new View.OnClickListener()
{
public void OnClick(View v) \\The error I'm getting here is REMOVE method'OnClick'
{
if((rg1.getCheckedRadioButtonId()!=R.id.radioButton1 || rg1.getCheckedRadioButtonId()!=R.id.radioButton2 || rg1.getCheckedRadioButtonId()!=R.id.radioButton3)||(rg2.getCheckedRadioButtonId()!=R.id.radioButton4 || rg2.getCheckedRadioButtonId()!=R.id.radioButton5 || rg2.getCheckedRadioButtonId()!=R.id.radioButton6)||(rg3.getCheckedRadioButtonId()!=R.id.radioButton7 || rg3.getCheckedRadioButtonId()!=R.id.radioButton8 || rg3.getCheckedRadioButtonId()!=R.id.radioButton9))
{
AlertDialog alert= new AlertDialog.Builder(ManagerQuestionnaire1.this).create();
alert.setTitle("Exception:Complete the Questions");
alert.setMessage("Please ensure all Questions are answered");
}
else
{
Intent intent = new Intent(ManagerQuestionnaire1.this, ManagerQuestionnaire2.class);
startActivity(intent);
}
}
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
}
});
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RadioGroup
android:id="@+id/radio"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MQ1_1_view"
android:textAppearance="?android:attr/textAppearanceSmall" />
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MQ1_1_rb1" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MQ1_1_rb2" />
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MQ1_1_rb3" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MQ1_2_view"
android:textAppearance="?android:attr/textAppearanceSmall" />
<RadioButton
android:id="@+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MQ1_2_rb1" />
<RadioButton
android:id="@+id/radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MQ1_2_rb2" />
<RadioButton
android:id="@+id/radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MQ1_2_rb3" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MQ1_3_view"
android:textAppearance="?android:attr/textAppearanceSmall" />
<RadioButton
android:id="@+id/radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MQ1_3_rb1" />
<RadioButton
android:id="@+id/radioButton8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MQ1_3_rb2" />
<RadioButton
android:id="@+id/radioButton9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MQ1_3_rb3" />
</RadioGroup>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="44dp" >
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="MQ1_next" />
</RelativeLayout>
<RadioGroup
android:id="@+id/question1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="RadioButton" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
</RadioGroup>
<RadioGroup
android:id="@+id/question2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<RadioButton
android:id="@+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="RadioButton" />
<RadioButton
android:id="@+id/radio4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
<RadioButton
android:id="@+id/radio5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
</RadioGroup>
RadioGroup question1 = (RadioGroup) findViewById(R.id.question1);
String answer1 = ((RadioButton) findViewById(question1.getCheckedRadioButtonId())).getText().toString();