- 目录
开发寄语开发功能开发环境程序流程图实现步骤将活动所需要的图片文件(.jpg或.png)统一保存在drawable文件夹主活动的布局文件activity_fore.xml主活动的java文件ForeActivity.java第二个活动的布局文件activity_main.xml第二个活动的菜单文件main.xml第二个活动的java文件MainActivity.java关于res/drawable的图片资源下载地址说明如何将项目app传到手机(1)生成APK安装包 (2)打开APK安装包保存地址(3)将安装包放入自命名的文件夹里,拉到手机,在手机文件资源处进行查看并安装APP效果图
开发寄语
本人出于情怀,恰巧与课程设计撞了个满怀,兴趣上头,开发了一款简单实用、便捷舒服的多功能篮球计分APP,以此来纪念我大学三年的篮球生涯以及队友。希望多年来我们还可以凑在一起,打打篮球、谈谈人生,或者带着我们的孩子,一起在篮球场上组个队。在此,我致敬所有热爱篮球并追逐梦想的人。
开发功能
实现一款简单、便捷、实用的多功能篮球计分器,其中包括篮球计分、比赛日程、天气预报、热门比赛、主题切换等功能。
开发环境
平台:Android studio
SDK:Android Platform 32
Virtual device:Nexus 5 API 32
程序流程图
活动跳转图(图1)
MainActivity框架图(图2)
实现步骤
将活动所需要的图片文件(.jpg或.png)统一保存在drawable文件夹
主活动的布局文件activity_fore.xml
此处采用约束布局,定义6个Guideline控件、1个ImageView控件、2个EditText控件、2个Button控件。实现代码如下:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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=".ForeActivity">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.02" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.45" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.55" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.65" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.8" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.95" />
<ImageView
android:id="@+id/iot"
android:layout_width="366dp"
android:layout_height="249dp"
android:src="@drawable/iot"
app:layout_constraintBottom_toTopOf="@+id/guideline2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline1"
app:layout_constraintVertical_bias="0.545" />
<EditText
android:id="@+id/edit_text1"
android:layout_width="264dp"
android:layout_height="47dp"
android:hint="请输入主队队名:"
android:textColor="#000000"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/edit_text2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline2"
app:layout_constraintVertical_bias="0.309" />
<EditText
android:id="@+id/edit_text2"
android:layout_width="264dp"
android:layout_height="47dp"
android:layout_marginBottom="12dp"
android:hint="请输入客队队名:"
android:textColor="#000000"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/guideline4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/start"
android:layout_width="149dp"
android:layout_height="67dp"
android:backgroundTint="@color/teal_200"
android:text="开始比赛"
android:textSize="25sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/guideline5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline4" />
<Button
android:id="@+id/schedule"
android:layout_width="149dp"
android:layout_height="67dp"
android:backgroundTint="@color/teal_200"
android:text="比赛日程"
android:textSize="25sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/guideline6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline5" />
</androidx.constraintlayout.widget.ConstraintLayout>
主活动的java文件ForeActivity.java
此处使用接口的方式注册监听器,监听“开始比赛”或“比赛日程”两个按钮是否被点击,从而执行相关程序 -> start()、schedule()。
start()使用Intent传递队名等数据信息到第二个活动MainActivity.java,同时进行页面跳转,来到篮球计分界面。
schedule()使用Intent跳转到第三个活动LastActivity,来到日程安排表,进行球赛日程及球员信息记录。实现代码如下:
package com.example.basketball_scorer;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class ForeActivity extends AppCompatActivity implements View.OnClickListener {
private Button start, schedule;
private EditText edit_text1, edit_text2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fore);
inintView();
}
private void inintView() {
//获取按钮id
start=findViewById(R.id.start);
schedule=findViewById(R.id.schedule);
//获取队名id
edit_text1=findViewById(R.id.edit_text1);
edit_text2=findViewById(R.id.edit_text2);
//按钮实现监听
start.setOnClickListener(this);
schedule.setOnClickListener(this);
edit_text1.setOnClickListener(this);
edit_text2.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()){
case R.id.start:
start();
break;
case R.id.schedule:
Toast.makeText(ForeActivity.this, "暂无最新比赛日程", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
private void start() {
//判断edit_text1、edit_text2是否填写
String inputText1 = edit_text1.getText().toString();
if(TextUtils.isEmpty(inputText1)) {
Toast.makeText(ForeActivity.this, "请输入主队队名!!!", Toast.LENGTH_SHORT).show();
return;
}
String inputText2 = edit_text2.getText().toString();
if(TextUtils.isEmpty(inputText2)) {
Toast.makeText(ForeActivity.this, "请输入客队队名!!!", Toast.LENGTH_SHORT).show();
return;
}
//使用Intent传递数据信息
Intent intent = new Intent(ForeActivity.this, MainActivity.class);
intent.putExtra("edit_text1",inputText1);
intent.putExtra("edit_text2",inputText2);
//启动
startActivity(intent);
}
//schedule函数,实现日程表功能
private void schedule() {
}
//退出应用,弹出提示框
@Override
public void onBackPressed() {
//弹出提示框,提示用户你是否要退出
AlertDialog.Builder builder=new AlertDialog.Builder(ForeActivity.this);
builder.setTitle("提示");
builder.setIcon(R.drawable.warning);
builder.setMessage("你确定退出应用吗?");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ForeActivity.this.finish();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}
第二个活动的布局文件activity_main.xml
此处采用约束布局,定义5个Guideline控件、2个ImageView控件、5个TextView控件、6个Button控件。实现代码如下:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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=".MainActivity">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.02" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="91dp" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="233dp" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.77" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/master_name"
android:textColor="#E91E63"
android:textSize="40sp"
android:textStyle="bold"
android:translationZ="1dp"
app:layout_constraintEnd_toStartOf="@+id/textView2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline1" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/guest_name"
android:textColor="#00BCD4"
android:textSize="40sp"
android:textStyle="bold"
android:translationZ="1dp"
app:layout_constraintBottom_toBottomOf="@+id/textView1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/textView1"
app:layout_constraintTop_toTopOf="@+id/textView1"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:id="@+id/score_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="0"
android:textColor="#E91E63"
android:textSize="80sp"
android:textStyle="bold"
android:translationZ="1dp"
app:layout_constraintEnd_toStartOf="@+id/score_b"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline3" />
<TextView
android:id="@+id/score_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="0"
android:textColor="#00BCD4"
android:textSize="80sp"
android:textStyle="bold"
android:translationZ="1dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/score_a"
app:layout_constraintTop_toTopOf="@+id/guideline3" />
<Button
android:id="@+id/add_a1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#ED047D"
android:text="+1"
android:textSize="25sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/add_a2"
app:layout_constraintEnd_toStartOf="@+id/add_b1"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/score_a" />
<Button
android:id="@+id/add_a2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#ED047D"
android:text="+2"
android:textSize="25sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/add_a3"
app:layout_constraintEnd_toStartOf="@+id/add_b2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/add_a1" />
<Button
android:id="@+id/add_a3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:backgroundTint="#ED047D"
android:text="+3"
android:textSize="25sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/img_cancel"
app:layout_constraintEnd_toStartOf="@+id/add_b3"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/add_a2" />
<Button
android:id="@+id/add_b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#00BCD4"
android:text="+1"
android:textSize="25sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/add_b2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/add_a1"
app:layout_constraintTop_toBottomOf="@+id/score_b" />
<Button
android:id="@+id/add_b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#00BCD4"
android:text="+2"
android:textSize="25sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/add_b3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/add_a2"
app:layout_constraintTop_toBottomOf="@+id/add_b1" />
<Button
android:id="@+id/add_b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:backgroundTint="#00BCD4"
android:text="+3"
android:textSize="25sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/img_reset"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/add_a3"
app:layout_constraintTop_toBottomOf="@+id/add_b2" />
<ImageView
android:id="@+id/img_cancel"
android:layout_width="110dp"
android:layout_height="65dp"
android:layout_marginTop="16dp"
android:src="@drawable/cancel"
android:translationZ="1dp"
app:layout_constraintEnd_toStartOf="@+id/img_reset"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline5" />
<ImageView
android:id="@+id/img_reset"
android:layout_width="110dp"
android:layout_height="65dp"
android:layout_marginTop="16dp"
android:src="@drawable/reset"
android:translationZ="1dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/img_cancel"
app:layout_constraintTop_toTopOf="@+id/guideline5" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
第二个活动的菜单文件main.xml
此处设置一、二级菜单,减少屏幕控件占比,整洁干净。实现代码如下:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/item_1"
android:title="热门比赛"/>
<item android:id="@+id/item_2"
android:title="天气预报"/>
<item android:id="@+id/item_3"
android:title="主题壁纸">
<menu>
<item
android:id="@+id/original"
android:title="默认主题"/>
<item
android:id="@+id/warriors"
android:title="勇士主场"/>
<item
android:id="@+id/lakers"
android:title="湖人主场"/>
<item
android:id="@+id/hawks"
android:title="老鹰主场"/>
<item
android:id="@+id/dallas"
android:title="小牛主场"/>
<item
android:id="@+id/nets"
android:title="篮网主场"/>
<item
android:id="@+id/sixers"
android:title="76人主场"/>
<item
android:id="@+id/nuggets"
android:title="掘金主场"/>
<item
android:id="@+id/cavaliers"
android:title="骑士主场"/>
<item
android:id="@+id/celtics"
android:title="绿军主场"/>
<item
android:id="@+id/hornets"
android:title="黄蜂主场"/>
</menu>
</item>
<item android:id="@+id/item_4"
android:title="更多服务"/>
</menu>
第二个活动的java文件MainActivity.java
此处使用接口的方式注册监听器,监听各类型的控件,实现分数加1、分数加2、分数加3、分数撤回、分数置0等功能;调用onOptionsItemSelected(MenuItem item) 方法,处理一、二级菜单的点击事件;调用Intent方法,跳转到热门比赛、天气预报等网站。
实现代码如下:
package com.example.basketball_scorer;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//定义数组存放加分
private final int scoreArray[]={1,2,3};
private int lastScore_a,lastScore_b,score_a,score_b;
// lastScore:上一次加的分数 score:总分数
private Button add_a1,add_a2,add_a3,add_b1,add_b2,add_b3;
private ImageView img_cancel,img_reset;
private TextView tv,master_name,guest_name,test_score_a,test_score_b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
String inputText1 = intent.getStringExtra("edit_text1");
String inputText2 = intent.getStringExtra("edit_text2");
inintView();
master_name.setText(inputText1);
guest_name.setText(inputText2);
}
private void inintView() {
//获取按钮id
add_a1=findViewById(R.id.add_a1);
add_a2=findViewById(R.id.add_a2);
add_a3=findViewById(R.id.add_a3);
add_b1=findViewById(R.id.add_b1);
add_b2=findViewById(R.id.add_b2);
add_b3=findViewById(R.id.add_b3);
//得分情况和清零
img_cancel=findViewById(R.id.img_cancel);
img_reset=findViewById(R.id.img_reset);
tv=findViewById(R.id.textView);
master_name=findViewById(R.id.textView1);
guest_name=findViewById(R.id.textView2);
test_score_a=findViewById(R.id.score_a);
test_score_b=findViewById(R.id.score_b);
//按钮实现监听
add_a1.setOnClickListener(this);
add_a2.setOnClickListener(this);
add_a3.setOnClickListener(this);
add_b1.setOnClickListener(this);
add_b2.setOnClickListener(this);
add_b3.setOnClickListener(this);
img_reset.setOnClickListener(this);
img_cancel.setOnClickListener(this);
}
//菜单栏
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.main,menu);
return true;
}
//处理菜单点击事件
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.item_1:
Intent intent1 = new Intent(Intent.ACTION_VIEW);
intent1.setData(Uri.parse("https://xw.qq.com/m/nba/vs"));
startActivity(intent1);
Toast.makeText(MainActivity.this, "1", Toast.LENGTH_SHORT).show();
break;
case R.id.item_2:
Intent intent2 = new Intent(Intent.ACTION_VIEW);
intent2.setData(Uri.parse("https://m.tianqi.com/"));
startActivity(intent2);
Toast.makeText(MainActivity.this, "2", Toast.LENGTH_SHORT).show();
break;
case R.id.item_3:
Toast.makeText(MainActivity.this, "请选择你喜爱的主题", Toast.LENGTH_SHORT).show();
break;
case R.id.item_4:
Toast.makeText(MainActivity.this, "更多服务请等待进一步完善", Toast.LENGTH_SHORT).show();
break;
case R.id.original:
tv.setBackgroundResource(R.drawable.original);
Toast.makeText(MainActivity.this, "主题已更改", Toast.LENGTH_SHORT).show();
break;
case R.id.warriors:
tv.setBackgroundResource(R.drawable.warriors);
Toast.makeText(MainActivity.this, "主题已更改", Toast.LENGTH_SHORT).show();
break;
case R.id.lakers:
tv.setBackgroundResource(R.drawable.lakers);
Toast.makeText(MainActivity.this, "主题已更改", Toast.LENGTH_SHORT).show();
break;
case R.id.hawks:
tv.setBackgroundResource(R.drawable.hawks);
Toast.makeText(MainActivity.this, "主题已更改", Toast.LENGTH_SHORT).show();
break;
case R.id.dallas:
tv.setBackgroundResource(R.drawable.dallas);
Toast.makeText(MainActivity.this, "主题已更改", Toast.LENGTH_SHORT).show();
break;
case R.id.nets:
tv.setBackgroundResource(R.drawable.nets);
Toast.makeText(MainActivity.this, "主题已更改", Toast.LENGTH_SHORT).show();
break;
case R.id.sixers:
tv.setBackgroundResource(R.drawable.sixers);
Toast.makeText(MainActivity.this, "主题已更改", Toast.LENGTH_SHORT).show();
break;
case R.id.nuggets:
tv.setBackgroundResource(R.drawable.nuggets);
Toast.makeText(MainActivity.this, "主题已更改", Toast.LENGTH_SHORT).show();
break;
case R.id.cavaliers:
tv.setBackgroundResource(R.drawable.cavaliers);
Toast.makeText(MainActivity.this, "主题已更改", Toast.LENGTH_SHORT).show();
break;
case R.id.celtics:
tv.setBackgroundResource(R.drawable.celtics);
Toast.makeText(MainActivity.this, "主题已更改", Toast.LENGTH_SHORT).show();
break;
case R.id.hornets:
tv.setBackgroundResource(R.drawable.hornets);
Toast.makeText(MainActivity.this, "主题已更改", Toast.LENGTH_SHORT).show();
break;
default:
}
return super.onOptionsItemSelected(item);
}
//点击事件
public void onClick(View v) {
switch (v.getId()){
case R.id.add_a1:
scoreAdd(0,scoreArray[0]);
break;
case R.id.add_a2:
scoreAdd(0,scoreArray[1]);
break;
case R.id.add_a3:
scoreAdd(0,scoreArray[2]);
break;
case R.id.add_b1:
scoreAdd(1,scoreArray[0]);
break;
case R.id.add_b2:
scoreAdd(1,scoreArray[1]);
break;
case R.id.add_b3:
scoreAdd(1,scoreArray[2]);
break;
case R.id.img_reset:
reset();
break;
case R.id.img_cancel:
cancel();
break;
default:
break;
}
}
//取消上次加分操作
private void cancel() {
if (score_a != 0 && score_a-lastScore_a >= 0){
score_a -= lastScore_a;
}
if (score_b != 0 && score_b-lastScore_b >= 0){
score_b -= lastScore_b;
}
Toast.makeText(MainActivity.this, "已取消上次加分操作", Toast.LENGTH_SHORT).show();
ShowText();
}
//重置功能,弹出提示框
private void reset() {
//弹出提示框,提示用户你是否要重置
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("提示");
builder.setIcon(R.drawable.warning);
builder.setMessage("你确定要重置分数吗?");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
score_a=0;
score_b=0;
ShowText();
dialog.dismiss();
Toast.makeText(MainActivity.this, "分数已重置", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
//返回上一层,弹出提示框
@Override
public void onBackPressed() {
//弹出提示框,提示用户你是否要退出
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("提示");
builder.setIcon(R.drawable.warning);
builder.setMessage("你确定要返回上一层?");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
MainActivity.this.finish();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
//进行加分
private void scoreAdd(int flag, int score){
//Tage: 0:a 1:b
if (flag == 0 || flag == 1){
if (flag == 0){
//表示a进行加分
lastScore_b = 0;
lastScore_a = score;
score_a += lastScore_a;
}else if (flag == 1){
//表示对b进行加分
lastScore_a = 0;
lastScore_b = score;
score_b += lastScore_b;
}
ShowText();
}
}
//进行显示
private void ShowText(){
test_score_a.setText(Integer.toString(score_a));
test_score_b.setText(Integer.toString(score_b));
}
}
关于res/drawable的图片资源下载地址说明
App 图标:阿里巴巴矢量图标
主题图片:百度搜索nba球队地板图
图片设计:canvas官网
如何将项目app传到手机
(1)生成APK安装包
(2)打开APK安装包保存地址
(3)将安装包放入自命名的文件夹里,拉到手机,在手机文件资源处进行查看并安装
APP效果图