Android应用-猜数字小游戏

末蓝、 2023-01-01 15:57 262阅读 0赞

描述:
猜数字游戏包含“猜数”、“排行榜”和“设置” 3个模块。
1)“猜数”模块:随机生成一定范围内的一个整数,用户可以输入自己的猜想数字,程序进行判断并给出“猜大”、“猜小”或“正确”的3种提示
2)“排行榜”模块:根据用户猜对数字的次数进行排名显示。
3)“设置”模块:在这个模块,用户可以设置自己的用户名,也可以选择是否使用“默认姓名”进行后面的每一次猜数游戏
如果使用,后面则直接进行游戏,
如果不使用,后面的每次游戏结束后程序会提示要输入用户名,以便记录游戏历史,并在“排行榜”模块显示。
在“设置”模块,用户还可以选择接下来的要进行的猜数游戏的猜数范围,本次给出1-100,1-1000,1-10000三种范围,大家后期可以根据需要修改调整。
一、程序整体实现效果是这样的:
在这里插入图片描述
在这里插入图片描述在这里插入图片描述在这里插入图片描述
无默认姓名猜对时:
在这里插入图片描述在这里插入图片描述
二、整个项目的结构分析:
先放一张整个Project目录结构:
在这里插入图片描述res下的图片素材:
在这里插入图片描述

接下来,逐一对每个模块实现步骤进行讲解!
【数据库】
新建一个辅助类MyHelper类继承SQLiteOpenHelper类,并实现它的onCreate方法和onUpgrade方法。

  1. package com.example.mygame;
  2. import android.content.Context;
  3. import android.database.sqlite.SQLiteDatabase;
  4. import android.database.sqlite.SQLiteOpenHelper;
  5. public class MyHelper extends SQLiteOpenHelper {
  6. public MyHelper(Context context,String name,SQLiteDatabase.CursorFactory factory,int version){
  7. super(context,name,factory,version);
  8. }
  9. @Override
  10. public void onCreate(SQLiteDatabase db) {
  11. db.execSQL("create table ordertb(_id integer primary key "+"autoincrement," +
  12. "name string not null,count integer not null,range integer not null)");
  13. }
  14. @Override
  15. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  16. }
  17. }

