@Override
public void onClick(View v) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
resetLinearLayout();
switch (v.getId()){
case R.id.llay_guess:
if (fm_guess == null){
fm_guess = new GuessFragment();
}
img_guess.setImageResource(R.drawable.game_on);
txt_guess.setTextColor(ContextCompat.getColor(MainActivity.this,R.color.color_txt_pressed));
transaction.replace(R.id.fragment_content,fm_guess,“GUESS”);
break;
case R.id.llay_order:
if (fm_order == null){
fm_order = new OrderFragment();
}
img_order.setImageResource(R.drawable.rank_on);
txt_order.setTextColor(ContextCompat.getColor(MainActivity.this,R.color.color_txt_pressed));
transaction.replace(R.id.fragment_content,fm_order,“ORDER”);
break;
case R.id.llay_setting:
if (fm_setting == null){
fm_setting = new SettingFragment();
}
img_setting.setImageResource(R.drawable.setting_on);
txt_setting.setTextColor(ContextCompat.getColor(MainActivity.this,R.color.color_txt_pressed));
transaction.replace(R.id.fragment_content,fm_setting,“SETTING”);
break;
}
transaction.commit();
}
}
private void resetLinearLayout(){
img_guess.setImageResource(R.drawable.game_off);
img_order.setImageResource(R.drawable.rank_off);
img_setting.setImageResource(R.drawable.setting_off);
txt_guess.setTextColor(ContextCompat.getColor(MainActivity.this,R.color.color_txt_normal));
txt_order.setTextColor(ContextCompat.getColor(MainActivity.this,R.color.color_txt_normal));
txt_setting.setTextColor(ContextCompat.getColor(MainActivity.this,R.color.color_txt_normal));
}
}
【“设置”模块的实现】
在对应的布局文件“fragment_setting.xml”中,代码布局如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".SettingFragment"
android:orientation=“vertical”>
<TextView
android:id="@+id/txt_name"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text="@string/txt_name"
android:textSize=“22dp”/>
<EditText
android:id="@+id/edit_defaultName"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_marginTop=“10dp”
android:padding=“8dp”
android:hint="@string/edit_name"
android:textSize=“22dp” />
<CheckBox
android:id="@+id/chb_name"
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginTop=“10dp”
android:text="@string/chb_useDefaultName"
android:textSize=“22dp”
/>
<TextView
android:id="@+id/txt_defaultRange"
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginTop=“10dp”
android:text="@string/txt_defaultRange"
android:textSize=“22dp”/>
<Spinner
android:id="@+id/spinner"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:entries="@array/range_array"
android:layout_marginTop=“10dp”/>
<Button
android:id="@+id/btn_save"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_marginTop=“10dp”
android:text="@string/btn_setting"
android:textSize=“22dp”/>
布局文件中涉及的“strings.xml”文件内容如下:
请输入默认姓名:
使用默认姓名:
默认范围:
保存设置
默认姓名
100
1000
10000
该模块的逻辑实现代码在“SettingFragment”类中实现:
package com.example.mygame;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class SettingFragment extends Fragment{
private EditText edit_name;
private Button btn_save;
private CheckBox chb_defaultName;
private Spinner spinner;
private Context context;
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_setting,container,false);
context = view.getContext();
initView(view);
btn_save.setOnClickListener(new ButtonOnClickLisener());
return view;
}
private void initView(View view){
edit_name = view.findViewById(R.id.edit_defaultName);
btn_save = view.findViewById(R.id.btn_save);
chb_defaultName = view.findViewById(R.id.chb_name);
spinner = view.findViewById(R.id.spinner);
}
public class ButtonOnClickLisener implements View.OnClickListener{
@Override
public void onClick(View v) {
SharedPreferences sharedPreferences = context.getSharedPreferences(“Info”,Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
int defaultRange = Integer.valueOf(spinner.getSelectedItem().toString());
editor.putInt(“range”,defaultRange);
if (chb_defaultName.isChecked()){
String defaultName = edit_name.getText().toString();
if (defaultName.isEmpty()){
Toast.makeText(context,“请输入默认姓名!”,Toast.LENGTH_SHORT).show();
return;
}
editor.putString(“name”,defaultName);
}
else {
editor.remove(“name”);
}
if (editor.commit()){
Toast.makeText(context,“保存成功!”,Toast.LENGTH_SHORT).show();
}
}
}
}
完成可执行run,在模拟器跑一下。
【“猜数”模块的实现】
猜数模块布局文件"fragment_guess.xml"内容如下:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".GuessFragment"
android:background="#ffffff"
android:orientation=“vertical”>
<TextView
android:id="@+id/txt_range"
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_margin=“5dp”
android:textSize=“22dp”/>
<EditText
android:id="@+id/edit_num"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_marginTop=“10dp”
android:layout_below="@id/txt_range"
android:padding=“8dp”
android:inputType=“numberSigned”
android:textSize=“22dp”/>
<Button
android:id="@+id/btn_submit"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_marginTop=“10dp”
android:layout_below="@id/edit_num"
android:textSize=“22dp”/>
<TextView
android:id="@+id/txt_compare"
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginTop=“10dp”
android:textSize=“22dp”
android:layout_below="@id/btn_submit"
android:layout_centerHorizontal=“true”/>
<ImageView
android:id="@+id/img_broad"
android:layout_width=“60dp”
android:layout_height=“60dp”
android:layout_margin=“10dp”
android:layout_toStartOf="@id/txt_compare"
android:layout_toLeftOf="@id/txt_compare"
android:layout_below="@id/btn_submit"/>
<TextView
android:id="@+id/txt_count"
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginTop=“10dp”
android:textSize=“22dp”
android:layout_below="@id/txt_compare"
android:layout_alignLeft="@id/txt_compare"
android:layout_alignStart="@id/txt_compare"
/>
<ImageView
android:id="@+id/img_bingo"
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_below="@id/img_broad"
android:layout_centerHorizontal=“true”
android:layout_marginTop=“10dp”
android:src="@drawable/bingo"/>
在GuessFragment类中实现猜数判断逻辑:
package com.example.mygame;
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
public class GuessFragment extends Fragment {
private int count,rndNum;
private EditText edit_num;
private TextView txt_range,txt_compare,txt_count;
private Button btn_submit;
private ImageView img_broad,img_bingo;
private boolean flag;
private String name;
public int range = 0;
private Context context;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_guess,container,false);
context = view.getContext();
initView(view);
initData();
btn_submit.setOnClickListener(new ButtonOnClickLisener());
return view;
}
private void initView(View view){
edit_num = view.findViewById(R.id.edit_num);
txt_range = view.findViewById(R.id.txt_range);
txt_compare = view.findViewById(R.id.txt_compare);
txt_count = view.findViewById(R.id.txt_count);
btn_submit = view.findViewById(R.id.btn_submit);
img_broad = view.findViewById(R.id.img_broad);
img_bingo = view.findViewById(R.id.img_bingo);
}
public void initData(){
SharedPreferences sharedPreferences = context.getSharedPreferences(“Info”,Context.MODE_PRIVATE);
name = sharedPreferences.getString(“name”,"");
range = sharedPreferences.getInt(“range”,100);
Random mRandom = new Random();
rndNum = mRandom.nextInt(range)+1;
txt_range.setText(“请输入1-”+range+“之间的整数”);
txt_count.setText("");
txt_compare.setText("");
img_broad.setVisibility(View.INVISIBLE);
img_bingo.setVisibility(View.INVISIBLE);
btn_submit.setText(“提交”);
flag = true;
count = 0;
}
private class ButtonOnClickLisener implements View.OnClickListener{
@Override
public void onClick(View v) {
if (flag){
String str_num = edit_num.getText().toString();
if (str_num.isEmpty()){
Toast.makeText(context,“请输入数字!”,Toast.LENGTH_SHORT).show();
return;
}
edit_num.setText("");
int numInput = Integer.valueOf(str_num);
if (numInput<1 || numInput > range){
Toast.makeText(context,“请输入范围内的数字!”,Toast.LENGTH_SHORT).show();
return;
}
count += 1;
img_broad.setVisibility(View.VISIBLE);
txt_count.setText(“猜数次数:”+count);
if (numInput > rndNum){
txt_compare.setText(“输入的”+str_num+“太大了!”);
}else if(numInput < rndNum){
txt_compare.setText(“输入的”+str_num+“太小了!”);
}else{
txt_compare.setText(“恭喜你猜对啦!正确答案是:”+rndNum+"!");
img_bingo.setVisibility(View.VISIBLE);
flag = false;
btn_submit.setText(“再猜一次”);
if (name.isEmpty()){
final CustomDialog dialog = new CustomDialog(context,R.style.MyDialog);
dialog.show();
Button btn_confirm = dialog.findViewById(R.id.btn_confirm);
final EditText edit_name = dialog.findViewById(R.id.edit_inputName);
btn_confirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
name = edit_name.getText().toString();
if (!name.isEmpty()){
insertData();
dialog.dismiss();
}
}
});
} else {
insertData();
}
}
}else {
initData();
}
}
}
private void insertData(){
MyHelper helper = new MyHelper(context,“Guess.db”,null,1);
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(“name”,name);
values.put(“count”,count);
values.put(“range”,range);
db.insert(“ordertb”,null,values);
db.close();
}
}
可以发现,在GuessFragment类中,引入了CustomDialog类,这是自定义的对话框,用于前面基本构思说的,用户在进行猜数游戏时,如果在设置页中,不选择使用默认姓名的话,游戏结束会要求用户输入用户名。
CustomDialog自定义对话框的具体实现如下:
布局文件“alertdialog_input.xml”:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:orientation=“vertical” android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:gravity=“center_horizontal”
android:background="#80BCF7"
android:padding=“20dp”>
<TextView
android:id="@+id/txt_info"
android:layout_width=“match_parent”
android:layout_height=“50dp”
android:layout_marginTop=“5dp”
android:gravity=“center”
android:text="@string/txt_dialog_info"
android:textSize=“22dp”
android:textColor="#ffffff"
/>
<EditText
android:id="@+id/edit_inputName"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:textSize=“20dp”
android:padding=“8dp”/>
<Button
android:id="@+id/btn_confirm"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text="@string/btn_dialog_confirm"
android:textSize=“20dp”/>
布局文件中涉及到的“strings.xml”文件内容如下:
请输入姓名
确定
CustomDialog类实现代码:
package com.example.mygame;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.WindowManager;
public class CustomDialog extends Dialog {
private Context context;
public CustomDialog(Context context,int themeResId) {
super(context,themeResId);
this.context = context;
}
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.setContentView(R.layout.alertdialog_input);
}
@Override
public void show(){
super.show();
// 设置对话框宽度、对齐方式等
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.gravity = Gravity.CENTER;
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
// getWindow().getDecorView().setPadding(0,0,0,0);
getWindow().setAttributes(layoutParams);
}
}
【“排行榜”模块的实现】
该模块布局文件“fragment_order.xml””内容如下:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".OrderFragment">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
<android.support.v4.view.PagerTabStrip
android:id="@+id/tabstrip"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”/>
</android.support.v4.view.ViewPager>
新建“viewpager_tab_hundred.xml”布局文件(用于在排行榜页中显示排名,也可
以多建几个同样的文件,在排行榜实现分页显示不同范围的猜数游戏有不同的排行榜,这里只在一页显示全部):
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:orientation=“vertical” android:layout_width=“match_parent”
android:layout_height=“match_parent”>
<ListView
android:id="@+id/lv_hundred"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
OrderFragment类逻辑代码实现:
package com.example.mygame;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
r
android:id="@+id/viewpager"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
<android.support.v4.view.PagerTabStrip
android:id="@+id/tabstrip"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”/>
</android.support.v4.view.ViewPager>
新建“viewpager_tab_hundred.xml”布局文件(用于在排行榜页中显示排名,也可[外链图片转存中…(img-RmC4EYXq-1643437357484)]
以多建几个同样的文件,在排行榜实现分页显示不同范围的猜数游戏有不同的排行榜,这里只在一页显示全部):
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:orientation=“vertical” android:layout_width=“match_parent”
android:layout_height=“match_parent”>
<ListView
android:id="@+id/lv_hundred"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
OrderFragment类逻辑代码实现:
package com.example.mygame;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;