【Android】综合练习：篮球比赛计分器
Demo 根据如图设计界面 涉及点： View Model Live Data Data Binding Sc...

21
2020/03

# 【Android】综合练习：篮球比赛计分器

## Demo

• 涉及点：
View Model
Live Data
Data Binding
Screen Orientation
Localization VectorDrawable

### 代码

#### MyViewModel.java

package cn.dxoca.basketballscoring;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import java.util.Stack;

public class MyViewModel extends ViewModel {
private MutableLiveData<Integer> score_a, score_b;
Stack<Integer> stackAB[] = new Stack[2];

public MutableLiveData<Integer> getScore_a() {
if (score_a == null) {
score_a = new MutableLiveData<>();
stackAB[0] = new Stack<>();
score_a.setValue(0);
}
return score_a;
}

public MutableLiveData<Integer> getScore_b() {
if (score_b == null) {
score_b = new MutableLiveData<>();
stackAB[1] = new Stack<>();
score_b.setValue(0);
}
return score_b;
}

/**
* 清空数据 初始化
*/
public void init() {
recordStackAB();
score_a.setValue(0);
score_b.setValue(0);
}

public void add_a(int n) {
recordStackAB();
score_a.setValue(score_a.getValue() + n);
}

public void add_b(int n) {
recordStackAB();
score_b.setValue(score_b.getValue() + n);
}
/**
* 恢复上一步的数据
* 用旧的分数覆盖新的分数
*/
public void recover() {
if (!stackAB[0].isEmpty()) {//因为ab同入同出 只要判断一个就好了
score_a.setValue(stackAB[0].pop());
score_b.setValue(stackAB[1].pop());
}
}

public void recordStackAB() {
stackAB[0].push(score_a.getValue());
stackAB[1].push(score_b.getValue());
}
}

#### MainActivity.java

package cn.dxoca.basketballscoring;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
MyViewModel myViewModel;//管理数据
ActivityMainBinding binding;//管理对象

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);//创建这个对象的绑定 xml的标签都会成为这个类的成员
myViewModel = new ViewModelProvider(this).get(MyViewModel.class);
binding.setData(myViewModel);//设置数据
binding.setLifecycleOwner(this);//设置监听
}
}

#### activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<variable
name="data"
</data>

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ImageButton
android:id="@+id/imageButton_Initialize"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginStart="52dp"
android:background="@android:color/darker_gray"
android:contentDescription="@string/imageDescription"
android:onClick="@{()->data.init()}"
app:layout_constraintBottom_toTopOf="@+id/guideline8"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintTop_toTopOf="@+id/guideline7"
app:srcCompat="@drawable/ic_autorenew_black_24dp" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.82" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.91" />

<Button
android:id="@+id/button_A2"
android:layout_width="133dp"
android:layout_height="49dp"
android:background="@android:color/holo_red_light"
android:textColor="@android:color/background_light"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/guideline6"
app:layout_constraintEnd_toStartOf="@+id/guideline"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline5" />

<Button
android:id="@+id/button_B2"
android:layout_width="133dp"
android:layout_height="49dp"
android:background="#009952"
android:textColor="@android:color/background_light"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/guideline6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintTop_toTopOf="@+id/guideline5" />

<Button
android:id="@+id/button_A3"
android:layout_width="133dp"
android:layout_height="49dp"
android:background="@android:color/holo_red_light"
android:textColor="@android:color/background_light"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/guideline7"
app:layout_constraintEnd_toStartOf="@+id/guideline"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline6" />

<Button
android:id="@+id/button_B3"
android:layout_width="133dp"
android:layout_height="49dp"
android:background="#009952"
android:textColor="@android:color/background_light"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/guideline7"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintTop_toTopOf="@+id/guideline6" />

<Button
android:id="@+id/button_B1"
android:layout_width="133dp"
android:layout_height="49dp"
android:background="#009952"
android:textColor="@android:color/background_light"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/guideline5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintTop_toTopOf="@+id/guideline4" />

<TextView
android:id="@+id/textView_B"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/textView_B"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="@+id/guideline3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintTop_toTopOf="@+id/guideline9" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.50121653" />

<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.16" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.09" />

<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.37" />

<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.52" />

<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.67" />

<TextView
android:id="@+id/textView_A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/textView_A"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="@+id/guideline3"
app:layout_constraintEnd_toStartOf="@+id/guideline"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline9" />

<TextView
android:id="@+id/textView_AScore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{String.valueOf(data.score_a)}"
android:textColor="@android:color/holo_red_light"
android:textSize="60sp"
app:layout_constraintBottom_toTopOf="@+id/guideline4"
app:layout_constraintEnd_toStartOf="@+id/guideline"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline3"
tools:text="50" />

<TextView
android:id="@+id/textView_BScore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{String.valueOf(data.score_b)}"
android:textColor="#0D7A18"
android:textSize="60sp"
app:layout_constraintBottom_toTopOf="@+id/guideline4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintTop_toTopOf="@+id/guideline3"
tools:text="120" />

<Button
android:id="@+id/button_A1"
android:layout_width="133dp"
android:layout_height="49dp"
android:background="@android:color/holo_red_light"
android:textColor="@android:color/background_light"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/guideline5"
app:layout_constraintEnd_toStartOf="@+id/guideline"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline4" />

<ImageButton
android:id="@+id/imageButton_Recover"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginEnd="52dp"
android:background="@android:color/darker_gray"
android:contentDescription="@string/imageDescription"
android:onClick="@{()->data.recover()}"
app:layout_constraintBottom_toTopOf="@+id/guideline8"
app:layout_constraintEnd_toStartOf="@+id/guideline"
app:layout_constraintTop_toTopOf="@+id/guideline7"
app:layout_constraintVertical_bias="0.451"
app:srcCompat="@drawable/ic_replay_black_24dp" />

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/author"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

百度已收录

Last modification：March 21st, 2020 at 08:04 pm