分享一个android端的小例子,代码虽少,五脏俱全。
得到的效果是
废话不多说,直接上代码。
我们先看项目的结构。
我们的主活动页面是activity_main.xml 代码如下
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/question_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="24dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/true_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/true_button"/>
<Button
android:id="@+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/false_button"/>
</LinearLayout>
<Button
android:id="@+id/cheat_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:text="@string/cheat_button"/>
<Button
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:text="@string/next_button"/>
</FrameLayout>
对应的活动页面是MainActivity.java,代码如下:
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);
}
}
点击cheat按钮,进入查看答案的页面,对应的xml文件是activity_cheat.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:text="@string/warning_text"/>
<TextView
android:id="@+id/answerTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/showAnswerButton"
android:text="@string/show_answer_button"/>
</LinearLayout>
对应的CheatActivity.java文件如下:
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);
}
});
}
}
model文件TrueFalse.java如下:
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;
}
}
对应Strings.xml如下:
<resources>
<string name="app_name">GeoQuiz</string>
<string name="question_text">中国哲学简史的作者是否是冯友兰</string>
<string name="question_text1">1984的作者是乔治奥威尔吗</string>
<string name="question_text2">王小波写过沉默的大多数吗</string>
<string name="question_text3">意志和表象的世界是叔本华的作品吗</string>
<string name="question_text4">牛棚杂忆和动物庄园的作者是同一人吗</string>
<string name="cheat_button">cheat!</string>
<string name="warning_text">你确定要查看答案吗</string>
<string name="show_answer_button">查看答案</string>
<string name="judgment_toast">Cheating is wrong</string>
<string name="true_button">对</string>
<string name="false_button">错</string>
<string name="menu_settings">Settings</string>
<string name="correct_toast">回答正确</string>
<string name="incorrect_toast">回答错误</string>
<string name="next_button">下一题</string>
</resources>