1. package com.example.geoquiz;
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.view.Gravity;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.TextView;
  9. import android.widget.Toast;
  10.  
  11. public class QuizActivity extends AppCompatActivity {
  12.  
  13. private Button mTrueButton;
  14. private Button mFlaseButton;
  15. private Button mNextButton;
  16. private Button mPrevButton;
  17. private TextView mQuestionTextView;
  18. private Question[] mQuestionBank = new Question[]{
  19. new Question(R.string.question_australia, true),
  20. new Question(R.string.question_oceans, true),
  21. new Question(R.string.question_mideast, false),
  22. new Question(R.string.question_africa, false),
  23. new Question(R.string.question_americas, true),
  24. new Question(R.string.question_asia, true),
  25. };
  26. private int mCurrentIndex = 0;
  27.  
  28. private TextView mTextView;
  29.  
  30. @Override
  31. protected void onCreate(Bundle savedInstanceState) {
  32. super.onCreate(savedInstanceState);
  33. setContentView(R.layout.activity_quiz);
  34.  
  35. mQuestionTextView = (TextView) findViewById(R.id.question_text_view);
  36. updateQuestion();
  37.  
  38. mFlaseButton = (Button) findViewById(R.id.false_button);
  39. mFlaseButton.setOnClickListener(new View.OnClickListener() {
  40. @Override
  41. public void onClick(View view) {
  42. // Toast.makeText(QuizActivity.this,R.string.correct_toast,Toast.LENGTH_SHORT).show();
  43. checkAnswer(false);
  44. }
  45. });
  46.  
  47. mTrueButton = (Button) findViewById(R.id.true_button);
  48. mTrueButton.setOnClickListener(new View.OnClickListener() {
  49. @Override
  50. public void onClick(View view) {
  51. // Toast toast = Toast.makeText(QuizActivity.this,R.string.incorrect_toast,Toast.LENGTH_SHORT);
  52. // toast.setGravity(Gravity.TOP, 0, 0);
  53. // toast.show();
  54.  
  55. checkAnswer(true);
  56. }
  57. });
  58.  
  59. mNextButton = (Button) findViewById(R.id.next_button);
  60. mNextButton.setOnClickListener(new View.OnClickListener() {
  61. @Override
  62. public void onClick(View view) {
  63. mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
  64. updateQuestion();
  65. }
  66. });
  67.  
  68. mPrevButton =(Button)findViewById(R.id.prev_button);
  69. mPrevButton.setOnClickListener(new View.OnClickListener() {
  70. @Override
  71. public void onClick(View view) {
  72. if(mCurrentIndex==0){
  73. mCurrentIndex = mQuestionBank.length-1;
  74. }else{
  75. // mCurrentIndex = (mCurrentIndex - 1) % mQuestionBank.length;
  76. mCurrentIndex = mCurrentIndex - 1;
  77. }
  78. updateQuestion();
  79. }
  80. });
  81.  
  82. mTextView =(TextView) findViewById(R.id.question_text_view);
  83. mTextView.setOnClickListener(new View.OnClickListener() {
  84. @Override
  85. public void onClick(View view) {
  86. mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
  87. updateQuestion();
  88. }
  89. });
  90.  
  91. }
  92.  
  93. private void updateQuestion() {
  94. int question = mQuestionBank[mCurrentIndex].getTextResId();
  95. mQuestionTextView.setText(question);
  96. }
  97.  
  98. private void checkAnswer(boolean userPressedTrue) {
  99. boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();
  100. int messageResId = 0;
  101. if (userPressedTrue == answerIsTrue) {
  102. messageResId = R.string.correct_toast;
  103. } else {
  104. messageResId = R.string.incorrect_toast;
  105. }
  106. Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show();
  107. }
  108.  
  109. }
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:gravity="center"
  5. android:orientation="vertical">
  6.  
  7. <TextView
  8. android:id="@+id/question_text_view"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:padding="24dp" />
  12.  
  13. <LinearLayout
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:orientation="horizontal">
  17.  
  18. <Button
  19. android:id="@+id/true_button"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:text="@string/true_button" />
  23.  
  24. <Button
  25. android:id="@+id/false_button"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:text="@string/false_button" />
  29.  
  30. </LinearLayout>
  31.  
  32. <LinearLayout
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:orientation="horizontal">
  36.  
  37. <Button
  38. android:id="@+id/prev_button"
  39. android:layout_width="wrap_content"
  40. android:layout_height="wrap_content"
  41. android:text="@string/prev_button"/>
  42.  
  43. <Button
  44. android:id="@+id/next_button"
  45. android:layout_width="wrap_content"
  46. android:layout_height="wrap_content"
  47. android:text="@string/next_button" />
  48.  
  49. </LinearLayout>
  50.  
  51. </LinearLayout>

