效果:单击应用的TextView文字区就可以跳转到下一道题。
步骤:根据Next按钮监听器的添加(34页的代码清单2-8和2-9),就可以了。在QuizActivity.java中添加如下代码:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_quiz); mQuestionTextView = (TextView)findViewById(R.id.question_text_view) ; /* int question = mQuestionBank[mCurrentIndex].getQuestion(); mQuestionTextView.setText(question); */ //这里开始 mQuestionTextView.setOnClickListener(new View.OnClickListener() { //开始,P41,为TextView添加监听器 @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) {
效果:与单击Next进入下一道题相反,单击此按钮是,进入上一页。
步骤:同样的,仿照Next按钮的添加就可以。在QuizActivity.java中添加:
这里与Next进入下一页+1不同,返回上一页则是-1,而且由于mCurrentIndex初始值为0,-1后则为-1,那么就会报错了,所以就需要再编辑,在0,即第一页时,返回最大一页。
package com.bignerdranch.android.geoquiz;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class QuizActivity extends Activity {
private Button mTrueButton;
private Button mFalseButton;
//这里开始
private Button mPrevButton;
//这里结束
private Button mNextButton;
private TextView mQuestionTextView;
private TrueFalse[ ] mQuestionBank = new TrueFalse[ ] {
new TrueFalse(R.string.question_oceans, true) ,
new TrueFalse(R.string.question_mideast, false) ,
new TrueFalse(R.string.question_africa, false) ,
new TrueFalse(R.string.question_americas, true) ,
new TrueFalse(R.string.question_asia, true) ,
} ;
private int mCurrentIndex = 0;
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) ;
/* int question = mQuestionBank[mCurrentIndex].getQuestion();
mQuestionTextView.setText(question); */
mQuestionTextView.setOnClickListener(new View.OnClickListener() { //开始,P41,为TextView添加监听器
@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) {
/* Toast.makeText(QuizActivity.this,
R.string.incorrect_toast,
Toast.LENGTH_SHORT ).show( ); */
checkAnswer(true);
}
});
mFalseButton=(Button)findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener( ) {
@Override
public void onClick(View v) {
/* Toast.makeText(QuizActivity.this,
R.string.correct_toast,
Toast.LENGTH_SHORT ).show( ); */
checkAnswer(false);
}
});
//这里开始
mPrevButton= (Button)findViewById(R.id.prev_button); //开始,P42,练习2,增加后退按钮
mPrevButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mCurrentIndex%mQuestionBank.length == 0) {
mCurrentIndex = mQuestionBank.length - 1 ;
} else {
mCurrentIndex = (mCurrentIndex - 1) % mQuestionBank.length;
}
updateQuestion();
}
}); //结束。
//这里结束
mNextButton= (Button)findViewById(R.id.next_button); //引用Next按钮,然后为其设置监听器View.OnClick.Listener
mNextButton.setOnClickListener(new View.OnClickListener() { //该监听器作用是:递增数组索引并相应更新显示TextView的文本内容。
@Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
/* int question = mQuestionBank[mCurrentIndex].getQuestion();
mQuestionTextView.setText(question); */
updateQuestion();
}
});
updateQuestion();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_quiz, menu);
return true;
}
}
接着,修改activity_quiz.xml,用drawableLeft,
最后,在strings.xml中增加:
GeoQuiz
True
False
Correct!
Prev
Next
Incorrect!
Settings
The Pacific Ocean is larger than
the Atlantic Ocean.
The Suez Canal connects the Red Sea
and the Indian Ocean.
The source of the Nile River is in Egypt.
The Amazon River is the longest river
in the Americas.
Lake Baikal is the world\'s oldest and deepest
freshwater lake.
效果:实现前进与后退按钮上只显示指示图标。
方法:需要将普通Button组件替换成ImageButton组件。ImageButton和Button与view间的继承关系如下图所示:
步骤:首先,修改文件activity_quiz.xml,将Button组件替换成ImageButton组件,删除Prev和Next按钮的text以及drawable属性定义,并添加ImageView属性,代码如下:
接着,调整QuizActivity.java中的代码:
package com.bignerdranch.android.geoquiz;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
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 mPrevButton;
private ImageButton mNextButton;
//这里结束
private TextView mQuestionTextView;
private TrueFalse[ ] mQuestionBank = new TrueFalse[ ] {
new TrueFalse(R.string.question_oceans, true) ,
new TrueFalse(R.string.question_mideast, false) ,
new TrueFalse(R.string.question_africa, false) ,
new TrueFalse(R.string.question_americas, true) ,
new TrueFalse(R.string.question_asia, true) ,
} ;
private int mCurrentIndex = 0;
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) ;
/* int question = mQuestionBank[mCurrentIndex].getQuestion();
mQuestionTextView.setText(question); */
mQuestionTextView.setOnClickListener(new View.OnClickListener() { //开始,P41,为TextView添加监听器
@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) {
/* Toast.makeText(QuizActivity.this,
R.string.incorrect_toast,
Toast.LENGTH_SHORT ).show( ); */
checkAnswer(true);
}
});
mFalseButton=(Button)findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener( ) {
@Override
public void onClick(View v) {
/* Toast.makeText(QuizActivity.this,
R.string.correct_toast,
Toast.LENGTH_SHORT ).show( ); */
checkAnswer(false);
}
});
//这里开始,两处Button改成ImageButton
mPrevButton= (ImageButton)findViewById(R.id.prev_button); //开始,P42,练习2,增加后退按钮
mPrevButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mCurrentIndex%mQuestionBank.length == 0) {
mCurrentIndex = mQuestionBank.length - 1 ;
} else {
mCurrentIndex = (mCurrentIndex - 1) % mQuestionBank.length;
}
updateQuestion();
}
}); //结束。
mNextButton= (ImageButton)findViewById(R.id.next_button); //引用Next按钮,然后为其设置监听器View.OnClick.Listener
mNextButton.setOnClickListener(new View.OnClickListener() { //该监听器作用是:递增数组索引并相应更新显示TextView的文本内容。
@Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
/* int question = mQuestionBank[mCurrentIndex].getQuestion();
mQuestionTextView.setText(question); */
updateQuestion();
}
});
updateQuestion();
}
//这里结束
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_quiz, menu);
return true;
}
}
这就好啦~~~
整体代码链接:《Android编程权威指南》第二章练习程序