下方导航栏的实现】

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context=".MainActivity">
  7. <LinearLayout
  8. android:id="@+id/linearLayout"
  9. android:layout_width="match_parent"
  10. android:layout_height="60dp"
  11. android:layout_alignParentLeft="true"
  12. android:layout_alignParentBottom="true"
  13. android:layout_alignParentStart="true"
  14. android:background="#E8EEF5"
  15. android:baselineAligned="false"
  16. tools:ignore="Orientation"
  17. >
  18. <!--猜数-->
  19. <LinearLayout
  20. android:id="@+id/llay_guess"
  21. android:layout_width="0dp"
  22. android:layout_height="match_parent"
  23. android:layout_weight="1"
  24. android:gravity="center"
  25. android:background="#E8EEF5"
  26. android:orientation="vertical">
  27. <ImageView
  28. android:id="@+id/img_guess"
  29. android:layout_width="40dp"
  30. android:layout_height="40dp"
  31. android:src="@drawable/game_on"/>
  32. <TextView
  33. android:id="@+id/txt_guess"
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:text="@string/txt_guess"
  37. android:textSize="20dp"
  38. android:textStyle="bold"
  39. android:textColor="@color/color_txt_pressed"/>
  40. </LinearLayout>
  41. <LinearLayout
  42. android:layout_width="2dp"
  43. android:layout_height="match_parent"
  44. android:divider="@color/color_txt_pressed"
  45. android:showDividers="end">
  46. </LinearLayout>
  47. <!--排行榜-->
  48. <LinearLayout
  49. android:id="@+id/llay_order"
  50. android:layout_width="0dp"
  51. android:layout_height="match_parent"
  52. android:layout_weight="1"
  53. android:orientation="vertical"
  54. android:gravity="center">
  55. <ImageView
  56. android:id="@+id/img_order"
  57. android:layout_width="40dp"
  58. android:layout_height="40dp"
  59. android:src="@drawable/rank_off"
  60. android:layout_gravity="center_horizontal"/>
  61. <TextView
  62. android:id="@+id/txt_order"
  63. android:layout_width="wrap_content"
  64. android:layout_height="wrap_content"
  65. android:text="@string/txt_order"
  66. android:textSize="20dp"
  67. android:textStyle="bold"
  68. android:textColor="@color/color_txt_normal"/>
  69. </LinearLayout>
  70. <!--设置-->
  71. <LinearLayout
  72. android:id="@+id/llay_setting"
  73. android:layout_width="0dp"
  74. android:layout_height="match_parent"
  75. android:layout_weight="1"
  76. android:orientation="vertical"
  77. android:gravity="center">
  78. <ImageView
  79. android:id="@+id/img_setting"
  80. android:layout_width="40dp"
  81. android:layout_height="40dp"
  82. android:src="@drawable/setting_off"/>
  83. <TextView
  84. android:id="@+id/txt_setting"
  85. android:layout_width="wrap_content"
  86. android:layout_height="wrap_content"
  87. android:text="@string/txt_setting"
  88. android:textSize="20dp"
  89. android:textStyle="bold"
  90. android:textColor="@color/color_txt_normal"/>
  91. </LinearLayout>
  92. </LinearLayout>
  93. <FrameLayout
  94. android:id="@+id/fragment_content"
  95. android:layout_width="match_parent"
  96. android:layout_height="match_parent"
  97. android:layout_above="@id/linearLayout"
  98. android:layout_alignParentLeft="true"
  99. android:layout_alignParentStart="true"/>
  100. </RelativeLayout>

布局中涉及的“strings.xml”文件如下:

  1. <string name="txt_guess">猜数</string>
  2. <string name="txt_order">排行榜</string>
  3. <string name="txt_setting">设置</string>

布局中涉及的“colors.xml”文件如下:

  1. <color name="color_txt_normal">#737373</color>
  2. <color name="color_txt_pressed">#d4257b</color>

