写在前面:
下载区
第6章挑战练习–>Link
后续还有其他方式提供没有积分的小伙伴下载
在GeoQuiz应用的页面布局上添加一个TextView组件,向用户报告设备运行系统的API级别
<TextView
android:id="@+id/show_api_level"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="14dp"
tools:text=""/>
解释:不用说,就是加一个文本框,用于显示API Level
private TextView mApiLevelTV;//别忘了创建
//...
protected void onCreate(Bundle savedInstanceState) {
//...
setContentView(R.layout.activity_cheat);
/*在上面这句后,加入绑定语句,然后设置与显示字符串*/
mApiLevelTV = (TextView)findViewById(R.id.show_api_level);
CharSequence cs = "API Level " + Build.VERSION.SDK_INT;
mApiLevelTV.setText(cs);
//...
}
解释:完成了,此处主要了解charSequance 是什么东西就可以,String也是继承自CharSequence实现了相关方法,只读。
允许用户最多作弊3次。记录用户偷看答案的次数,在CHEAT按钮下显示剩余的次数。超出后,禁用偷看按钮。
本题目按照我理解的意思,点击了show answer
按钮才算是偷看了,所以这就有点麻烦,需要数据两边传,从QuizActivity
到CheatActivity
相互传输。
思路:QuizActiviity
创建一个记录显示答案次数的变量–>传到CheatActivty
,当用户点击了显示答案,该变量,会减一,–>传回原本的QuizActivity
,最后更新一下UI,如果次数为0了说明不能看了。
QuizActivity.java
和 CheatActivity.java
中前面数据创建部分加入两个量private static final String EXTRA_CHEAT_CHANCE =
"com.bignerdranch.android.geoquiz.cheat_chance";
//记录作弊次数
private static int mCheatChance = 3;
TextView
用于显示剩余的可以偷看的次数。我放在了nextButton
后面 "wrap_content"
android:layout_height="wrap_content"
android:id="@+id/show_cheat_chance"
android:text=""
/>
此处有错误,已经更正
- 控件绑定不用说了吧
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
//下方添加绑定
mCheatChanceTextView = (TextView)findViewById(R.id.show_cheat_chance);
...
}
CheatActivity.java
中,让他知道现在的可看次数 mCheatButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();
Intent intent = CheatActivity.newIntent(QuizActivity.this, answerIsTrue);
//下方多传入剩余可查看答案的次数
intent.putExtra(EXTRA_CHEAT_CHANCE,mCheatChance);
startActivityForResult(intent,REQUEST_CODE_CHEAT);
}
});
onCreate
的代码全部贴出,但其实只要增加与mCheatChance
有关的两句就可以 protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cheat);
//下面这句从QuizActivity中获得可查看答案次数的数据
mCheatChance = getIntent().getIntExtra(EXTRA_CHEAT_CHANCE,3);
mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);
mAnswerTextView = (TextView)findViewById(R.id.answer_text_view);
mApiLevelTV = (TextView)findViewById(R.id.show_api_level);
CharSequence cs = "API Level " + Build.VERSION.SDK_INT;
mApiLevelTV.setText(cs);
mShowAnswerButton = (Button)findViewById(R.id.show_answer_button);
mShowAnswerButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
//每次看过答案就-1
mCheatChance--;
if(mAnswerIsTrue){
mAnswerTextView.setText(R.string.true_button);
}else{
mAnswerTextView.setText(R.string.false_button);
}
setAnswerShownResult(true);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int cx = mShowAnswerButton.getWidth() / 2;
int cy = mShowAnswerButton.getHeight() / 2;
float radius = mShowAnswerButton.getWidth();
Animator anim = ViewAnimationUtils
.createCircularReveal(mShowAnswerButton, cx, cy, radius, 0);
anim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
mShowAnswerButton.setVisibility(View.INVISIBLE);
}
});
anim.start();
}else{
mShowAnswerButton.setVisibility(View.INVISIBLE);
}
}
});
}
setAnswerShownResul
t把mCheatChance
传回去 private void setAnswerShownResult(boolean isAnswerShown) {
Intent data = new Intent();
data.putExtra(EXTRA_ANSWER_SHOWN, isAnswerShown);
//顺便传一个数回去
data.putExtra(EXTRA_CHEAT_CHANCE, mCheatChance);
setResult(RESULT_OK, data);
}
在onActivityResult
收到返回的数据
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_OK) {
return;
}
if (requestCode == REQUEST_CODE_CHEAT) {
if (data == null) {
return;
}
mIsCheater = CheatActivity.wasAnswerShown(data);
//得到返回数据
mCheatChance = data.getIntExtra(EXTRA_CHEAT_CHANCE,0);
}
}
在onStart里面更新显示UI
@Override
public void onStart() {
super.onStart();
//没有作弊的机会了
if(mCheatChance == 0) {
mCheatButton.setEnabled(false);
mCheatChanceTextView.setText("no chance" + " times left");
}
else{
mCheatChanceTextView.setText(mCheatChance + " time(s) left");
}
Log.d(TAG, "onStart() called");
}