2C. 籃球記分 App 應(yīng)用

前言

該篇筆記是我學習 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 筆記分享

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末俭尖,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌稽犁,老刑警劉巖焰望,帶你破解...
    沈念sama閱讀 219,039評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異已亥,居然都是意外死亡熊赖,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評論 3 395
  • 文/潘曉璐 我一進店門虑椎,熙熙樓的掌柜王于貴愁眉苦臉地迎上來震鹉,“玉大人,你說我怎么就攤上這事捆姜〈海” “怎么了?”我有些...
    開封第一講書人閱讀 165,417評論 0 356
  • 文/不壞的土叔 我叫張陵泥技,是天一觀的道長浆兰。 經(jīng)常有香客問我,道長珊豹,這世上最難降的妖魔是什么簸呈? 我笑而不...
    開封第一講書人閱讀 58,868評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮平夜,結(jié)果婚禮上蝶棋,老公的妹妹穿的比我還像新娘。我一直安慰自己忽妒,他們只是感情好玩裙,可當我...
    茶點故事閱讀 67,892評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著段直,像睡著了一般吃溅。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上鸯檬,一...
    開封第一講書人閱讀 51,692評論 1 305
  • 那天决侈,我揣著相機與錄音,去河邊找鬼喧务。 笑死赖歌,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的功茴。 我是一名探鬼主播庐冯,決...
    沈念sama閱讀 40,416評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼坎穿!你這毒婦竟也來了展父?” 一聲冷哼從身側(cè)響起返劲,我...
    開封第一講書人閱讀 39,326評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎栖茉,沒想到半個月后篮绿,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,782評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡吕漂,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,957評論 3 337
  • 正文 我和宋清朗相戀三年亲配,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片痰娱。...
    茶點故事閱讀 40,102評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡弃榨,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出梨睁,到底是詐尸還是另有隱情,我是刑警寧澤娜饵,帶...
    沈念sama閱讀 35,790評論 5 346
  • 正文 年R本政府宣布坡贺,位于F島的核電站,受9級特大地震影響箱舞,放射性物質(zhì)發(fā)生泄漏遍坟。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,442評論 3 331
  • 文/蒙蒙 一晴股、第九天 我趴在偏房一處隱蔽的房頂上張望愿伴。 院中可真熱鬧,春花似錦电湘、人聲如沸隔节。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽怎诫。三九已至,卻和暖如春贷痪,著一層夾襖步出監(jiān)牢的瞬間幻妓,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評論 1 272
  • 我被黑心中介騙來泰國打工劫拢, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留肉津,地道東北人。 一個月前我還...
    沈念sama閱讀 48,332評論 3 373
  • 正文 我出身青樓舱沧,卻偏偏與公主長得像妹沙,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子狗唉,可洞房花燭夜當晚...
    茶點故事閱讀 45,044評論 2 355

推薦閱讀更多精彩內(nèi)容