在MainActivity中实现导航逻辑之前,先搭个基本页面框架:
新建GuessFragment(猜数字页)、OrderFragment(积分排行榜页)和SettingFragment(设置页)3个类,以及对应的布局文件“fragment_guess.xml”、“fragment_order.xml”和“fragment_setting.xml”。
在MainActivity中的代码如下:

  1. package com.example.mygame;
  2. import android.support.v4.app.Fragment;
  3. import android.support.v4.app.FragmentManager;
  4. import android.support.v4.app.FragmentTransaction;
  5. import android.support.v4.content.ContextCompat;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.widget.ImageView;
  10. import android.widget.LinearLayout;
  11. import android.widget.TextView;
  12. public class MainActivity extends AppCompatActivity {
  13. private LinearLayout ILay_guess,ILay_order,ILay_setting;
  14. private ImageView img_guess,img_order,img_setting;
  15. private TextView txt_guess,txt_order,txt_setting;
  16. private Fragment fm_guess,fm_order,fm_setting;
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_main);
  21. initView();
  22. setDefaultFragment();
  23. setOnClickLisener();
  24. }
  25. private void initView(){
  26. ILay_guess = findViewById(R.id.llay_guess);
  27. ILay_order = findViewById(R.id.llay_order);
  28. ILay_setting = findViewById(R.id.llay_setting);
  29. img_guess = findViewById(R.id.img_guess);
  30. img_order = findViewById(R.id.img_order);
  31. img_setting = findViewById(R.id.img_setting);
  32. txt_guess = findViewById(R.id.txt_guess);
  33. txt_order = findViewById(R.id.txt_order);
  34. txt_setting = findViewById(R.id.txt_setting);
  35. }
  36. private void setDefaultFragment(){
  37. FragmentManager fm = getSupportFragmentManager();
  38. FragmentTransaction transaction = fm.beginTransaction();
  39. fm_guess = new GuessFragment();
  40. transaction.replace(R.id.fragment_content,fm_guess,"GUESS");
  41. transaction.commit();
  42. }
  43. private void setOnClickLisener(){
  44. ILay_guess.setOnClickListener(new LinearLayoutOnClickLisener());
  45. ILay_order.setOnClickListener(new LinearLayoutOnClickLisener());
  46. ILay_setting.setOnClickListener(new LinearLayoutOnClickLisener());
  47. }
  48. private class LinearLayoutOnClickLisener implements View.OnClickListener{
  49. @Override
  50. public void onClick(View v) {
  51. FragmentManager fm = getSupportFragmentManager();
  52. FragmentTransaction transaction = fm.beginTransaction();
  53. resetLinearLayout();
  54. switch (v.getId()){
  55. case R.id.llay_guess:
  56. if (fm_guess == null){
  57. fm_guess = new GuessFragment();
  58. }
  59. img_guess.setImageResource(R.drawable.game_on);
  60. txt_guess.setTextColor(ContextCompat.getColor(MainActivity.this,R.color.color_txt_pressed));
  61. transaction.replace(R.id.fragment_content,fm_guess,"GUESS");
  62. break;
  63. case R.id.llay_order:
  64. if (fm_order == null){
  65. fm_order = new OrderFragment();
  66. }
  67. img_order.setImageResource(R.drawable.rank_on);
  68. txt_order.setTextColor(ContextCompat.getColor(MainActivity.this,R.color.color_txt_pressed));
  69. transaction.replace(R.id.fragment_content,fm_order,"ORDER");
  70. break;
  71. case R.id.llay_setting:
  72. if (fm_setting == null){
  73. fm_setting = new SettingFragment();
  74. }
  75. img_setting.setImageResource(R.drawable.setting_on);
  76. txt_setting.setTextColor(ContextCompat.getColor(MainActivity.this,R.color.color_txt_pressed));
  77. transaction.replace(R.id.fragment_content,fm_setting,"SETTING");
  78. break;
  79. }
  80. transaction.commit();
  81. }
  82. }
  83. private void resetLinearLayout(){
  84. img_guess.setImageResource(R.drawable.game_off);
  85. img_order.setImageResource(R.drawable.rank_off);
  86. img_setting.setImageResource(R.drawable.setting_off);
  87. txt_guess.setTextColor(ContextCompat.getColor(MainActivity.this,R.color.color_txt_normal));
  88. txt_order.setTextColor(ContextCompat.getColor(MainActivity.this,R.color.color_txt_normal));
  89. txt_setting.setTextColor(ContextCompat.getColor(MainActivity.this,R.color.color_txt_normal));
  90. }
  91. }

【“设置”模块的实现】
在对应的布局文件“fragment_setting.xml”中,代码布局如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".SettingFragment"
  8. android:orientation="vertical">
  9. <TextView
  10. android:id="@+id/txt_name"
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:text="@string/txt_name"
  14. android:textSize="22dp"/>
  15. <EditText
  16. android:id="@+id/edit_defaultName"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:layout_marginTop="10dp"
  20. android:padding="8dp"
  21. android:hint="@string/edit_name"
  22. android:textSize="22dp" />
  23. <CheckBox
  24. android:id="@+id/chb_name"
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:layout_marginTop="10dp"
  28. android:text="@string/chb_useDefaultName"
  29. android:textSize="22dp"
  30. />
  31. <TextView
  32. android:id="@+id/txt_defaultRange"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:layout_marginTop="10dp"
  36. android:text="@string/txt_defaultRange"
  37. android:textSize="22dp"/>
  38. <Spinner
  39. android:id="@+id/spinner"
  40. android:layout_width="match_parent"
  41. android:layout_height="wrap_content"
  42. android:entries="@array/range_array"
  43. android:layout_marginTop="10dp"/>
  44. <Button
  45. android:id="@+id/btn_save"
  46. android:layout_width="match_parent"
  47. android:layout_height="wrap_content"
  48. android:layout_marginTop="10dp"
  49. android:text="@string/btn_setting"
  50. android:textSize="22dp"/>
  51. </LinearLayout>