Android编程权威指南(第三版)- 2.8 挑战练习:添加后退按钮的更多相关文章

  1. Android编程权威指南第三版 第32章

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/qq_35564145/article/de ...

  2. 使用最新AndroidStudio编写Android编程权威指南(第3版)中的代码会遇到的一些问题

    Android编程权威指南(第3版)这本书是基于Android7.0的,到如今已经过于古老,最新的Android版本已经到10,而这本书的第四版目前还没有正式发售,在最近阅读这本书时,我发现这本书的部 ...

  3. Android编程权威指南(第2版)--第16章 使用intent拍照 挑战练习

    16.7挑战练习:优化照片显示 新建dialog_photo.xml 1234567891011121314 <?xml version="1.0" encoding=&qu ...

  4. 读《Android编程权威指南》

    因为去年双十二购买了一折的<Android 编程权威指南(第一版)>,在第二版出来后图灵社区给我推送了第二版的优惠码,激动之余就立马下单购买电子书,不得不说Big Nerd Ranch G ...

  5. 《Android编程权威指南》

    <Android编程权威指南> 基本信息 原书名:Android programming: the big nerd ranch guide 原出版社: Big Nerd Ranch Gu ...

  6. 《Android编程权威指南》CriminalIntent项目梳理

    相信很多新手或者初级开发人员都已经买了第2版的<Android编程权威指南>, 这本书基于Android Studio开发,对入门人员来说是很好的选择,但是很可惜的是, 在完成一个项目后, ...

  7. 《Android编程权威指南》PhotoGallery应用梳理

    PhotoGalley是<Android编程权威指南>书中另外一个重要的应用.       

  8. Swift编程权威指南第2版 读后收获

    自从参加工作一直在用OC做iOS开发.在2015年的时候苹果刚推出swift1.0不久,当时毕竟是新推出的语言,大家也都很有激情的学习.不过在学完后发现很难在实际项目中使用,再加上当时公司项目都是基于 ...

  9. Android编程权威指南笔记2:解决R文件爆红问题和SDK概念

    在android studio中会遇到R文件的丢失,所以遇见这问题怎么解决呢? 重新检查资源文件中xml文件 最近一次编译时如果未生成R.java文件,项目中资源引用的地方都会出错.通常,这是某个xm ...

随机推荐

  1. LeetCode——9. Palindrome Number

    一.题目链接:https://leetcode.com/problems/palindrome-number/ 二.题目大意: 给定一个整数,判断它是否为一个回文数.(例如-12,它就不是一个回文数: ...

  2. 【剑指offer】字符串替换

    请实现一个函数,将一个字符串中的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. *StringBuffer 扩容 str ...

  3. 力奋github:https://github.com/birdstudiocn

    我的github地址https://github.com/birdstudiocn

  4. Java NIO系列教程(五)Buffer

    Java NIO中的Buffer用于和NIO通道进行交互.如你所知,数据是从通道读入缓冲区,从缓冲区写入到通道中的.交互图如下: 缓冲区本质上是一块可以写入数据,然后可以从中读取数据的内存.这块内存被 ...

  5. 信息安全-加密:DES 加密

    ylbtech-信息安全-加密:DES 加密 DES全称为Data Encryption Standard,即数据加密标准,是一种使用密钥加密的块算法,1977年被美国联邦政府的国家标准局确定为联邦资 ...

  6. HDOJ 2020 绝对值排序

    #include<iostream> #include<cmath> #include<algorithm> #include<vector> usin ...

  7. Java zxing生成二维码所需的jar包

    免费的,不需要积分. 共有2个jar包, 链接: https://pan.baidu.com/s/1QJcEkRQOp1NdrNAgGC6LvQ 密码: 4524

  8. OA-DB-LINUX安装说明

    HOST配置: HOST1:OADB-NODE1.LSTECH.COM HOST2:OADB-NODE2.LSTECH.COM 每台主机配置两个不同VLAN的IP地址 OSUSER:oracle 1. ...

  9. Shiro 五张表

    参考博客: http://blog.csdn.net/frankcheng5143/article/details/50836619 Filter:运行过程中改变进入资源的请求和资源返回的响应中的有效 ...

  10. 安装配置Glusterfs

    软件下载地址:http://bits.gluster.org/pub/gluster/glusterfs/3.4.2/x86_64/ 192.168.1.11 10.1.1.241 glusterfs ...