Ste*_*eve 2 java android button radio-button onclicklistener
我正在开发一个 android 应用程序,其中问卷活动包含每个问题的单选按钮。我设计了线性布局的活动。所以现在我想检查用户是否回答了所有问题。我面临的问题在公共无效 OnClick(View v) 帮助我修改 java 代码。
<RadioGroup
android:id="@+id/Mquestion1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/MQ1_1_rb1" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/MQ1_1_rb2" />
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/MQ1_1_rb3" />
</RadioGroup>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/MQ1_2_view"
android:textAppearance="?android:attr/textAppearanceSmall" />
<RadioGroup
android:id="@+id/Mquestion2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/MQ1_2_rb1" />
<RadioButton
android:id="@+id/radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/MQ1_2_rb2" />
<RadioButton
android:id="@+id/radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/MQ1_2_rb3" />
</RadioGroup>
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/MQ1_3_view"
android:textAppearance="?android:attr/textAppearanceSmall" />
<RadioGroup
android:id="@+id/Mquestion3"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/MQ1_3_rb1" />
<RadioButton
android:id="@+id/radioButton8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/MQ1_3_rb2" />
<RadioButton
android:id="@+id/radioButton9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/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_alignParentLeft="true"
android:layout_alignParentTop="true"
android:onClick="OnClickListener"
android:text="@string/MQ1_next" />
</RelativeLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
这是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
}
});
}
Run Code Online (Sandbox Code Playgroud)
始终建议用户使用 radioGroup 内的单选按钮
像这样 :
<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>
Run Code Online (Sandbox Code Playgroud)
现在,无论何时您想查看对应于哪个广播组选择的答案,您都可以执行以下操作:
RadioGroup question1 = (RadioGroup) findViewById(R.id.question1);
String answer1 = ((RadioButton) findViewById(question1.getCheckedRadioButtonId())).getText().toString();
Run Code Online (Sandbox Code Playgroud)
恭喜!你在这里得到了答案
归档时间: |
|
查看次数: |
9644 次 |
最近记录: |