布局文件中涉及的“strings.xml”文件内容如下:

  1. <string name="txt_name">请输入默认姓名:</string>
  2. <string name="chb_useDefaultName">使用默认姓名:</string>
  3. <string name="txt_defaultRange">默认范围:</string>
  4. <string name="btn_setting">保存设置</string>
  5. <string name="edit_name">默认姓名</string>
  6. <string-array name="range_array">
  7. <item>100</item>
  8. <item>1000</item>
  9. <item>10000</item>
  10. </string-array>

该模块的逻辑实现代码在“SettingFragment”类中实现:

  1. package com.example.mygame;
  2. import android.content.Context;
  3. import android.content.SharedPreferences;
  4. import android.support.v4.app.Fragment;
  5. import android.os.Bundle;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.widget.Button;
  10. import android.widget.CheckBox;
  11. import android.widget.EditText;
  12. import android.widget.Spinner;
  13. import android.widget.Toast;
  14. public class SettingFragment extends Fragment{
  15. private EditText edit_name;
  16. private Button btn_save;
  17. private CheckBox chb_defaultName;
  18. private Spinner spinner;
  19. private Context context;
  20. @Override
  21. public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
  22. View view = inflater.inflate(R.layout.fragment_setting,container,false);
  23. context = view.getContext();
  24. initView(view);
  25. btn_save.setOnClickListener(new ButtonOnClickLisener());
  26. return view;
  27. }
  28. private void initView(View view){
  29. edit_name = view.findViewById(R.id.edit_defaultName);
  30. btn_save = view.findViewById(R.id.btn_save);
  31. chb_defaultName = view.findViewById(R.id.chb_name);
  32. spinner = view.findViewById(R.id.spinner);
  33. }
  34. public class ButtonOnClickLisener implements View.OnClickListener{
  35. @Override
  36. public void onClick(View v) {
  37. SharedPreferences sharedPreferences = context.getSharedPreferences("Info",Context.MODE_PRIVATE);
  38. SharedPreferences.Editor editor = sharedPreferences.edit();
  39. int defaultRange = Integer.valueOf(spinner.getSelectedItem().toString());
  40. editor.putInt("range",defaultRange);
  41. if (chb_defaultName.isChecked()){
  42. String defaultName = edit_name.getText().toString();
  43. if (defaultName.isEmpty()){
  44. Toast.makeText(context,"请输入默认姓名!",Toast.LENGTH_SHORT).show();
  45. return;
  46. }
  47. editor.putString("name",defaultName);
  48. }
  49. else {
  50. editor.remove("name");
  51. }
  52. if (editor.commit()){
  53. Toast.makeText(context,"保存成功!",Toast.LENGTH_SHORT).show();
  54. }
  55. }
  56. }
  57. }

