سورس کد آزمون چهار گزینه در اندروید استودیو
در اینجا به بررسی سورس کد یک آزمون چهار گزینهای در اندروید استودیو خواهیم پرداخت. این برنامه به شما این امکان را میدهد که سوالات را به صورت چند گزینهای ارائه دهید و نتایج را بررسی کنید.
ساختار پروژه
ابتدا باید یک پروژه جدید در اندروید استودیو ایجاد کنید. برای این کار:
- ایجاد پروژه: ابتدا "New Project" را انتخاب کنید و یک Activity خالی انتخاب کنید.
- افزودن Dependencies: در فایل `build.gradle` (Module: app) به dependencies نیاز دارید، از جمله `RecyclerView` برای نمایش سوالات.
طراحی رابط کاربری
رابط کاربری را با استفاده از XML طراحی کنید. برای مثال:
```xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/question_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioGroup
android:id="@+id/options_group"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/option1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:id="@+id/option2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:id="@+id/option3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:id="@+id/option4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RadioGroup>
<Button
android:id="@+id/submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"/>
</LinearLayout>
```
منطق برنامه
در فایل `MainActivity.java`، باید منطق برنامه را پیادهسازی کنید. سوالات و گزینهها را میتوان در یک آرایه یا لیست تعریف کرد.
```java
public class MainActivity extends AppCompatActivity {
private TextView questionText;
private RadioGroup optionsGroup;
private Button submitButton;
private String[] questions = {"Question 1?", "Question 2?"};
private String[][] options = {{"Option1", "Option2", "Option3", "Option4"}, {"Option1", "Option2", "Option3", "Option4"}};
private int[] answers = {0, 2}; // index of the correct answer
private int currentQuestionIndex = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
questionText = findViewById(R.id.question_text);
optionsGroup = findViewById(R.id.options_group);
submitButton = findViewById(R.id.submit_button);
loadQuestion();
submitButton.setOnClickListener(v -> {
int selectedId = optionsGroup.getCheckedRadioButtonId();
RadioButton selectedOption = findViewById(selectedId);
// check answer and load the next question
});
}
private void loadQuestion() {
questionText.setText(questions[currentQuestionIndex]);
// load options accordingly
}
}
```
جمع بندی
این پروژه به شما این امکان را میدهد که سوالات و گزینهها را به راحتی مدیریت کنید. میتوانید با افزودن ویژگیهای بیشتر، مانند ذخیره نتایج و تجزیه و تحلیل، آن را گسترش دهید. حالا شما یک آزمون چهار گزینهای ساده دارید که میتوانید آن را توسعه دهید و به نیازهای خود تنظیم کنید.