当前位置:   article > 正文

Android 答题_获取安卓app里面的题目

获取安卓app里面的题目

1.功能介绍

关键技术:SQLite,fragment,ViewFlipper,OnTouchListener

(1)搜索:在搜索框中输入关键词,搜索包含该关键词的题目,并可查看题目详情。 

 
(2)题库:按内容和来源两种分类的题库,可选择题库种的题目进行测试或者练习。其中练习没有计时,没有计分,但能保存当前做题进度;测试计时且计分,最后会显示成绩 

 
 
 
(3)错题:按内容和来源两种分类的错题集,可进行练习 
(4)收藏:按内容和来源两种分类的收藏,可进行练习 
(5)我的记录:记录每次测试的用时和分数

2.搜索功能

【思路】通过关键词,先从SQLite中获取数据cursor,然后在ListView中加载,并设置OnItemClickListener,跳转到对应的题目详情。 
【注意】 
(1)搜索结果列表,需要设置limit限制列表数量和更多或没有结果的提示信息,以免一次性加载,导致内存溢出或者加载缓慢。 
(2)查询关键词的语句是(冒号和空格一定要仔细检查)

"select _id,que from que where que like '%"+text                +"%' or choiceA like '%"+text+"%' or choiceB like'%"+text                +"%' or choiceC like'%"+text+"%' or choiceD ike'%"+text+"%' limit "+limit"
  1. activity_search.xml 搜索界面
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. xmlns:tools="http://schemas.android.com/tools"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:orientation="vertical"
  9. tools:context="com.lidan.xiao.danquestion.activity.SearchActivity">
  10. <android.support.design.widget.AppBarLayout
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:theme="@style/AppTheme.AppBarOverlay">
  14. <android.support.v7.widget.Toolbar
  15. android:id="@+id/toolbar1"
  16. android:layout_width="match_parent"
  17. android:layout_height="?attr/actionBarSize"
  18. android:background="?attr/colorPrimary"
  19. app:popupTheme="@style/AppTheme.PopupOverlay">
  20. <LinearLayout
  21. android:layout_width="match_parent"
  22. android:layout_height="wrap_content"
  23. android:gravity="center"
  24. android:orientation="horizontal">
  25. <EditText
  26. android:id="@+id/sv"
  27. android:layout_width="0dp"
  28. android:layout_height="wrap_content"
  29. android:textColor="@color/white"
  30. android:layout_weight="1"/>
  31. <ImageView
  32. android:id="@+id/img_search"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:src="@drawable/search"
  36. android:padding="8dp"/>
  37. </LinearLayout>
  38. </android.support.v7.widget.Toolbar>
  39. </android.support.design.widget.AppBarLayout>
  40. <ScrollView
  41. android:layout_width="match_parent"
  42. android:layout_height="0dp"
  43. android:layout_weight="1">
  44. <LinearLayout
  45. android:layout_width="match_parent"
  46. android:layout_height="wrap_content"
  47. android:orientation="vertical">
  48. <com.lidan.xiao.danquestion.view.MyListView
  49. android:id="@+id/lv_search"
  50. android:layout_width="match_parent"
  51. android:layout_height="wrap_content"/>
  52. <TextView
  53. android:id="@+id/tv_info"
  54. android:layout_width="match_parent"
  55. android:layout_height="wrap_content"
  56. android:textColor="@color/gray"
  57. android:padding="8dp"
  58. android:gravity="center"
  59. android:background="@drawable/bt1"
  60. android:textSize="16dp"/>
  61. </LinearLayout>
  62. </ScrollView>
  63. </LinearLayout>
  1. listitem1.xml
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <LinearLayout
  4. xmlns:android="http://schemas.android.com/apk/res/android"
  5. android:layout_width="match_parent"
  6. android:layout_height="wrap_content"
  7. android:orientation="horizontal"
  8. android:background="@android:drawable/picture_frame">
  9. <TextView
  10. android:id="@+id/tv_item2"
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:padding="16dp"
  14. android:ellipsize="end"
  15. android:textSize="14sp"/>
  16. </LinearLayout>
  1. MyListView .java
  2. //自定义ListView防止在ScrollView中不能正常显示
  3. public class MyListView extends ListView {
  4. public MyListView(Context context) {
  5. super(context);
  6. }
  7. public MyListView(Context context, AttributeSet attrs) {
  8. super(context, attrs);
  9. }
  10. public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {
  11. super(context, attrs, defStyleAttr);
  12. }
  13. @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  14. public MyListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
  15. super(context, attrs, defStyleAttr, defStyleRes);
  16. }
  17. @Override
  18. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  19. int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
  20. MeasureSpec.AT_MOST);
  21. super.onMeasure(widthMeasureSpec, expandSpec);
  22. }
  23. }
  1. public class SearchActivity extends AppCompatActivity implements View.OnClickListener {
  2. private EditText sv;
  3. private MyListView lv;
  4. private TextView tv;
  5. private ImageView submit;
  6. private boolean isLv=false;
  7. private int num=0,limit=10;
  8. private Cursor cursor;
  9. private SimpleCursorAdapter adapter;
  10. @Override
  11. public void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_search);
  14. //ActionBar工具栏设置
  15. Toolbar toolbar = findViewById(R.id.toolbar1);
  16. setSupportActionBar(toolbar);
  17. getSupportActionBar().setHomeButtonEnabled(true);
  18. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  19. sv=findViewById(R.id.sv);
  20. submit=findViewById(R.id.img_search);
  21. tv=findViewById(R.id.tv_info);
  22. lv=findViewById(R.id.lv_search);
  23. submit.setOnClickListener(this);
  24. }
  25. @Override
  26. public void onClick(View v) {
  27. String text=sv.getText().toString();
  28. if(!text.isEmpty()){
  29. searchResult(text);
  30. }else {
  31. Toast.makeText(this,"请输入查询内容",Toast.LENGTH_LONG).show();
  32. }
  33. }
  34. //查询结果加载
  35. private void searchResult(final String text) {
  36. cursor = ToolHelper.loadDB(this,"select _id,que from que where que like '%"+text
  37. +"%' or choiceA like '%"+text+"%' or choiceB like'%"+text
  38. +"%' or choiceC like'%"+text+"%' or choiceD like'%"+text+"%' limit "+limit);
  39. num=cursor.getCount();
  40. if(num>0) {
  41. if(!isLv) {//如果lv未创建
  42. adapter = new SimpleCursorAdapter(this, R.layout.listitem1, cursor,
  43. new String[]{"que"}, new int[]{R.id.tv_item2},
  44. CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
  45. lv.setAdapter(adapter);
  46. lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  47. @Override
  48. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  49. cursor.moveToPosition(position);
  50. int select = cursor.getInt(cursor.getColumnIndex("_id"));
  51. Intent intent = new Intent(SearchActivity.this, DetailActivity.class);
  52. intent.putExtra("qid", select);
  53. startActivity(intent);
  54. }
  55. });
  56. isLv=true;
  57. }else {//如果lv已经创建,数据改变则重新加载lv
  58. adapter.changeCursor(cursor);
  59. adapter.notifyDataSetChanged();
  60. }
  61. resultTv(text);
  62. }else {
  63. limit=10;
  64. if(isLv){
  65. adapter.changeCursor(cursor);
  66. adapter.notifyDataSetChanged();
  67. isLv=false;
  68. }
  69. tv.setVisibility(View.VISIBLE);
  70. tv.setText("无查询结果");
  71. }
  72. }
  73. //提示信息TextView设置
  74. private void resultTv(final String text) {
  75. if(num<limit) {//如果查询结果数小于限制数
  76. tv.setVisibility(View.GONE);
  77. limit=10;
  78. }else if(num>=limit){//如果查询结果数多于限制数
  79. tv.setText("更多查询数据");
  80. tv.setVisibility(View.VISIBLE);
  81. tv.setOnClickListener(new View.OnClickListener() {
  82. @Override
  83. public void onClick(View v) {
  84. limit=limit+10;
  85. searchResult(text);
  86. //Toast.makeText(SearchActivity.this,"limit="+String.valueOf(limit),Toast.LENGTH_SHORT).show();
  87. }
  88. });
  89. }
  90. }
  91. @Override
  92. public boolean onOptionsItemSelected(MenuItem item) {
  93. if(item.getItemId()==android.R.id.home){//返回
  94. finish();
  95. }
  96. return super.onOptionsItemSelected(item);
  97. }
  98. }
  1. activity_detail.xml 详情界面
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. xmlns:tools="http://schemas.android.com/tools"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:fitsSystemWindows="true"
  9. tools:context="com.lidan.xiao.danquestion.activity.DetailActivity">
  10. <LinearLayout
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:orientation="vertical">
  14. <include layout="@layout/layout_top1"/>
  15. <TextView
  16. android:id="@+id/tv_que"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:layout_margin="8dp"
  20. android:textColor="#000000"
  21. android:textSize="16sp" />
  22. <TextView
  23. android:id="@+id/tv_choice"
  24. android:layout_width="match_parent"
  25. android:layout_height="wrap_content"
  26. android:layout_margin="8dp"
  27. android:textColor="@color/colorPrimary" />
  28. <TextView
  29. android:id="@+id/tv_answer"
  30. android:layout_width="match_parent"
  31. android:layout_height="wrap_content"
  32. android:layout_margin="8dp"
  33. android:textColor="@color/colorAccent" />
  34. <TextView
  35. android:id="@+id/tv_source"
  36. android:layout_width="match_parent"
  37. android:layout_height="wrap_content"
  38. android:layout_margin="8dp"
  39. android:textColor="@color/gray"
  40. android:textSize="14sp" />
  41. <TextView
  42. android:id="@+id/tv_detail"
  43. android:layout_width="match_parent"
  44. android:layout_height="wrap_content"
  45. android:layout_margin="8dp"
  46. android:textColor="@color/gray"
  47. android:textSize="14sp" />
  48. </LinearLayout>
  49. <android.support.design.widget.FloatingActionButton
  50. android:id="@+id/fab_collect"
  51. android:layout_width="wrap_content"
  52. android:layout_height="wrap_content"
  53. android:layout_gravity="end"
  54. android:layout_marginRight="8dp"
  55. android:layout_marginTop="180dp" />
  56. </FrameLayout>
  1. public class DetailActivity extends AppCompatActivity {
  2. private TextView tv_que, tv_choice, tv_answer, tv_source, tv_detail;
  3. private FloatingActionButton fabcollect;
  4. private int qid;
  5. private boolean tag = false;
  6. private String kind, type, choice, que, answer, source, detail;
  7. @Override
  8. public void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_detail);
  11. //ActionBar工具栏设置
  12. Toolbar toolbar = findViewById(R.id.toolbar1);
  13. setSupportActionBar(toolbar);
  14. getSupportActionBar().setHomeButtonEnabled(true);
  15. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  16. tv_que = findViewById(R.id.tv_que);
  17. tv_choice = findViewById(R.id.tv_choice);
  18. tv_answer = findViewById(R.id.tv_answer);
  19. tv_source = findViewById(R.id.tv_source);
  20. tv_detail = findViewById(R.id.tv_detail);
  21. fabcollect = findViewById(R.id.fab_collect);
  22. Intent intent = getIntent();
  23. qid = intent.getIntExtra("qid", 0);
  24. if (qid > 0) {
  25. loadData();
  26. initData();
  27. initCollect();
  28. }
  29. }
  30. private void initCollect() {
  31. final Cursor cursor = ToolHelper.loadDB(this, "select qid from collection where qid=" + qid);
  32. if (cursor.getCount() > 0) {
  33. tag = true;
  34. fabcollect.setImageResource(R.drawable.star_on);
  35. } else {
  36. tag = false;
  37. fabcollect.setImageResource(R.drawable.star1);
  38. }
  39. fabcollect.setOnClickListener(new View.OnClickListener() {
  40. @Override
  41. public void onClick(View v) {
  42. if (tag) {
  43. fabcollect.setImageResource(R.drawable.star1);
  44. Toast.makeText(DetailActivity.this, "取消收藏", Toast.LENGTH_SHORT).show();
  45. ToolHelper.excuteDB(DetailActivity.this, "delete from collection where qid=" + qid);
  46. } else {
  47. fabcollect.setImageResource(R.drawable.star_on);
  48. Toast.makeText(DetailActivity.this, "成功收藏", Toast.LENGTH_SHORT).show();
  49. ToolHelper.excuteDB(DetailActivity.this, "insert into collection (_id,qid) values (" + (Math.random()*10000) + "," + qid + ")");
  50. }
  51. }
  52. });
  53. }
  54. private void loadData() {
  55. Cursor cursor = ToolHelper.loadDB(this, "select * from que where _id=" + qid);
  56. cursor.moveToFirst();
  57. kind = cursor.getString(cursor.getColumnIndex("kind"));
  58. String choiceA = "A."+cursor.getString(cursor.getColumnIndex("choiceA"));
  59. String choiceB = "B."+cursor.getString(cursor.getColumnIndex("choiceB"));
  60. String choiceC = "C."+cursor.getString(cursor.getColumnIndex("choiceC"));
  61. String choiceD = "D."+cursor.getString(cursor.getColumnIndex("choiceD"));
  62. StringBuffer sb = new StringBuffer();
  63. if (choiceA != "null") {
  64. sb.append(choiceA + "\n");
  65. }
  66. if (choiceB != "null") {
  67. sb.append(choiceB + "\n");
  68. }
  69. if (choiceC != "null") {
  70. sb.append(choiceC + "\n");
  71. }
  72. if (choiceD != "null") {
  73. sb.append(choiceD + "\n");
  74. }
  75. choice = sb.toString();
  76. que = cursor.getString(cursor.getColumnIndex("que"));
  77. type = cursor.getString(cursor.getColumnIndex("type"));
  78. answer = cursor.getString(cursor.getColumnIndex("answer"));
  79. source = cursor.getString(cursor.getColumnIndex("source"));
  80. detail = cursor.getString(cursor.getColumnIndex("detail"));
  81. }
  82. private void initData() {
  83. setTitle(kind);
  84. tv_que.setText("(" + type + ")" + que);
  85. tv_choice.setText(choice);
  86. tv_answer.setText("【答案】" + answer);
  87. tv_source.setText("【来源】" + source);
  88. tv_detail.setText("【解析】" + detail);
  89. }
  90. @Override
  91. public boolean onOptionsItemSelected(MenuItem item) {
  92. if (item.getItemId() == android.R.id.home)
  93. finish();
  94. return super.onOptionsItemSelected(item);
  95. }
  96. }