完成可执行run,在模拟器跑一下。
【“猜数”模块的实现】
猜数模块布局文件”fragment_guess.xml”内容如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".GuessFragment"
  8. android:background="#ffffff"
  9. android:orientation="vertical">
  10. <TextView
  11. android:id="@+id/txt_range"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:layout_margin="5dp"
  15. android:textSize="22dp"/>
  16. <EditText
  17. android:id="@+id/edit_num"
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content"
  20. android:layout_marginTop="10dp"
  21. android:layout_below="@id/txt_range"
  22. android:padding="8dp"
  23. android:inputType="numberSigned"
  24. android:textSize="22dp"/>
  25. <Button
  26. android:id="@+id/btn_submit"
  27. android:layout_width="match_parent"
  28. android:layout_height="wrap_content"
  29. android:layout_marginTop="10dp"
  30. android:layout_below="@id/edit_num"
  31. android:textSize="22dp"/>
  32. <TextView
  33. android:id="@+id/txt_compare"
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:layout_marginTop="10dp"
  37. android:textSize="22dp"
  38. android:layout_below="@id/btn_submit"
  39. android:layout_centerHorizontal="true"/>
  40. <ImageView
  41. android:id="@+id/img_broad"
  42. android:layout_width="60dp"
  43. android:layout_height="60dp"
  44. android:layout_margin="10dp"
  45. android:layout_toStartOf="@id/txt_compare"
  46. android:layout_toLeftOf="@id/txt_compare"
  47. android:layout_below="@id/btn_submit"/>
  48. <TextView
  49. android:id="@+id/txt_count"
  50. android:layout_width="wrap_content"
  51. android:layout_height="wrap_content"
  52. android:layout_marginTop="10dp"
  53. android:textSize="22dp"
  54. android:layout_below="@id/txt_compare"
  55. android:layout_alignLeft="@id/txt_compare"
  56. android:layout_alignStart="@id/txt_compare"
  57. />
  58. <ImageView
  59. android:id="@+id/img_bingo"
  60. android:layout_width="wrap_content"
  61. android:layout_height="wrap_content"
  62. android:layout_below="@id/img_broad"
  63. android:layout_centerHorizontal="true"
  64. android:layout_marginTop="10dp"
  65. android:src="@drawable/bingo"/>
  66. </RelativeLayout>

在GuessFragment类中实现猜数判断逻辑:

  1. package com.example.mygame;
  2. import android.content.ContentValues;
  3. import android.content.Context;
  4. import android.content.SharedPreferences;
  5. import android.database.sqlite.SQLiteDatabase;
  6. import android.support.v4.app.Fragment;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.os.Bundle;
  9. import android.view.LayoutInflater;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import android.widget.Button;
  13. import android.widget.EditText;
  14. import android.widget.ImageView;
  15. import android.widget.TextView;
  16. import android.widget.Toast;
  17. import java.util.Random;
  18. public class GuessFragment extends Fragment {
  19. private int count,rndNum;
  20. private EditText edit_num;
  21. private TextView txt_range,txt_compare,txt_count;
  22. private Button btn_submit;
  23. private ImageView img_broad,img_bingo;
  24. private boolean flag;
  25. private String name;
  26. public int range = 0;
  27. private Context context;
  28. @Override
  29. public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){
  30. View view = inflater.inflate(R.layout.fragment_guess,container,false);
  31. context = view.getContext();
  32. initView(view);
  33. initData();
  34. btn_submit.setOnClickListener(new ButtonOnClickLisener());
  35. return view;
  36. }
  37. private void initView(View view){
  38. edit_num = view.findViewById(R.id.edit_num);
  39. txt_range = view.findViewById(R.id.txt_range);
  40. txt_compare = view.findViewById(R.id.txt_compare);
  41. txt_count = view.findViewById(R.id.txt_count);
  42. btn_submit = view.findViewById(R.id.btn_submit);
  43. img_broad = view.findViewById(R.id.img_broad);
  44. img_bingo = view.findViewById(R.id.img_bingo);
  45. }
  46. public void initData(){
  47. SharedPreferences sharedPreferences = context.getSharedPreferences("Info",Context.MODE_PRIVATE);
  48. name = sharedPreferences.getString("name","");
  49. range = sharedPreferences.getInt("range",100);
  50. Random mRandom = new Random();
  51. rndNum = mRandom.nextInt(range)+1;
  52. txt_range.setText("请输入1-"+range+"之间的整数");
  53. txt_count.setText("");
  54. txt_compare.setText("");
  55. img_broad.setVisibility(View.INVISIBLE);
  56. img_bingo.setVisibility(View.INVISIBLE);
  57. btn_submit.setText("提交");
  58. flag = true;
  59. count = 0;
  60. }
  61. private class ButtonOnClickLisener implements View.OnClickListener{
  62. @Override
  63. public void onClick(View v) {
  64. if (flag){
  65. String str_num = edit_num.getText().toString();
  66. if (str_num.isEmpty()){
  67. Toast.makeText(context,"请输入数字!",Toast.LENGTH_SHORT).show();
  68. return;
  69. }
  70. edit_num.setText("");
  71. int numInput = Integer.valueOf(str_num);
  72. if (numInput<1 || numInput > range){
  73. Toast.makeText(context,"请输入范围内的数字!",Toast.LENGTH_SHORT).show();
  74. return;
  75. }
  76. count += 1;
  77. img_broad.setVisibility(View.VISIBLE);
  78. txt_count.setText("猜数次数:"+count);
  79. if (numInput > rndNum){
  80. txt_compare.setText("输入的"+str_num+"太大了!");
  81. }else if(numInput < rndNum){
  82. txt_compare.setText("输入的"+str_num+"太小了!");
  83. }else{
  84. txt_compare.setText("恭喜你猜对啦!正确答案是:"+rndNum+"!");
  85. img_bingo.setVisibility(View.VISIBLE);
  86. flag = false;
  87. btn_submit.setText("再猜一次");
  88. if (name.isEmpty()){
  89. final CustomDialog dialog = new CustomDialog(context,R.style.MyDialog);
  90. dialog.show();
  91. Button btn_confirm = dialog.findViewById(R.id.btn_confirm);
  92. final EditText edit_name = dialog.findViewById(R.id.edit_inputName);
  93. btn_confirm.setOnClickListener(new View.OnClickListener() {
  94. @Override
  95. public void onClick(View v) {
  96. name = edit_name.getText().toString();
  97. if (!name.isEmpty()){
  98. insertData();
  99. dialog.dismiss();
  100. }
  101. }
  102. });
  103. } else {
  104. insertData();
  105. }
  106. }
  107. }else {
  108. initData();
  109. }
  110. }
  111. }
  112. private void insertData(){
  113. MyHelper helper = new MyHelper(context,"Guess.db",null,1);
  114. SQLiteDatabase db = helper.getWritableDatabase();
  115. ContentValues values = new ContentValues();
  116. values.put("name",name);
  117. values.put("count",count);
  118. values.put("range",range);
  119. db.insert("ordertb",null,values);
  120. db.close();
  121. }
  122. }

