我需要一个有两个选择题各有四个选项的android页面 要有java代码 全部的完整代码
时间: 2024-05-09 18:17:24 浏览: 19
以下是一个简单的Android页面,包含两个选择题,每个问题有四个选项。Java代码也包含在内。
activity_main.xml:
```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/question1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Question 1: What is the capital of France?" />
<RadioGroup
android:id="@+id/choices1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/choice1A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A. Madrid" />
<RadioButton
android:id="@+id/choice1B"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B. Paris" />
<RadioButton
android:id="@+id/choice1C"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C. Rome" />
<RadioButton
android:id="@+id/choice1D"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="D. Berlin" />
</RadioGroup>
<TextView
android:id="@+id/question2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Question 2: What is the largest planet in our solar system?" />
<RadioGroup
android:id="@+id/choices2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/choice2A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A. Earth" />
<RadioButton
android:id="@+id/choice2B"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B. Jupiter" />
<RadioButton
android:id="@+id/choice2C"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C. Mars" />
<RadioButton
android:id="@+id/choice2D"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="D. Venus" />
</RadioGroup>
<Button
android:id="@+id/submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
```
MainActivity.java:
```
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private RadioGroup choices1;
private RadioGroup choices2;
private Button submitButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
choices1 = findViewById(R.id.choices1);
choices2 = findViewById(R.id.choices2);
submitButton = findViewById(R.id.submit_button);
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int correctAnswers = 0;
RadioButton selectedChoice1 = findViewById(choices1.getCheckedRadioButtonId());
if (selectedChoice1 != null && selectedChoice1.getId() == R.id.choice1B) {
correctAnswers++;
}
RadioButton selectedChoice2 = findViewById(choices2.getCheckedRadioButtonId());
if (selectedChoice2 != null && selectedChoice2.getId() == R.id.choice2B) {
correctAnswers++;
}
String toastMessage = "You got " + correctAnswers + " out of 2 questions correct.";
Toast.makeText(MainActivity.this, toastMessage, Toast.LENGTH_SHORT).show();
}
});
}
}
```
请注意,这只是一个示例代码,并且可能需要进行修改以适应您的特定要求。
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)