3.测试和练习功能

测试功能中有计时,滑动切换试题,题号选择卡,收藏,错对判断和保存错题,测试结束后会保存考试成绩。 
练习功能中有滑动切换试题,题号选择卡,收藏,保存上次进度,错对判断和保存错题。

【滑动切换题目思路】使用AdapterViewFlipper和OnToucherListener实现,首先使用List来存储每个题目view,然后使用BaseAdapter加载到AdapterViewFlipper中,并通过AdapterViewFlipper的OnToucherListener来实现滑动切换题目的指针,切换题目view。

【题号卡思路】使用GridView来显示所有题目号,并标志已答题目,点击题号是返回答题界面结果,并将跳转到选择题目view,其中,viewFlipper不能直接设置当前view的Id来实现跳转,所以可以通过获取当前的题目view的Id,与选择题号相比较,用showPrevious和showNext来实现。

【错对判断思路】当点击提交当前题目的按钮时。先获取checkbox是否被选中,用StringBuffer来拼接自己答案,并存储自己选择的答案到List中来标志为已答题目,然后通过与参考答案对比,看是否加分,如果正确直接跳转到下一题,否则显示正确答案和解析,并存储错题。最后一题完成后会直接显示成绩和考试用时,并保存测试记录。

【注意】 
(1)选择测试和练习的题目需显示点击选择后的那项需改变一下,提醒已经选择了该项。 
(2)保存错题前一定要检查此题是否已经存在,以免重复。 
(3)收藏功能,判断当前题目是否被收藏,并显示,如果未收藏则可以点击按钮,并收藏成功。 
(3)保存上次练习进度,直接通过sharedPreference保存当前练习的指针位置。

  1. fragment_question.xml 题库/错题/收藏界面
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:id="@android:id/tabhost"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical">
  8. <LinearLayout
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent"
  11. android:orientation="vertical">
  12. <LinearLayout
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:orientation="horizontal"
  16. android:padding="8dp">
  17. <TextView
  18. android:id="@+id/top_source"
  19. android:layout_width="0dp"
  20. android:layout_height="wrap_content"
  21. android:layout_weight="1"
  22. android:gravity="center"/>
  23. <TextView
  24. android:id="@+id/top_kind"
  25. android:layout_width="0dp"
  26. android:layout_height="wrap_content"
  27. android:layout_weight="1"
  28. android:gravity="center"/>
  29. </LinearLayout>
  30. <ScrollView
  31. android:layout_width="match_parent"
  32. android:layout_height="wrap_content">
  33. <LinearLayout
  34. android:id="@+id/tab1"
  35. android:layout_width="match_parent"
  36. android:layout_height="wrap_content"
  37. android:orientation="vertical">
  38. <com.lidan.xiao.danquestion.view.MyListView
  39. android:id="@+id/lv_que"
  40. android:layout_width="match_parent"
  41. android:layout_height="match_parent" />
  42. <TextView
  43. android:id="@+id/tv_info1"
  44. android:layout_width="match_parent"
  45. android:layout_height="wrap_content"
  46. android:gravity="center"
  47. android:padding="8dp" />
  48. </LinearLayout>
  49. </ScrollView>
  50. </LinearLayout>
  51. <LinearLayout
  52. android:layout_width="wrap_content"
  53. android:layout_height="wrap_content"
  54. android:layout_margin="8dp"
  55. android:layout_gravity="end|bottom"
  56. android:orientation="vertical">
  57. <android.support.design.widget.FloatingActionButton
  58. android:id="@+id/fab_prac"
  59. android:layout_width="wrap_content"
  60. android:layout_height="wrap_content"
  61. android:src="@drawable/test"/>
  62. <android.support.design.widget.FloatingActionButton
  63. android:id="@+id/fab_test"
  64. android:layout_width="wrap_content"
  65. android:layout_height="wrap_content"
  66. android:layout_marginTop="8dp"
  67. android:src="@drawable/exam"/>
  68. </LinearLayout>
  69. </FrameLayout>
  1. @SuppressLint("ValidFragment")
  2. public class QuestionFragment extends Fragment implements View.OnClickListener {
  3. private int tab;
  4. private boolean tag = false;
  5. private View rootView,itemView=null;
  6. private ListView lv;
  7. private String table,content;
  8. public static String field,value;
  9. private TextView tv1, tv2, info;
  10. private SimpleCursorAdapter adapter;
  11. private Cursor cursor;
  12. private FloatingActionButton fabtest,fabprac;
  13. @SuppressLint("ValidFragment")
  14. public QuestionFragment(int tab) {
  15. this.tab = tab;
  16. }
  17. @Nullable
  18. @Override
  19. public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
  20. rootView = inflater.inflate(R.layout.fragment_question, container, false);
  21. tv1 = rootView.findViewById(R.id.top_source);
  22. tv2 = rootView.findViewById(R.id.top_kind);
  23. info = rootView.findViewById(R.id.tv_info1);
  24. info.setText("无内容");
  25. tv1.setText("来源");
  26. tv2.setText("分类");
  27. tv1.setOnClickListener(this);
  28. tv2.setOnClickListener(this);
  29. lv = rootView.findViewById(R.id.lv_que);
  30. fabtest = rootView.findViewById(R.id.fab_test);
  31. fabtest.setOnClickListener(this);
  32. fabprac=rootView.findViewById(R.id.fab_prac);
  33. fabprac.setOnClickListener(this);
  34. loadData();
  35. tab1();
  36. return rootView;
  37. }
  38. //加载分类题库
  39. private void tab2() {
  40. tv1.setTextColor(getResources().getColor(R.color.gray));
  41. tv2.setTextColor(getResources().getColor(R.color.colorAccent));
  42. queList("kind");
  43. }
  44. //加载来源题库
  45. private void tab1() {
  46. tv1.setTextColor(getResources().getColor(R.color.colorAccent));
  47. tv2.setTextColor(getResources().getColor(R.color.gray));
  48. queList("source");
  49. }
  50. private void loadData() {
  51. switch (tab) {
  52. case MyTag.QUE://题库
  53. table = "que";
  54. content="题库";
  55. break;
  56. case MyTag.COLLECT://收藏
  57. table = "collection ,que where collection.qid=que._id ";
  58. fabtest.setVisibility(View.GONE);
  59. content="收藏";
  60. break;
  61. case MyTag.WRONG://错题
  62. table = "wrong,que where wrong.qid=que._id ";
  63. fabtest.setVisibility(View.GONE);
  64. content="错题";
  65. break;
  66. }
  67. }
  68. //加载内容到列表
  69. private void queList(final String type) {
  70. cursor = ToolHelper.loadDB(getActivity(), "select que._id, que." + type + ",count(que._id) as num from " + table + " group by que." + type+" order by source desc");
  71. if (cursor.getCount() > 0) {
  72. if (!tag) {
  73. adapter = new SimpleCursorAdapter(getActivity(), R.layout.listitem, cursor,
  74. new String[]{type, "num"}, new int[]{R.id.tv_item, R.id.tv_item1},
  75. CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
  76. lv.setAdapter(adapter);
  77. lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  78. @Override
  79. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  80. TextView tv=view.findViewById(R.id.tv_item);
  81. if(itemView!=null) {
  82. TextView tv1=itemView.findViewById(R.id.tv_item);
  83. tv1.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
  84. }
  85. tv.setTextColor(getResources().getColor(R.color.colorAccent));
  86. itemView=view;
  87. field =type;
  88. value=cursor.getString(cursor.getColumnIndex(type));
  89. }
  90. });
  91. info.setVisibility(View.GONE);
  92. } else {
  93. adapter.notify();
  94. }
  95. } else {
  96. info.setVisibility(View.VISIBLE);
  97. info.setText("无记录");
  98. }
  99. }
  100. @Override
  101. public void onClick(View v) {
  102. switch (v.getId()){
  103. case R.id.fab_test:
  104. if(itemView!=null)
  105. askDialog("测试:",2);
  106. else
  107. Toast.makeText(getActivity(),"请选择题目集",Toast.LENGTH_SHORT).show();
  108. break;
  109. case R.id.fab_prac:
  110. if(itemView!=null)
  111. askDialog("练习:",1);
  112. else
  113. Toast.makeText(getActivity(),"请选择题目集",Toast.LENGTH_SHORT).show();
  114. break;
  115. case R.id.top_source:
  116. tab1();
  117. break;
  118. case R.id.top_kind:
  119. tab2();
  120. break;
  121. }
  122. }
  123. private void askDialog(String str,final int c) {
  124. AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
  125. builder.setMessage(str+"("+content+")"+value+"?");
  126. builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
  127. @Override
  128. public void onClick(DialogInterface dialog, int which) {
  129. Intent intent;
  130. if(c==2) {
  131. intent = new Intent(getActivity(), QuestionActivity.class);
  132. }else {
  133. intent = new Intent(getActivity(), PracticeActivity.class);
  134. }
  135. intent.putExtra("tab",tab);
  136. startActivity(intent);
  137. }
  138. });
  139. builder.setNegativeButton("取消",null);
  140. builder.show();
  141. }
  142. }
  1. activity_question.xml 题库测试界面
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. xmlns:tools="http://schemas.android.com/tools"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:fitsSystemWindows="true"
  9. tools:context=".activity.QuestionActivity"
  10. android:orientation="vertical">
  11. <android.support.design.widget.AppBarLayout
  12. android:id="@+id/app_bar"
  13. android:layout_width="match_parent"
  14. android:layout_height="56dp"
  15. android:fitsSystemWindows="true"
  16. android:theme="@style/AppTheme.AppBarOverlay">
  17. <android.support.v7.widget.Toolbar
  18. android:id="@+id/toolbar_que"
  19. android:layout_width="match_parent"
  20. android:layout_height="?attr/actionBarSize"
  21. app:layout_collapseMode="pin"
  22. app:popupTheme="@style/AppTheme.PopupOverlay">
  23. <LinearLayout
  24. android:layout_width="match_parent"
  25. android:layout_height="wrap_content"
  26. android:orientation="horizontal"
  27. android:gravity="center">
  28. <ImageView
  29. android:id="@+id/img_pre"
  30. android:layout_width="32dp"
  31. android:layout_height="32dp"
  32. android:src="@drawable/pre"/>
  33. <ImageView
  34. android:id="@+id/img_card"
  35. android:layout_width="32dp"
  36. android:layout_height="32dp"
  37. android:layout_margin="8dp"
  38. android:src="@drawable/quecard"/>
  39. <Chronometer
  40. android:id="@+id/mytime"
  41. android:layout_width="80dp"
  42. android:layout_height="wrap_content"
  43. android:gravity="center"
  44. android:textColor="@color/white" />
  45. <ImageView
  46. android:id="@+id/img_collect"
  47. android:layout_width="32dp"
  48. android:layout_height="32dp"
  49. android:src="@drawable/star1"
  50. android:layout_margin="8dp"/>
  51. <ImageView
  52. android:id="@+id/img_next"
  53. android:layout_width="32dp"
  54. android:layout_height="32dp"
  55. android:src="@drawable/next"/>
  56. </LinearLayout>
  57. </android.support.v7.widget.Toolbar>
  58. </android.support.design.widget.AppBarLayout>
  59. <include layout="@layout/que_content"/>
  60. </LinearLayout>
  1. queitem.xml题目view
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <LinearLayout
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:orientation="vertical">
  10. <TextView
  11. android:id="@+id/tv_que1"
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:padding="8dp" />
  15. <CheckBox
  16. android:id="@+id/cb_choice1"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:button="@drawable/cb"
  20. android:padding="8dp"
  21. android:textColor="@color/colorPrimary" />
  22. <CheckBox
  23. android:id="@+id/cb_choice2"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:button="@drawable/cb"
  27. android:padding="8dp"
  28. android:textColor="@color/colorPrimary" />
  29. <CheckBox
  30. android:id="@+id/cb_choice3"
  31. android:layout_width="wrap_content"
  32. android:layout_height="wrap_content"
  33. android:button="@drawable/cb"
  34. android:padding="8dp"
  35. android:textColor="@color/colorPrimary" />
  36. <CheckBox
  37. android:id="@+id/cb_choice4"
  38. android:layout_width="wrap_content"
  39. android:layout_height="wrap_content"
  40. android:button="@drawable/cb"
  41. android:padding="8dp"
  42. android:textColor="@color/colorPrimary" />
  43. <TextView
  44. android:id="@+id/tv_you"
  45. android:layout_width="match_parent"
  46. android:layout_height="wrap_content"
  47. android:padding="8dp"
  48. android:textColor="@color/colorAccent" />
  49. <TextView
  50. android:id="@+id/tv_answer1"
  51. android:layout_width="match_parent"
  52. android:layout_height="wrap_content"
  53. android:padding="8dp"
  54. android:textColor="@color/colorAccent" />
  55. <TextView
  56. android:id="@+id/tv_detail1"
  57. android:layout_width="match_parent"
  58. android:layout_height="wrap_content"
  59. android:padding="8dp"
  60. android:textColor="@color/gray" />
  61. </LinearLayout>
  62. </ScrollView>
  1. //答题Activity
  2. public class QuestionActivity extends AppCompatActivity implements View.OnClickListener {
  3. private int tab;
  4. private String table,content;
  5. private TextView tvTitle, tvScore;
  6. private Chronometer chronometer;
  7. private Cursor cursor;
  8. private boolean isCollect=false,isFirst=false;
  9. private int num;
  10. private int score = 0,index=0;
  11. public static List<String> anList;
  12. private String source;
  13. private String qid, type, que, A, B, C, D, answer, detail;
  14. private ImageView imgPre, imgNext;
  15. private AdapterViewFlipper vf;
  16. private BaseAdapter adapter;
  17. private ProgressBar pb;
  18. private View root;
  19. private TextView tvQue, tvDetail, tvAns, tvYou;
  20. private CheckBox cb1, cb2, cb3, cb4;
  21. private ImageView imgCollect,imgCard;
  22. @Override
  23. protected void onCreate(@Nullable Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.activity_question);
  26. //ActionBar工具栏设置
  27. Toolbar toolbar = findViewById(R.id.toolbar_que);
  28. setSupportActionBar(toolbar);
  29. getSupportActionBar().setHomeButtonEnabled(true);
  30. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  31. Intent intent=getIntent();
  32. tab=intent.getIntExtra("tab",1);
  33. initTable();
  34. initView();
  35. }
  36. private void initTable() {
  37. switch (tab) {
  38. case MyTag.QUE://题库
  39. table = "que";
  40. content="题库";
  41. break;
  42. case MyTag.COLLECT://收藏
  43. table = "collection ,que where collection.qid=que._id ";
  44. content="收藏";
  45. break;
  46. case MyTag.WRONG://错题
  47. table = "wrong,que where wrong.qid=que._id ";
  48. content="错题";
  49. break;
  50. }
  51. }
  52. @SuppressLint("SetTextI18n")
  53. private void initView() {
  54. //初始化收藏按钮
  55. imgCollect =findViewById(R.id.img_collect);
  56. imgCollect.setOnClickListener(this);
  57. //初始化答题卡按钮
  58. imgCard=findViewById(R.id.img_card);
  59. imgCard.setOnClickListener(this);
  60. //初始化计时器
  61. chronometer = findViewById(R.id.mytime);
  62. chronometer.setBase(SystemClock.elapsedRealtime());
  63. chronometer.start();
  64. chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
  65. @Override
  66. public void onChronometerTick(Chronometer chronometer) {
  67. if (SystemClock.elapsedRealtime() - chronometer.getBase() == 1.5 * 360 * 1000) {
  68. Toast.makeText(QuestionActivity.this, "考试时间到", Toast.LENGTH_LONG).show();
  69. saveExam();
  70. }
  71. }
  72. });
  73. //获取题目集关键字
  74. String field = QuestionFragment.field;
  75. String value = QuestionFragment.value;
  76. source = value;
  77. //设置标题
  78. tvTitle = findViewById(R.id.tv_title);
  79. tvTitle.setText(source);
  80. //获取SQLite数据库中题库数据
  81. if(tab==MyTag.QUE)
  82. cursor = ToolHelper.loadDB(this,
  83. "select que.* from "+table+" where " + field + "='" + value + "' order by type");
  84. else
  85. cursor = ToolHelper.loadDB(this,
  86. "select que.* from "+table+" and " + field + "='" + value + "' order by type");
  87. num = cursor.getCount();
  88. //答案List初始化
  89. anList = new ArrayList<>();
  90. for (int i = 0; i < num; i++) {
  91. anList.add("");
  92. }
  93. //设置进度条
  94. pb = findViewById(R.id.pb);
  95. pb.setMax(num-1);
  96. pb.setProgress(0);
  97. //前后按钮
  98. imgPre = findViewById(R.id.img_pre);
  99. imgNext = findViewById(R.id.img_next);
  100. imgPre.setOnClickListener(this);
  101. imgNext.setOnClickListener(this);
  102. //设置初始分数
  103. tvScore =findViewById(R.id.tv_num);
  104. tvScore.setText("得分:" + String.valueOf(score )+ "/" +String.valueOf( num));
  105. //设置ViewFlipper
  106. vf=findViewById(R.id.vf);
  107. adapter=new BaseAdapter() {
  108. @Override
  109. public int getCount() {
  110. return num;
  111. }
  112. @Override
  113. public Object getItem(int position) {
  114. return position;
  115. }
  116. @Override
  117. public long getItemId(int position) {
  118. return position;
  119. }
  120. @Override
  121. public View getView(int position, View convertView, ViewGroup parent) {
  122. index=position;
  123. createView(position);
  124. return root;
  125. }
  126. };
  127. vf.setAdapter(adapter);
  128. }
  129. //答题卡设置
  130. private void createView(int pos) {
  131. root = LayoutInflater.from(QuestionActivity.this).inflate(R.layout.queitem, null);
  132. tvQue = root.findViewById(R.id.tv_que1);
  133. cb1 = root.findViewById(R.id.cb_choice1);
  134. cb2 = root.findViewById(R.id.cb_choice2);
  135. cb3 = root.findViewById(R.id.cb_choice3);
  136. cb4 = root.findViewById(R.id.cb_choice4);
  137. tvAns = root.findViewById(R.id.tv_answer1);
  138. tvDetail = root.findViewById(R.id.tv_detail1);
  139. tvYou = root.findViewById(R.id.tv_you);
  140. //获取数据
  141. cursor.moveToPosition(pos);
  142. type = cursor.getString(cursor.getColumnIndex("type"));
  143. que = cursor.getString(cursor.getColumnIndex("que"));
  144. A = "A."+cursor.getString(cursor.getColumnIndex("choiceA"));
  145. B = "B."+cursor.getString(cursor.getColumnIndex("choiceB"));
  146. C = "C."+cursor.getString(cursor.getColumnIndex("choiceC"));
  147. D = "D."+cursor.getString(cursor.getColumnIndex("choiceD"));
  148. answer = cursor.getString(cursor.getColumnIndex("answer"));
  149. detail = cursor.getString(cursor.getColumnIndex("detail"));
  150. qid = cursor.getString(cursor.getColumnIndex("_id"));
  151. //加载内容
  152. tvQue.setText((pos + 1) + ".(" + type + ")" + que);
  153. cb1.setText(A);
  154. cb2.setText(B);
  155. cb3.setText(C);
  156. cb4.setText(D);
  157. cb1.setButtonDrawable(R.drawable.cb);
  158. cb2.setButtonDrawable(R.drawable.cb);
  159. cb3.setButtonDrawable(R.drawable.cb);
  160. cb4.setButtonDrawable(R.drawable.cb);
  161. cb1.setEnabled(true);
  162. cb2.setEnabled(true);
  163. cb3.setEnabled(true);
  164. cb4.setEnabled(true);
  165. cb1.setChecked(false);
  166. cb2.setChecked(false);
  167. cb3.setChecked(false);
  168. cb4.setChecked(false);
  169. tvAns.setText("【正确答案】" + answer);
  170. tvDetail.setText("【解析】" + detail);
  171. if (anList.get(pos).equals("")) {
  172. tvAns.setVisibility(View.GONE);
  173. tvYou.setVisibility(View.GONE);
  174. tvDetail.setVisibility(View.GONE);
  175. } else {
  176. //已答题设置为不可操作
  177. disableChecked(pos);
  178. }
  179. //设置当前进度
  180. pb.setProgress(pos);
  181. //设置是否被收藏
  182. if(queCollect()){
  183. isCollect=true;
  184. imgCollect.setImageResource(R.drawable.star_on);
  185. }else {
  186. isCollect=false;
  187. imgCollect.setImageResource(R.drawable.star1);
  188. }
  189. //滑动切换
  190. root.setOnTouchListener(new View.OnTouchListener() {
  191. @Override
  192. public boolean onTouch(View v, MotionEvent event) {
  193. float startX=v.getWidth()/2,endX=v.getWidth()/2,min=100;
  194. switch (event.getAction()){
  195. case MotionEvent.ACTION_DOWN:
  196. startX=event.getX();
  197. case MotionEvent.ACTION_UP:
  198. endX=event.getX();
  199. break;
  200. }
  201. if (startX - endX > min) {
  202. vf.showNext();
  203. }else if (endX - startX > min) {
  204. vf.showPrevious();
  205. }
  206. return true;
  207. }
  208. });
  209. }
  210. //判断选择答案对错
  211. private void isAnswerTrue(int pos) {
  212. if (cb1.isChecked() || cb2.isChecked() || cb3.isChecked() || cb4.isChecked()) {
  213. //获取答案
  214. StringBuffer sb = new StringBuffer();
  215. if (cb1.isChecked()) sb.append("A");
  216. if (cb2.isChecked()) sb.append("B");
  217. if (cb3.isChecked()) sb.append("C");
  218. if (cb4.isChecked()) sb.append("D");
  219. String you = sb.toString();
  220. //保存答案
  221. anList.set(pos, you);
  222. //判断对错
  223. if (you.equals(answer)) {
  224. moveCorrect();
  225. } else {
  226. //错误则保存错题,显示答案
  227. saveWrong(sb.toString());
  228. disableChecked(pos);
  229. }
  230. }else {
  231. Toast.makeText(QuestionActivity.this, "请选择答案", Toast.LENGTH_SHORT).show();
  232. }
  233. }
  234. //移除正确题目
  235. @SuppressLint("SetTextI18n")
  236. private void moveCorrect() {
  237. score++;
  238. tvScore.setText("得分:" + String.valueOf(score )+ "/" +String.valueOf( num));
  239. vf.showNext();
  240. int c=ToolHelper.loadDB(this,"select _id from wrong where qid="+qid).getCount();
  241. if(c>0)
  242. ToolHelper.excuteDB(this, "delete from wrong where qid=" +qid);
  243. }
  244. //已做题不可再做
  245. private void disableChecked(int pos) {
  246. tvYou.setText("【你的答案】" + anList.get(pos));
  247. tvAns.setVisibility(View.VISIBLE);
  248. tvDetail.setVisibility(View.VISIBLE);
  249. tvYou.setVisibility(View.VISIBLE);
  250. if (answer.contains("A")) cb1.setButtonDrawable(R.drawable.cb_right);
  251. if (answer.contains("B")) cb2.setButtonDrawable(R.drawable.cb_right);
  252. if (answer.contains("C")) cb3.setButtonDrawable(R.drawable.cb_right);
  253. if (answer.contains("D")) cb4.setButtonDrawable(R.drawable.cb_right);
  254. //设置为不可答题
  255. cb1.setEnabled(false);
  256. cb2.setEnabled(false);
  257. cb3.setEnabled(false);
  258. cb4.setEnabled(false);
  259. }
  260. //保存错题
  261. private void saveWrong(String ans) {
  262. int c=ToolHelper.loadDB(this,"select _id from wrong where qid="+qid).getCount();
  263. if(c==0) {
  264. Date date = new Date();
  265. SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  266. String mydate = ft.format(date);
  267. ToolHelper.excuteDB(this,
  268. "insert into wrong (_id,qid,answer,anTime) values (" + String.valueOf(Math.random() * 10000) + "," + qid + ",'" + ans + "','" + mydate + "')");
  269. }
  270. }
  271. //判断当前题目是否被收藏
  272. private boolean queCollect() {
  273. int c=ToolHelper.loadDB(this,"select _id from collection where qid="+qid).getCount();
  274. if(c>0) return true;
  275. else return false;
  276. }
  277. //保存考试记录
  278. private void saveExam() {
  279. chronometer.stop();
  280. Date date = new Date();
  281. SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  282. String mytime = chronometer.getText().toString();
  283. String mydate = ft.format(date);
  284. String title=source+"\n"+"("+content+")";
  285. ToolHelper.excuteDB(this, "insert into exam (_id,title,examTime,score,examDate) values (" + String.valueOf(Math.random()*10000)
  286. +",'" + title + "','" + mytime + "'," + score + ",'" + mydate + "')");
  287. Intent intent = new Intent(this, ResultActivity.class);
  288. intent.putExtra("score", score+"/"+num);
  289. intent.putExtra("time", mytime);
  290. intent.putExtra("date", mydate);
  291. intent.putExtra("title",title);
  292. startActivity(intent);
  293. finish();
  294. }
  295. @Override
  296. public void onClick(View v) {
  297. switch (v.getId()){
  298. case R.id.img_next:
  299. vf.showNext();
  300. break;
  301. case R.id.img_pre:
  302. vf.showPrevious();
  303. break;
  304. case R.id.img_collect://收藏
  305. if(!isCollect){
  306. imgCollect.setImageResource(R.drawable.star_on);
  307. ToolHelper.excuteDB(this,"insert into collection (_id,qid) values ("+String.valueOf(Math.random()*10000)+","+qid+")");
  308. Toast.makeText(this,"成功收藏",Toast.LENGTH_SHORT).show();
  309. isCollect=true;
  310. }else {
  311. imgCollect.setImageResource(R.drawable.star1);
  312. ToolHelper.excuteDB(this,"delete from collection where qid="+qid);
  313. Toast.makeText(this,"取消收藏",Toast.LENGTH_SHORT).show();
  314. isCollect=false;
  315. }
  316. break;
  317. case R.id.img_card:
  318. Intent intent=new Intent(this,CardActivity.class);
  319. intent.putExtra("num",num);
  320. intent.putExtra("from",1);
  321. startActivityForResult(intent,MyTag.CARD);
  322. break;
  323. }
  324. }
  325. @Override
  326. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  327. if(requestCode==MyTag.CARD&&resultCode==MyTag.CARD){
  328. int select=data.getIntExtra("card",0);
  329. moveToItem(select);
  330. }
  331. super.onActivityResult(requestCode, resultCode, data);
  332. }
  333. //跳转到指定题目
  334. private void moveToItem(int t) {
  335. if (t != index) {
  336. if(t>index) {
  337. int d= t-index;
  338. for (int i = 0; i < d + 1; i++)
  339. vf.showNext();
  340. }else if(t<index){
  341. int p=index-t;
  342. for (int i = 0; i < p + 1; i++)
  343. vf.showPrevious();
  344. }
  345. }
  346. }
  347. @Override
  348. public boolean onCreateOptionsMenu(Menu menu) {
  349. getMenuInflater().inflate(R.menu.que, menu);
  350. return super.onCreateOptionsMenu(menu);
  351. }
  352. @Override
  353. public boolean onOptionsItemSelected(MenuItem item) {
  354. switch (item.getItemId()) {
  355. case R.id.que_ok://提交答案
  356. if (index >= num - 1) {
  357. if(!isFirst) {
  358. isAnswerTrue(index);
  359. isFirst = true;
  360. }else {
  361. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  362. builder.setMessage("是否结束测试?");
  363. builder.setNegativeButton("取消", null);
  364. builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
  365. @Override
  366. public void onClick(DialogInterface dialog, int which) {
  367. saveExam();
  368. }
  369. });
  370. builder.show();
  371. }
  372. } else {
  373. isAnswerTrue(index);
  374. }
  375. break;
  376. case android.R.id.home://返回
  377. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  378. builder.setTitle("是否取消测试?");
  379. builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
  380. @Override
  381. public void onClick(DialogInterface dialog, int which) {
  382. }
  383. });
  384. builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
  385. @Override
  386. public void onClick(DialogInterface dialog, int which) {
  387. QuestionActivity.this.finish();
  388. }
  389. });
  390. builder.show();
  391. break;
  392. }
  393. return super.onOptionsItemSelected(item);
  394. }
  395. }
  1. activity_card.xml 题号卡
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. xmlns:tools="http://schemas.android.com/tools"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:orientation="vertical"
  9. tools:context="com.lidan.xiao.danquestion.activity.CardActivity">
  10. <GridView
  11. android:id="@+id/gv_card"
  12. android:layout_width="match_parent"
  13. android:layout_height="match_parent"
  14. android:numColumns="5"/>
  15. </LinearLayout>
  1. carditem.xml
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <TextView
  4. xmlns:android="http://schemas.android.com/apk/res/android"
  5. android:id="@+id/tv_carditem"
  6. android:layout_width="64dp"
  7. android:layout_height="64dp"
  8. android:gravity="center"
  9. android:background="@android:drawable/picture_frame"/>
  1. public class CardActivity extends AppCompatActivity {
  2. private GridView gv;
  3. private int select,num,from;
  4. private TextView selectView=null;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_card);
  9. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  10. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  11. setTitle("选择题号");
  12. Intent intent=getIntent();
  13. num=intent.getIntExtra("num",0);
  14. from=intent.getIntExtra("from",1);
  15. createCard();
  16. }
  17. private void createCard() {
  18. gv=findViewById(R.id.gv_card);
  19. BaseAdapter adapter=new BaseAdapter() {
  20. @Override
  21. public int getCount() {
  22. return num;
  23. }
  24. @Override
  25. public Object getItem(int position) {
  26. return position;
  27. }
  28. @Override
  29. public long getItemId(int position) {
  30. return position;
  31. }
  32. @Override
  33. public View getView(int position, View convertView, ViewGroup parent) {
  34. View view= LayoutInflater.from(CardActivity.this).inflate(R.layout.carditem,null);
  35. TextView tv=view.findViewById(R.id.tv_carditem);
  36. tv.setText(String.valueOf(position+1));
  37. if(from==1){
  38. if(!QuestionActivity.anList.get(position).equals("")){
  39. tv.setTextColor(getResources().getColor(R.color.colorAccent));
  40. }else {
  41. tv.setTextColor(getResources().getColor(R.color.gray));
  42. }
  43. }else if(from==2){
  44. if(!PracticeActivity.anList.get(position).equals("")){
  45. tv.setTextColor(getResources().getColor(R.color.colorAccent));
  46. }else {
  47. tv.setTextColor(getResources().getColor(R.color.gray));
  48. }
  49. }
  50. return view;
  51. }
  52. };
  53. gv.setAdapter(adapter);
  54. gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  55. @Override
  56. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  57. if(selectView!=null) {
  58. selectView.setTextColor(getResources().getColor(R.color.gray));
  59. }
  60. ((TextView) view).setTextColor(getResources().getColor(R.color.colorAccent));
  61. selectView= (TextView) view;
  62. select=position;
  63. selectCard();
  64. }
  65. });
  66. }
  67. @Override
  68. public boolean onOptionsItemSelected(MenuItem item) {
  69. switch (item.getItemId()){
  70. case android.R.id.home:
  71. finish();
  72. break;
  73. }
  74. return super.onOptionsItemSelected(item);
  75. }
  76. private void selectCard() {
  77. Intent intent1 = new Intent(this, CardActivity.class);
  78. intent1.putExtra("card", select);
  79. //返回数据到前一个Activity
  80. setResult(MyTag.CARD, intent1);
  81. finish();
  82. }
  83. }
  1. activity_result.xml测试成绩
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. xmlns:tools="http://schemas.android.com/tools"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:orientation="vertical"
  9. android:fitsSystemWindows="true"
  10. tools:context="com.lidan.xiao.danquestion.activity.ResultActivity">
  11. <include layout="@layout/layout_top2"/>
  12. <LinearLayout
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:background="@android:drawable/picture_frame"
  16. android:orientation="vertical"
  17. android:padding="16dp">
  18. <TextView
  19. android:id="@+id/tv_title1"
  20. android:layout_width="match_parent"
  21. android:layout_height="wrap_content"
  22. android:gravity="center"
  23. android:padding="8dp"
  24. android:textColor="@color/colorPrimaryDark"
  25. android:textSize="14sp" />
  26. <TextView
  27. android:id="@+id/tv_score"
  28. android:layout_width="match_parent"
  29. android:layout_height="wrap_content"
  30. android:layout_marginLeft="32dp"
  31. android:layout_marginTop="8dp"
  32. android:text="得 分:"
  33. android:textColor="@color/colorAccent"
  34. android:textSize="16sp" />
  35. <TextView
  36. android:id="@+id/tv_time"
  37. android:layout_width="match_parent"
  38. android:layout_height="wrap_content"
  39. android:layout_marginLeft="32dp"
  40. android:layout_marginTop="8dp"
  41. android:text="答题时间:"
  42. android:textColor="@color/gray"
  43. android:textSize="16sp" />
  44. <TextView
  45. android:id="@+id/tv_date"
  46. android:layout_width="match_parent"
  47. android:layout_height="wrap_content"
  48. android:layout_marginLeft="32dp"
  49. android:layout_marginTop="8dp"
  50. android:text="提交时间:"
  51. android:textColor="@color/gray"
  52. android:textSize="16sp" />
  53. </LinearLayout>
  54. <Button
  55. android:id="@+id/bt_record"
  56. android:layout_width="match_parent"
  57. android:layout_height="wrap_content"
  58. android:padding="16dp"
  59. android:background="@drawable/bt"
  60. android:textColor="@color/white"
  61. android:text="查看考试记录"
  62. android:layout_margin="8dp"/>
  63. </LinearLayout>
  1. public class ResultActivity extends AppCompatActivity {
  2. private String title,date,time,score;
  3. private TextView tvTitle,tvScore,tvDate,tvTime;
  4. private Button bt;
  5. @Override
  6. protected void onCreate(@Nullable Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_result);
  9. //ActionBar工具栏设置
  10. Toolbar toolbar = findViewById(R.id.toolbar2);
  11. setSupportActionBar(toolbar);
  12. getSupportActionBar().setHomeButtonEnabled(true);
  13. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  14. Intent intent=getIntent();
  15. title=intent.getStringExtra("title");
  16. date=intent.getStringExtra("date");
  17. time=intent.getStringExtra("time");
  18. score=intent.getStringExtra("score");
  19. tvTitle=findViewById(R.id.tv_title1);
  20. tvScore=findViewById(R.id.tv_score);
  21. tvDate=findViewById(R.id.tv_date);
  22. tvTime=findViewById(R.id.tv_time);
  23. tvTitle.setText(title);
  24. tvScore.append(score);
  25. tvDate.append(date);
  26. tvTime.append(time);
  27. setTitle("测试成绩");
  28. bt=findViewById(R.id.bt_record);
  29. bt.setOnClickListener(new View.OnClickListener() {
  30. @Override
  31. public void onClick(View v) {
  32. Intent intent1=new Intent(ResultActivity.this,ExamActivity.class);
  33. startActivity(intent1);
  34. }
  35. });
  36. }
  37. @Override
  38. public boolean onOptionsItemSelected(MenuItem item) {
  39. switch (item.getItemId()){
  40. case android.R.id.home:
  41. finish();
  42. break;
  43. }
  44. return super.onOptionsItemSelected(item);
  45. }
  46. }

4.项目GitHut地址

https://github.com/xiaolidan00/question

微信关注 “安卓集中营”,获取更多

或者扫码关注

一起共同学习探讨

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/995908
推荐阅读
相关标签
  

闽ICP备14008679号