可以发现,在GuessFragment类中,引入了CustomDialog类,这是自定义的对话框,用于前面基本构思说的,用户在进行猜数游戏时,如果在设置页中,不选择使用默认姓名的话,游戏结束会要求用户输入用户名。
CustomDialog自定义对话框的具体实现如下:
布局文件“alertdialog_input.xml”:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:gravity="center_horizontal"
  6. android:background="#80BCF7"
  7. android:padding="20dp">
  8. <TextView
  9. android:id="@+id/txt_info"
  10. android:layout_width="match_parent"
  11. android:layout_height="50dp"
  12. android:layout_marginTop="5dp"
  13. android:gravity="center"
  14. android:text="@string/txt_dialog_info"
  15. android:textSize="22dp"
  16. android:textColor="#ffffff"
  17. />
  18. <EditText
  19. android:id="@+id/edit_inputName"
  20. android:layout_width="match_parent"
  21. android:layout_height="wrap_content"
  22. android:textSize="20dp"
  23. android:padding="8dp"/>
  24. <Button
  25. android:id="@+id/btn_confirm"
  26. android:layout_width="match_parent"
  27. android:layout_height="wrap_content"
  28. android:text="@string/btn_dialog_confirm"
  29. android:textSize="20dp"/>
  30. </LinearLayout>

布局文件中涉及到的“strings.xml”文件内容如下:

  1. <string name="txt_dialog_info">请输入姓名</string>
  2. <string name="btn_dialog_confirm">确定</string>

