ایجاد بازی سوال و جواب در اندروید
چهارشنبه 1 شهریور 1396در این مقاله قصد داریم یک بازی سوال و جواب به شما نشان دهیم مثل بعضی از بازی های معروفی که در جال حاضر در بازار وجود دارد و یک سوال 3 گزینه ای برای شما می گذارد و شما می توانید یک گزینه را انتخاب نمایید و به سوال بعدی بروید و یا اینکه جواب را ببنید آیا درست است یا نه ؟
در این سورس دو تا لایه استفاده شده است به صورت زیر:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/opening_text" /> <Button android:id="@+id/add_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Go to First Question" /> </LinearLayout>
و از 3 تا کلاس تشکیل شده است که یکی از آن ها را اینجا قرار می دهیم:
package com.hiphopjunkie; import java.util.HashMap; import java.util.Map; public class Questions { private int answer_key; //Write questions that are going to be loaded into the text private String first_question = "Hello this is my first question"; private String second_question = "Hello this is my second question"; private String third_question = "Hello this is my third question"; private String fourth_question = "Hello this is my fourth question"; private String fifth_question = "Hello this is my fifth question"; //These are the answers to the questions private String first_answer = "first answer"; private String second_answer = "second answer"; private String third_answer = "third answer"; private String fourth_answer = "fourth answer"; private String fifth_answer = "fifth answer"; //These are fake answers to the questions private String fake_first_answer = "fake_first answer"; private String fake_second_answer = "fake_second answer"; private String fake_third_answer = "fake_third answer"; private String fake_fourth_answer = "fake_fourth answer"; private String fake_fifth_answer = "fake_fifth answer"; public String loadQuestionsFromQueue(int select_key){ String question; Map questionMap = new HashMap(); questionMap.put(0, first_question); questionMap.put(1, second_question); questionMap.put(2, third_question); questionMap.put(3, fourth_question); questionMap.put(4, fifth_question); try{ answer_key = select_key; question = (String)questionMap.get(select_key); return question; }catch(NumberFormatException nFE){ return "Not a number"; } } public String grabAnswer(int n){ return findAnswer(n); } private String findAnswer(int num){ String answer; Map answerMap = new HashMap(); answerMap.put(0, first_answer); answerMap.put(1, second_answer); answerMap.put(2, third_answer); answerMap.put(3, fourth_answer); answerMap.put(4, fifth_answer); if(num < answerMap.size()){ answer = (String)answerMap.get(num); return answer; }else { answer = (String)answerMap.get(0); return answer; } } public String findFakeAnswer(int num){ String answer; Map answerMap = new HashMap(); answerMap.put(0, fake_first_answer); answerMap.put(1, fake_second_answer); answerMap.put(2, fake_third_answer); answerMap.put(3, fake_fourth_answer); answerMap.put(4, fake_fifth_answer); if(num < answerMap.size()){ answer = (String)answerMap.get(num); return answer; }else { answer = (String)answerMap.get(0); return answer; } } }
کلاس مربوط به سوالات که در اینجا قرار گرفته است.
خروجی به صورت زیر است:
- Android
- 2k بازدید
- 3 تشکر