此次接着上面题库框架,增加查看答案和作弊功能。
知识点:
1.增加Activity并在AndroidManifest.xml中声明
2.Activity之间的数据传输对象Intent,及extra数据信息对(key-value)
3.传输函数startActivityForResult(in,0);返回函数 setAnswerShowResult(boolean isAnsShow)//生成返回数据的Intent,参数为是否看过答案
HelloAndroidActivity
package com.example.helloandroid;
import android.app.Activity;
import android.content.Intent;
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 HelloAndroidActivity extends Activity {
private ImageButton b1;
private ImageButton b2;
private ImageButton nb;
private ImageButton pb;
private Button cb;
private TextView qtv;
//设置三个按钮变量,一个文本变量
private TrueFalse[] qBank = new TrueFalse[]{
new TrueFalse(R.string.question1,true),
//TrueFalse包含两个参数,调用构造函数
new TrueFalse(R.string.question2,false),
new TrueFalse(R.string.question3,true),
new TrueFalse(R.string.question4,false),
};//设置TrueFalse对象数组
private int currentIndex=0;
//计数变量
private boolean isCheat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello_android);
//调用名为activity_hello_android的layout的用户界面
qtv = (TextView) findViewById(R.id.question_text_view);
//找到id为这个的文本id资源
int question = qBank[currentIndex].getQuestion();
//找到当前问题的文本string资源的id
qtv.setText(question);
//将数组中的问题显示到qtv上
qtv.setOnClickListener(new View.OnClickListener() {
//点击题目文本也能显示下一道题
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
updateQuestion(1);
}
});
b1 = (ImageButton) findViewById(R.id.true_button);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//Toast.makeText(HelloAndroidActivity.this,R.string.correct_toast,Toast.LENGTH_SHORT).show();
/*toast为弹窗消息,生成一个toast用Toast.makeText,三个参数
1.Activity的一个实例
2.toast的string资源id
3.显示时间
Toast.show()显示在屏幕上
*/
checkAnswer(true);
}
});
b2 = (ImageButton) findViewById(R.id.false_button);
b2.setOnClickListener(new View.OnClickListener() {
//设置button2的监听器
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
checkAnswer(false);
}
});
pb = (ImageButton) findViewById(R.id.pre_button);
pb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
updateQuestion(-1);
//向前翻页
}
});
nb = (ImageButton) findViewById(R.id.next_button);
//next按钮显示NEXT
nb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
updateQuestion(1);
}
});
cb = (Button) findViewById(R.id.cheatButton);
cb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//進入CheatActivity
Intent in = new Intent(HelloAndroidActivity.this,CheatActivity.class);
//构造Intent对象
boolean ansIsTrue = qBank[currentIndex].isTruequestion();
//构造EXTRA数据对
in.putExtra(CheatActivity.EXTRA_ANSWER_IS_TRUE, ansIsTrue);
//将EXTRA数据对附加到Intent对象上
startActivityForResult(in,0);
//为了从CheatActivity返回结果,第一个参数,传过去的Intent,第二个参数传过去的请求代码
//启动CheatActivity
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.hello_android, menu);
return true;
}
private void checkAnswer(boolean userA){
//判断答案是否正确,参数为用户答案
boolean currans = qBank[currentIndex].isTruequestion();
//currans为正确答案
int maessageId = 0;
//maessageId设置toast的string资源id
if(isCheat){
maessageId = R.string.cheat_toast;
}
else{
if(userA == currans){
//如果正确
maessageId = R.string.correct_toast;
}
else{
maessageId = R.string.incorrect_toast;
}
}
Toast.makeText(this, maessageId, Toast.LENGTH_SHORT).show();
//显示提示信息
}
private void updateQuestion(int isNext){
if(isNext==1){
//1为向后翻,否则向前翻
currentIndex = (currentIndex+1)%qBank.length;
isCheat=false;
}
else{
currentIndex = (currentIndex-1+qBank.length)%qBank.length;
}
//当前索引先加1
int question = qBank[currentIndex].getQuestion();
//找到当前问题的文本string资源的id
qtv.setText(question);
//将数组中的问题显示到qtv上
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(data==null)return;
isCheat = data.getBooleanExtra(CheatActivity.EXTRA_ANSWER_SHOW,false);
//第二个参数为默认值,在key无效时使用
}
}
CheatActivity
package com.example.helloandroid;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class CheatActivity extends Activity {
public static final String EXTRA_ANSWER_IS_TRUE = "answer is true";
//接收EXTRA
public static final String EXTRA_ANSWER_SHOW = "answer show";
//生成EXTRA
private TextView anst;
private Button showAnsb;
private boolean ansIsTrue;
private void setAnswerShowResult(boolean isAnsShow){
//生成返回数据的Intent,参数为是否看过答案
Intent resultData = new Intent();
resultData.putExtra(EXTRA_ANSWER_SHOW, isAnsShow);
setResult(RESULT_OK,resultData);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cheat);
ansIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);
//接收Intent的Extra信息
anst = (TextView) findViewById(R.id.ansTextView);
//显示答案的文本
showAnsb = (Button) findViewById(R.id.showAnsButton);
//显示答案按钮
setAnswerShowResult(false);
//这时还未偷看答案
showAnsb.setOnClickListener(new View.OnClickListener() {
//监听器
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(ansIsTrue){
anst.setText("正确");
//设置答案的文本
}
else{
anst.setText("错误");
}
setAnswerShowResult(true);
//已看,则设置返回“看过”
}
});
}
}
其他
1.string.xml不提
2.bug:关键是
EXTRA_ANSWER_SHOW的值传回首页时为true还是false。可通过两次进入
CheatActivity来逃过作弊检查,第一次看答案,第二次不看答案。最后一次传回的值为false,点击选择的答案时就避免了作弊的提示。