CustomDialog类实现代码:

  1. package com.example.mygame;
  2. import android.app.Dialog;
  3. import android.content.Context;
  4. import android.os.Bundle;
  5. import android.view.Gravity;
  6. import android.view.WindowManager;
  7. public class CustomDialog extends Dialog {
  8. private Context context;
  9. public CustomDialog(Context context,int themeResId) {
  10. super(context,themeResId);
  11. this.context = context;
  12. }
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState){
  15. super.onCreate(savedInstanceState);
  16. this.setContentView(R.layout.alertdialog_input);
  17. }
  18. @Override
  19. public void show(){
  20. super.show();
  21. // 设置对话框宽度、对齐方式等
  22. WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
  23. layoutParams.gravity = Gravity.CENTER;
  24. layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
  25. layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
  26. // getWindow().getDecorView().setPadding(0,0,0,0);
  27. getWindow().setAttributes(layoutParams);
  28. }
  29. }

【“排行榜”模块的实现】
该模块布局文件“fragment_order.xml””内容如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context=".OrderFragment">
  7. <android.support.v4.view.ViewPager
  8. android:id="@+id/viewpager"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. >
  12. <android.support.v4.view.PagerTabStrip
  13. android:id="@+id/tabstrip"
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content"/>
  16. </android.support.v4.view.ViewPager>
  17. </RelativeLayout>

新建“viewpager_tab_hundred.xml”布局文件(用于在排行榜页中显示排名,也可以多建几个同样的文件,在排行榜实现分页显示不同范围的猜数游戏有不同的排行榜,这里只在一页显示全部):

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <ListView
  6. android:id="@+id/lv_hundred"
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. >
  10. </ListView>
  11. </LinearLayout>

