1. 리스너를 TextView에 추가한다.
2. 이전 버튼을 추가한다.
mCurrentIndex += 2;
mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
3. Button에서 ImageButton으로 변경한다.
QuizActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | package com.bignerdranch.android.geoquiz; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.ImageButton; import android.widget.TextView; import android.widget.Toast; public class QuizActivity extends Activity { private Button mTrueButton; private Button mFalseButton; private ImageButton mNextButton; private ImageButton mPrevButton; private TextView mQuestionTextView; private int mCurrentIndex = 0; private TrueFalse[] mQuestionBank = new TrueFalse[]{ new TrueFalse(R.string.question_africa, false), new TrueFalse(R.string.question_oceans, true), new TrueFalse(R.string.question_asia, true), new TrueFalse(R.string.question_mideast, false) }; private void updateQuestion(){ int question = mQuestionBank[mCurrentIndex].getQuestion(); mQuestionTextView.setText(question); } private void checkAnswer(boolean userPressedTrue){ boolean answerIsTrue = mQuestionBank[mCurrentIndex].isTrueQuestion(); int messageResId = 0; if(userPressedTrue == answerIsTrue) { messageResId = R.string.correct_toast; } else { messageResId = R.string.incorrect_toast; } Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show(); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_quiz); mQuestionTextView = (TextView) findViewById(R.id.question_text_view); mQuestionTextView.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { mCurrentIndex = (mCurrentIndex+1)%mQuestionBank.length; updateQuestion(); } }); mTrueButton = (Button) findViewById(R.id.true_button); mTrueButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { checkAnswer(true); } }); mFalseButton = (Button) findViewById(R.id.false_button); mFalseButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { checkAnswer(false); } }); mNextButton = (ImageButton) findViewById(R.id.next_button); mNextButton.setBackgroundResource(android.R.drawable.ic_media_next); mNextButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length; updateQuestion(); } }); mPrevButton = (ImageButton) findViewById(R.id.prev_button); mPrevButton.setBackgroundResource(android.R.drawable.ic_media_previous); mPrevButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mCurrentIndex += 2; mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length; updateQuestion(); } }); updateQuestion(); } } | cs |
activity_quiz.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="24dp" android:id="@+id/question_text_view" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/true_button" android:text="@string/true_button"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/false_button" android:text="@string/false_button"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/prev_button" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/next_button" /> </LinearLayout> </LinearLayout> | cs |
TrueFalse.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | package com.bignerdranch.android.geoquiz; /** * Created by NAYEON on 2015-06-18. */ public class TrueFalse { private int mQuestion; private boolean mTrueQuestion; public TrueFalse(int question, boolean trueQuestion){ mQuestion = question; mTrueQuestion = trueQuestion; } public int getQuestion() { return mQuestion; } public void setQuestion(int question) { mQuestion = question; } public boolean isTrueQuestion() { return mTrueQuestion; } public void setTrueQuestion(boolean trueQuestion) { mTrueQuestion = trueQuestion; } } | cs |
'Android' 카테고리의 다른 글
매니페스트 (0) | 2015.06.20 |
---|---|
장치 회전과 액티비티의 부활 (0) | 2015.06.19 |
모델-뷰-컨트롤러, MVC (0) | 2015.06.18 |
개발환경에 융통성 심어주기, 멤버변수의 'm'을 인식하라 (0) | 2015.06.18 |
[UI] 버튼을 "동적으로" 생성하고 이벤트리스너 달기 (0) | 2015.06.09 |