1.比赛计分器界面设计
源代码
<?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=".main.MainActivity" android:orientation="vertical" android:gravity="center_horizontal"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center_horizontal" android:paddingTop="50dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Left:"/> <TextView android:layout_width="50dp" android:layout_height="wrap_content" android:id="@+id/leftScore" android:text="0" android:gravity="center"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=":" /> <TextView android:layout_width="50dp" android:layout_height="wrap_content" android:id="@+id/rightScore" android:text="0" android:gravity="center"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Right:"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingTop="50dp"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+" android:id="@+id/leftBtn"/> <TextView android:layout_width="50dp" android:layout_height="wrap_content" android:id="@+id/left" android:text="0" android:gravity="center"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=":" /> <TextView android:layout_width="50dp" android:layout_height="wrap_content" android:id="@+id/right" android:text="0" android:gravity="center"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+" android:id="@+id/rightBtn"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="bottom" android:layout_weight="1"> <Button android:layout_width="50dp" android:layout_height="wrap_content" android:text="1" android:id="@+id/one"/> <Button android:layout_width="50dp" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="2" android:id="@+id/two"/> <Button android:layout_width="50dp" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="3" android:id="@+id/three"/> <Button android:layout_width="50dp" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="4" android:id="@+id/four"/> <Button android:layout_width="50dp" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="5" android:id="@+id/five"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:id="@+id/clear" android:text="clear"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="查询比分:" android:layout_marginLeft="10dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/showView" android:text="none" /> </LinearLayout> </LinearLayout>
2.比赛现场计分
源码
package com.example.gengen.domain; public class Game { private int round; private String result; public int getRound() { return round; } public void setRound(int round) { this.round = round; } public String getResult() { return result; } public void setResult(String result) { this.result = result; } @Override public String toString() { return "Game{" + "round=" + round + ", result='" + result + '\'' + '}'; } }
package com.example.gengen.main; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import com.example.gengen.R; import com.example.gengen.db.DbDao; import com.example.gengen.domain.Game; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private DbDao dbDao; private TextView left, leftScore, right, rightScore, showView; private Button leftBtn, rightBtn, one, two, three, four, five, clear; private int innings, numLeft, numRight, round, scoreL, scoreR; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); initView(); initValues(); } // 链接数据库 private void init() { dbDao = new DbDao(getApplicationContext()); } // 初始化变量 private void initValues() { numLeft=0; numRight=0; scoreL=0; scoreR=0; round=1; } // 初始化控件 private void initView() { left = findViewById(R.id.left); leftScore = findViewById(R.id.leftScore); right = findViewById(R.id.right); rightScore = findViewById(R.id.rightScore); showView = findViewById(R.id.showView); leftBtn = findViewById(R.id.leftBtn); rightBtn = findViewById(R.id.rightBtn); one = findViewById(R.id.one); two = findViewById(R.id.two); three = findViewById(R.id.three); four = findViewById(R.id.four); five = findViewById(R.id.five); clear = findViewById(R.id.clear); // 绑定单击事件 leftBtn.setOnClickListener(this); rightBtn.setOnClickListener(this); one.setOnClickListener(this); two.setOnClickListener(this); three.setOnClickListener(this); four.setOnClickListener(this); five.setOnClickListener(this); clear.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.leftBtn: leftAdd(); break; case R.id.rightBtn: rightAdd(); break; case R.id.one: innings = 1; showView.setText(getInnings(innings)); break; case R.id.two: innings = 2; showView.setText(getInnings(innings)); break; case R.id.three: innings = 3; showView.setText(getInnings(innings)); break; case R.id.four: innings = 4; showView.setText(getInnings(innings)); break; case R.id.five: innings = 5; showView.setText(getInnings(innings)); break; case R.id.clear: clearView(); break; } } private void leftAdd() { numLeft++; left.setText(""+numLeft); isWin(numLeft, numRight); getWinner(); } private void rightAdd() { numRight++; right.setText(""+numRight); isWin(numLeft, numRight); getWinner(); } private void clearView() { left.setText("0"); right.setText("0"); leftScore.setText("0"); rightScore.setText("0"); showView.setText("none"); numLeft=0; numRight=0; dbDao.delete(); } private void clearScore() { left.setText("0"); right.setText("0"); numLeft=0; numRight=0; } // 获得具体对局 private String getInnings(int innings) { String result = dbDao.findRound(innings); return result; } // 判断对局是否胜利 public String isWin(int scoreLeft, int scoreRight) { int difference = scoreLeft - scoreRight; if(round <= 4) { if(scoreLeft >= 25 & difference >= 2) { scoreL++; leftScore.setText(""+scoreL); Log.d("test", "isWin: "); dbDao.insert(round, scoreLeft +":"+ scoreRight); clearScore(); round++; }else if(scoreRight >= 25 & difference <= -2) { scoreR++; rightScore.setText(""+scoreR); dbDao.insert(round, scoreLeft +":"+ scoreRight); clearScore(); round++; } }else { if(scoreLeft >= 15 & difference >= 2) { scoreL++; leftScore.setText(""+scoreL); dbDao.insert(round, scoreLeft +":"+ scoreRight); clearScore(); round=1; }else if(scoreRight >= 15 & difference <= -2) { scoreR++; rightScore.setText(""+scoreR); dbDao.insert(round, scoreLeft +":"+ scoreRight); clearScore(); round=1; } } return scoreLeft +":"+ scoreRight; } // 判断比赛输赢 private void getWinner() { int l = Integer.parseInt(leftScore.getText().toString()); int r = Integer.parseInt(rightScore.getText().toString()); int difference = l-r; if(difference == 2 | l == 3) { Toast.makeText(this, "left win", Toast.LENGTH_LONG).show(); }else if(difference == -2 | r == 3) { Toast.makeText(this, "right win", Toast.LENGTH_LONG).show(); } } }
3.比赛计分存入数据库
源码
package com.example.gengen.db; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import com.example.gengen.domain.Game; import java.util.ArrayList; import java.util.List; public class DbDao { private DbHelper dbHelper; private SQLiteDatabase db; private List<Game> list = new ArrayList<>(); public DbDao(Context context) { dbHelper = new DbHelper(context); } public String findRound(int round) { String result = null; db = dbHelper.getReadableDatabase(); String sql = "select result from ball where round = '"+round+"'"; Cursor c = db.rawQuery(sql, null); while (c.moveToNext()) { result = c.getString(c.getColumnIndex("result")); } c.close(); db.close(); return result; } public void insert(int round, String result) { db = dbHelper.getReadableDatabase(); ContentValues values = new ContentValues(); values.put("round", round); values.put("result", result); db.insert("ball", null, values); db.close(); } public void delete() { db = dbHelper.getReadableDatabase(); String sql = "delete from ball"; db.execSQL(sql); db.close(); } }
package com.example.gengen.db; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import androidx.annotation.Nullable; public class DbHelper extends SQLiteOpenHelper { public DbHelper(Context context) { super(context, "dbTest.db", null, 1); } @Override public void onCreate(SQLiteDatabase db) { String createSql = "create table ball(" + "round int primary key, " + "result varchar(50))"; db.execSQL(createSql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { String sql = "drop table if exists ball"; db.execSQL(sql); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律