0

大家好,我需要做一个简单的安卓测验。那么我所做的就是在其他在线示例的帮助下完成这些。

MyOSIActivity.java

package com.mns.mp;


import tp.mns.mp.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MyOSIActivity extends Activity {

    private RadioGroup radioAnswerGroup;
    private RadioButton radioAnswerButton;
    private Button btnSubmit;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        addListenerOnButton();

    }

    public void addListenerOnButton() {

        radioAnswerGroup = (RadioGroup) findViewById(R.id.radioAnswer);
        btnSubmit = (Button) findViewById(R.id.btnSubmit);

        btnSubmit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                // get selected radio button from radioGroup
                int selectedId = radioAnswerGroup.getCheckedRadioButtonId();

                // find the radiobutton by returned id
                radioAnswerButton = (RadioButton) findViewById(selectedId);

                Toast.makeText(MyOSIActivity.this,
                        radioAnswerButton.getText(), Toast.LENGTH_SHORT).show();

            }

        });

    }
}

主要的.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

        <TextView
        android:id="@+id/Qn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:text="@string/Qn_1"
       />

    <RadioGroup
        android:id="@+id/radioAnswer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio_1" 
            android:checked="true" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio_2" />

         <RadioButton
            android:id="@+id/radio3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio_3" />

    </RadioGroup>

    <Button
        android:id="@+id/btnSubmit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_submit" />

</LinearLayout>

字符串.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Hello World, MyAndroidAppActivity!</string>
    <string name="app_name">MyAndroidApp</string>
    <string name="Qn_1"> Question 1: Acknowledgement, Sequencing, and Flow control are characteristics of which OSI layer?</string>
    <string name="radio_1">Layer 2</string>
    <string name="radio_2"> Layer 3</string>
    <string name="radio_3"> Layer 4</string>
    <string name="btn_submit">Submit</string>

</resources>

好的,现在我试图让系统在用户按下提交按钮后将用户答案与正确答案进行比较,在这种情况下它是第 4 层。然后如果用户答案错误,吐司将回复用户它是不正确的并显示正确答案。如果用户是正确的,那么它将正确地回复用户。所以我需要使用 if else 但问题是我仍然不熟悉它。如果你们给我指导,我将不胜感激,因为我真的需要它。非常感谢你:D

4

2 回答 2

0

你可以通过很多方式做到这一点......如果你知道正确答案的ID,你可以这样做:

if (selectedId==correctId){
    //show toast saying it is correct
}
else{
    //toast incorrect
}
于 2012-05-23T11:16:37.627 回答
0

在 kotlin 你可以使用..

radioAnswerGroup.setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener { group, checkedId ->

//radioAnswerButton is your correct radio button id

val isChecked = radioAnswerButton.isChecked

  if (isChecked) {
      Log.e("is check","right answer--")
  }else{
      Log.e("is check","wrong answer")
  }

})
于 2020-03-21T11:17:08.320 回答