分享一个android端的小例子,代码虽少,五脏俱全。
废话不多说,直接上代码。
我们先看项目的结构。
package ligz.com.geoquiz;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "QuizActivity";
private static final String KEY_INDEX = "index";
private Button mTrueButton;
private Button mFalseButton;
private Button mNextButton;
private TextView mQuestionTextView;
private Button mCheatButtton;//作弊的按钮
private boolean mIsCheater;
private TrueFalse[] mQuestionBank = new TrueFalse[]{
new TrueFalse(R.string.question_text,true),
new TrueFalse(R.string.question_text1,true),
new TrueFalse(R.string.question_text2,true),
new TrueFalse(R.string.question_text3,true),
new TrueFalse(R.string.question_text4,false),
};
private int mCurrentIndex = 0;
private void updateQuestion(){
int question = mQuestionBank[mCurrentIndex].getmQuestion();
mQuestionTextView.setText(question);
}
private void checkAnswer(boolean userPressedTrue){
boolean answerIsTrue = mQuestionBank[mCurrentIndex].ismTrueQuestion();
int messageResId = 0;
if(mIsCheater){
messageResId = R.string.judgment_toast;
}else{
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);
Log.d(TAG, "onCreate(Bundel) called");
setContentView(R.layout.activity_main);
//从数组中拿出问题放入TextView
mQuestionTextView = (TextView)findViewById(R.id.question_text_view);
/**
* 正确按钮
*/
mTrueButton = (Button)findViewById(R.id.true_button);
//使用监听器
mTrueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
checkAnswer(true);
}
});
/**
* 错误按钮
*/
mFalseButton = (Button) findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
checkAnswer(false);
}
});
/**
* 下一道题目按钮
*/
mNextButton = (Button)findViewById(R.id.next_button);
mNextButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
mIsCheater = false;
updateQuestion();
}
});
if(savedInstanceState != null){
mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, 0);
}
/**
* 作弊的按钮跳转
*/
mCheatButtton = (Button) findViewById(R.id.cheat_button);
mCheatButtton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Start CheatActivity
//跳转activity
Intent i = new Intent(MainActivity.this, CheatActivity.class);
//传递内容
boolean answerIsTrue = mQuestionBank[mCurrentIndex].ismTrueQuestion();
i.putExtra(CheatActivity.EXTRA_ANSWER_IS_TRUE, answerIsTrue);
//startActivity(i);
startActivityForResult(i,0);//第二个参数是请求代码,当一个activity有多个子activity时,且需要区分消息反馈
}
});
updateQuestion();
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState){//记住当前的答案,当屏幕旋转时,仍可以回到当前问题
super.onSaveInstanceState(savedInstanceState);
Log.i(TAG, "onSavedInstanceState");
savedInstanceState.putInt(KEY_INDEX, mCurrentIndex);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){//
if(data == null){
return;
}
mIsCheater = data.getBooleanExtra(CheatActivity.EXTRA_ANSWER_SHOWN, false);
}
}
package ligz.com.geoquiz;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
/**
* Created by Es muss sein on 2018/9/4.
*/
public class CheatActivity extends Activity{
public static final String EXTRA_ANSWER_IS_TRUE = "ligz.com.geoquiz.answer_is_true";
public static final String EXTRA_ANSWER_SHOWN = "ligz.com.geoquiz.answer_show";
private boolean mAnswerIsTrue;
private TextView mAnswerTextView;
private Button mShowAnswer;
private void setAnswerShowResult(boolean isAnswerShown){
Intent data = new Intent();
data.putExtra(EXTRA_ANSWER_SHOWN, isAnswerShown);
setResult(RESULT_OK, data);
}
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cheat);
mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);
mAnswerTextView = (TextView)findViewById(R.id.answerTextView);
//答案不会显示除非用户按下按钮
setAnswerShowResult(false);
mShowAnswer = (Button)findViewById(R.id.showAnswerButton);
mShowAnswer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mAnswerIsTrue){
mAnswerTextView.setText(R.string.true_button);
}else{
mAnswerTextView.setText(R.string.false_button);
}
setAnswerShowResult(true);
}
});
}
}
package ligz.com.geoquiz;
/**
* Created by Es muss sein on 2018/9/3.
*/
public class TrueFalse {
private int mQuestion;
private boolean mTrueQuestion;
public TrueFalse(int mQuestion, boolean mTrueQuestion){
this.mQuestion = mQuestion;
this.mTrueQuestion = mTrueQuestion;
}
public int getmQuestion() {
return mQuestion;
}
public void setmQuestion(int mQuestion) {
this.mQuestion = mQuestion;
}
public boolean ismTrueQuestion() {
return mTrueQuestion;
}
public void setmTrueQuestion(boolean mTrueQuestion) {
this.mTrueQuestion = mTrueQuestion;
}
}
GeoQuiz
中国哲学简史的作者是否是冯友兰
1984的作者是乔治奥威尔吗
王小波写过沉默的大多数吗
意志和表象的世界是叔本华的作品吗
牛棚杂忆和动物庄园的作者是同一人吗
cheat!
你确定要查看答案吗
查看答案
Cheating is wrong
对
错
Settings
回答正确
回答错误
下一题
csdn下载