大家好,我需要做一个简单的android测验。好吧,我这样做是借助在线其他示例进行的。
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();
}
});
}
}
main.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>
strings.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层)。然后,如果用户答案错误,吐司将向用户回复是不正确的,并显示正确的答案。如果用户是正确的,它将向用户回复正确。所以我需要使用其他方法,但问题是我仍然不熟悉它。如果你们给我指导,我会非常感激,因为我确实需要它。非常感谢:D
您可以通过多种方式执行此操作。...如果您知道正确答案的ID,则可以执行以下操作:
if (selectedId==correctId){
//show toast saying it is correct
}
else{
//toast incorrect
}
在kotlin中,您可以使用..
radioAnswerGroup.setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener { group, checkedId ->
val isChecked = radioAnswerButton.isChecked
if (isChecked) {
Log.e("is check","right answer--")
}else{
Log.e("is check","wrong answer")
}
})