OrderFragment类逻辑代码实现:

  1. package com.example.mygame;
  2. import android.content.Context;
  3. import android.database.Cursor;
  4. import android.database.sqlite.SQLiteDatabase;
  5. import android.graphics.Color;
  6. import android.support.annotation.NonNull;
  7. import android.support.v4.app.Fragment;
  8. import android.support.v4.view.PagerAdapter;
  9. import android.support.v4.view.PagerTabStrip;
  10. import android.support.v4.view.ViewPager;
  11. import android.os.Bundle;
  12. import android.util.Log;
  13. import android.view.LayoutInflater;
  14. import android.view.View;
  15. import android.view.ViewGroup;
  16. import android.widget.ArrayAdapter;
  17. import android.widget.ListView;
  18. import java.util.ArrayList;
  19. public class OrderFragment extends Fragment {
  20. private ListView lv_hunderd,lv_thousand,lv_tenthousand;
  21. private ViewPager pager;
  22. private Context context;
  23. private PagerTabStrip tabStrip;
  24. private ArrayList<View> viewContainer;
  25. private ArrayList<String> titleContainer;
  26. private ArrayList<String>[] data; //用于为每个pager的ListView准备数据
  27. @Override
  28. public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){
  29. View view = inflater.inflate(R.layout.fragment_order,container,false);
  30. context = view.getContext();
  31. initView(view);
  32. pagersetAdapter();
  33. return view;
  34. }
  35. private void initData(){
  36. MyHelper helper = new MyHelper(context,"Guess.db",null,1);
  37. SQLiteDatabase db = helper.getReadableDatabase();
  38. int[] ranges = {
  39. 100,1000,1000};
  40. data = new ArrayList[3];
  41. data[0] = new ArrayList<>();
  42. data[1] = new ArrayList<>();
  43. data[2] = new ArrayList<>();
  44. for (int i = 0; i < 3; i++) {
  45. Cursor c = db.query("ordertb",null,null,null
  46. ,null,null,"count");
  47. if (c.getCount()>0){
  48. while (c.moveToNext()){
  49. data[i].add("姓名:"+c.getString(c.getColumnIndex("name"))+"\t\t花费次数: "+
  50. c.getString(c.getColumnIndex("count")));
  51. }
  52. }else {
  53. Log.i("info","数据库无数据");
  54. }
  55. c.close();
  56. }
  57. db.close();
  58. }
  59. private void initListView(){
  60. ArrayAdapter<String> adapter_hundred = new ArrayAdapter<String>(context,android.R.layout.simple_expandable_list_item_1,data[0]);
  61. lv_hunderd.setAdapter(adapter_hundred);
  62. ArrayAdapter<String> adapter_thousand = new ArrayAdapter<String>(context,android.R.layout.simple_expandable_list_item_1,data[1]);
  63. lv_hunderd.setAdapter(adapter_thousand);
  64. ArrayAdapter<String> adapter_tenthousand = new ArrayAdapter<String>(context,android.R.layout.simple_expandable_list_item_1,data[2]);
  65. lv_hunderd.setAdapter(adapter_tenthousand);
  66. }
  67. private void initView(View view){
  68. pager = view.findViewById(R.id.viewpager);
  69. tabStrip = view.findViewById(R.id.tabstrip);
  70. tabStrip.setDrawFullUnderline(false);
  71. tabStrip.setBackgroundColor(Color.parseColor("#ccccff"));
  72. tabStrip.setTabIndicatorColor(Color.parseColor("#6666ff"));
  73. LayoutInflater inflater = getActivity().getLayoutInflater();
  74. View view_tab1 = inflater.inflate(R.layout.viewpager_tab_hundred,null);
  75. // View view_tab2 = inflater.inflate(R.layout.viewpager_tab_thousand,null);
  76. // View view_tab3 = inflater.inflate(R.layout.viewpager_tab_tenthousand,null);
  77. lv_hunderd = view_tab1.findViewById(R.id.lv_hundred);
  78. // lv_thousand = view_tab2.findViewById(R.id.lv_thousand);
  79. // lv_tenthousand = view_tab3.findViewById(R.id.lv_tenthousand);
  80. initData();
  81. initListView();
  82. // 添加到ViewPager
  83. viewContainer = new ArrayList<>();
  84. viewContainer.add(view_tab1);
  85. // viewContainer.add(view_tab2);
  86. // viewContainer.add(view_tab3);
  87. // 设置标题
  88. titleContainer = new ArrayList<>();
  89. titleContainer.add("排行榜");
  90. // titleContainer.add("1-1000排行榜");
  91. // titleContainer.add("1-10000排行榜");
  92. }
  93. private void pagersetAdapter(){
  94. pager.setAdapter(new PagerAdapter() {
  95. @Override
  96. public int getCount() {
  97. return viewContainer.size();
  98. }
  99. @Override
  100. public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
  101. return view == object;
  102. }
  103. @Override
  104. public void destroyItem(ViewGroup container,int position,Object object){
  105. ((ViewPager)container).removeView(viewContainer.get(position));
  106. }
  107. @Override
  108. public Object instantiateItem(ViewGroup container,int position){
  109. ((ViewPager)container).addView(viewContainer.get(position));
  110. return viewContainer.get(position);
  111. }
  112. @Override
  113. public int getItemPosition(Object object){
  114. return super.getItemPosition(object);
  115. }
  116. @Override
  117. public CharSequence getPageTitle(int position){
  118. return titleContainer.get(position);
  119. }
  120. });
  121. }
  122. }

到这里,猜数页,猜数页填写用户名的对话框,排行榜,设置页都已经实现了。整个工程也基本完结了。

作者:唐玉金

原文地址

发表评论

表情:
评论列表 (有 0 条评论,262人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Android应用-数字游戏

    描述: 猜数字游戏包含“猜数”、“排行榜”和“设置” 3个模块。 1)“猜数”模块:随机生成一定范围内的一个整数,用户可以输入自己的猜想数字,程序进行判断并给出“猜大”

    相关 Java实现数字游戏

    本文首发于微信公众号:"算法与编程之美",欢迎关注,及时了解更多此系列文章。 在利用Java中猜数字的小游戏,可以深度理解Java中的许多知识,譬如Random,条件语句,数