前言
該篇筆記是我學習 L2-2c 課程后的筆記挣郭,此次課程學習完成實現(xiàn)了一個類似籃球計分 APP 項目的開發(fā)蔫慧,現(xiàn)在我們一起來做吧蜂挪!
1.變量的意義
在開發(fā)之前滓走,我們先來了解一下什么是變量與作用域垦江?
變量:用于表達在程序中可能被改變的值,分為全局變量和局部變量搅方。
全局變量也叫成員變量比吭,是指在類中定義的變量;它在整個類中都有效姨涡,比如下面示例中 name衩藤、age 兩個變量就是全局變量。
局部變量是在方法內(nèi)部聲明涛漂,并且只能在方法內(nèi)部使用赏表,比如小面的 height、weight 就是局部變量匈仗。
public class Student {
//name瓢剿,age 為全局變量
String name;
int age;
public static void main(String[] age) {
//height,weight 為局部變量
int height;
int weight;
}
}
2.APP UI 布局的開發(fā)
一言不合先上圖
basketball.png
在以前的課程中我們知道悠轩,創(chuàng)建一個布局需要 3 步:
第一步:選擇 View
從上圖可得到 UI 界面中有 TextView间狂、Button、View 3 種類型控件哗蜈,所以我們就選擇這 3 種控件類型前标。
第二步:擺放 View:
這次我選擇的是線性布局(LinearLayout)做為 ViewGroup ,你也可以選擇其他布局距潘,下面是代碼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="湖人"
android:textSize="20sp"/>
<TextView
android:id="@+id/team_a_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="0"
android:textColor="#000000"
android:textSize="64sp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="24dp"
android:onClick="addThreeForTeamA"
android:text="3分遠射"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="8dp"
android:onClick="addTwoForTeamA"
android:text="2分中投"/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="8dp"
android:onClick="addOneForTeamA"
android:text="1分罰球"/>
</LinearLayout>
<View
android:layout_width="1px"
android:layout_height="match_parent"
android:background="#000"
android:layout_marginTop="16dp"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="凱爾特人"
android:textSize="20sp"/>
<TextView
android:id="@+id/team_b_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="0"
android:textColor="#000000"
android:textSize="64sp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="24dp"
android:onClick="addThreeForTeamB"
android:text="3分遠射"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="8dp"
android:onClick="addTwoForTeamB"
android:text="2分中投"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="8dp"
android:onClick="addOneForTeamB"
android:text="1分罰球"/>
</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:layout_marginBottom="32dp"
android:layout_marginTop="100dp"
android:text="清零"
android:onClick="reset"/>
</LinearLayout>
第三步:修改 View 的樣式
按照課程所提示的樣式如下所示:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="colorPrimary">#FF9800</item>
<!-- Background color of buttons in the app -->
<item name="colorButtonNormal">#FF9800</item>
</style>
</resources>
3. MainActivity 中功能邏輯實現(xiàn)
由于在開始我們就學習了變量的知識炼列,到了功能邏輯這里就簡單多了,代碼如下:
package com.vole.basketball;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView tvTeamAScore, tvTeamBScore;
private int aScore, bScore;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
tvTeamAScore = (TextView) findViewById(R.id.team_a_score);
tvTeamBScore = (TextView) findViewById(R.id.team_b_score);
}
public void addThreeForTeamA(View view) {
aScore += 3;
aDisplayScore(aScore);
}
public void addTwoForTeamA(View view) {
aScore += 2;
aDisplayScore(aScore);
}
public void addOneForTeamA(View view) {
aScore += 1;
aDisplayScore(aScore);
}
public void addThreeForTeamB(View view) {
bScore += 3;
bDisplayScore(bScore);
}
public void addTwoForTeamB(View view) {
bScore += 2;
bDisplayScore(bScore);
}
public void addOneForTeamB(View view) {
bScore += 1;
bDisplayScore(bScore);
}
public void reset(View view) {
aScore = 0;
bScore = 0;
aDisplayScore(aScore);
bDisplayScore(bScore);
}
private void aDisplayScore(int aScore) {
tvTeamAScore.setText(String.valueOf(aScore));
}
private void bDisplayScore(int bScore) {
tvTeamBScore.setText(String.valueOf(bScore));
}
}
到此為止 籃球記分 App 應(yīng)用開發(fā)完成音比,效果如下:
android.gif
最后在這里推薦另一種實現(xiàn)方法的文章,很強大:
「自定義控件」籃球記分器 App 筆記分享