2017Google Study Jams之L2籃球計分APP實踐

L2課程計分APP實踐筆記

此次活動的舉辦方:Google Study Jams活動官網(wǎng)

我的博客(同步此次活動筆記):CSDN博客我的簡書

Google Developers

L2的實踐課程是實現(xiàn)一個類似球場積分器的APP炼吴。完成這么一個APP,我大致分為以下幾步:

1.需求:計分APP的實現(xiàn)

實現(xiàn)的效果如下圖所示樱蛤,需求是钮呀,有兩個籃球隊,A隊和B對昨凡,在比賽的過程需求為得分的球隊加分爽醋。有3種進(jìn)球加分項,遠(yuǎn)投加3份便脊,近投加2分蚂四,罰球加1分。需要展示各自球隊的總分哪痰,在比賽結(jié)束時可以重置兩隊的分?jǐn)?shù)即分?jǐn)?shù)為0遂赠。

實現(xiàn)的效果圖

2.分析:由哪些控件組成及準(zhǔn)備工作

準(zhǔn)備工作:Android Studio工具,已經(jīng)運行應(yīng)用程序的手機(jī)或者模擬器晌杰。

分析:(來張手?jǐn)]分析圖跷睦,字比較丑多多擔(dān)待哈~)

用到的控件

3.創(chuàng)建項目:開干

好了,有了以上的布局分析和準(zhǔn)備工作乎莉,現(xiàn)在我們就可以動手開干了送讲。

    • 首先我們需要創(chuàng)建個新的工作空間奸笤,如圖所示


      創(chuàng)建項目
    • 填寫項目名稱惋啃、包名,選擇項目在本地磁盤的位置(包名通常寫為com.xxxx监右,及公司域名倒著寫)
填寫項目名边灭,包名
    • 選擇sdk的兼容版本,這個一般默認(rèn)即可健盒,目前市場上4.0.3以上的手機(jī)占97.4%以上绒瘦,所以我們最小兼容到4.0.3的版本即可,點擊Next
設(shè)置SDK版本
    • 這一步的話扣癣,Studio給我們提供了好多種模板惰帽,這里我們只需要選擇EmptyActivity,也就是空白的頁面即可父虑,點擊Next
選擇頁面模板
    • 為我們的主頁面命名该酗,一般默認(rèn)為MainActivity,點擊Next
為主頁面命名
    • 這樣項目就創(chuàng)建好啦士嚎,這時候可以運行一下看看效果了
項目創(chuàng)建效果
創(chuàng)建完項目效果圖

4.設(shè)計:按照效果圖和分析編寫XML文件布局

編寫XML大概的預(yù)覽布局就是這樣:

Xml布局預(yù)覽

附上Xml布局的代碼:

<?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:layout_weight="1"
                  android:orientation="horizontal"
                  android:paddingTop="20dp">
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:orientation="vertical">
            <TextView android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_marginBottom="10dp"
                      android:text="A隊得分:"
                      android:textColor="#000000"
                      android:textSize="16sp"/>
            <TextView
                android:id="@+id/aScoreText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="0"
                android:textColor="#FF0000"
                android:textSize="30sp"/>
            <Button
                android:id="@+id/aAddThreeBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="3分遠(yuǎn)投"/>
            <Button
                android:id="@+id/aAddTwoBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2分近投"/>
            <Button
                android:id="@+id/aAddOneBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="1分罰球"/>
        </LinearLayout>
        <View android:layout_width="1px"
              android:layout_height="match_parent"
              android:background="#D5D5D5"/>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:orientation="vertical">
            <TextView android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_marginBottom="10dp"
                      android:text="B隊得分:"
                      android:textColor="#000000"
                      android:textSize="16sp"/>
            <TextView
                android:id="@+id/bScoreText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="0"
                android:textColor="#0000FF"
                android:textSize="30sp"/>
            <Button
                android:id="@+id/bAddThreeBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="3分遠(yuǎn)投"/>
            <Button
                android:id="@+id/bAddTwoBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2分近投"/>
            <Button
                android:id="@+id/bAddOneBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="1分罰球"/>
        </LinearLayout>
    </LinearLayout>
    <Button
        android:id="@+id/resetBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:layout_marginBottom="20dp"
        android:text="重置"/>
</LinearLayout>

4.實現(xiàn):編寫Java代碼實現(xiàn)邏輯

附上Java代碼:

package com.shawpoo.app;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView mAScoreText, mBScoreText; //顯示兩隊分?jǐn)?shù)
    private Button mAAddThreeBtn, mAAddTwoBtn, mAAddOneBtn; //A對加分按鈕
    private Button mBAddThreeBtn, mBAddTwoBtn, mBAddOneBtn; //B對加分按鈕
    private Button mResetBtn;
    private int mAScore, mBScore; //記錄兩隊的分?jǐn)?shù)

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    /**
     * 初始化組件
     */
    private void initView() {
        mAScoreText = (TextView) findViewById(R.id.aScoreText);
        mBScoreText = (TextView) findViewById(R.id.bScoreText);
        mAAddThreeBtn = (Button) findViewById(R.id.aAddThreeBtn);
        mAAddTwoBtn = (Button) findViewById(R.id.aAddTwoBtn);
        mAAddOneBtn = (Button) findViewById(R.id.aAddOneBtn);
        mBAddThreeBtn = (Button) findViewById(R.id.bAddThreeBtn);
        mBAddTwoBtn = (Button) findViewById(R.id.bAddTwoBtn);
        mBAddOneBtn = (Button) findViewById(R.id.bAddOneBtn);
        mResetBtn = (Button) findViewById(R.id.resetBtn);

        mAAddThreeBtn.setOnClickListener(this);
        mAAddTwoBtn.setOnClickListener(this);
        mAAddOneBtn.setOnClickListener(this);
        mBAddThreeBtn.setOnClickListener(this);
        mBAddTwoBtn.setOnClickListener(this);
        mBAddOneBtn.setOnClickListener(this);
        mResetBtn.setOnClickListener(this);
    }

    /**
     * 實現(xiàn)按鈕點擊回調(diào)
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.aAddThreeBtn:
                aAddScore(3);
                break;
            case R.id.aAddTwoBtn:
                aAddScore(2);
                break;
            case R.id.aAddOneBtn:
                aAddScore(1);
                break;
            case R.id.bAddThreeBtn:
                bAddScore(3);
                break;
            case R.id.bAddTwoBtn:
                bAddScore(2);
                break;
            case R.id.bAddOneBtn:
                bAddScore(1);
                break;
            case R.id.resetBtn:
                showResetDialog();
                break;
        }
    }

    /**
     * A隊加分
     *
     * @param score 要加的分?jǐn)?shù)
     */
    private void aAddScore(int score) {
        mAScore = mAScore + score;
        displayAScore(mAScore);
    }

    /**
     * A隊加分
     *
     * @param score 要加的分?jǐn)?shù)
     */
    private void bAddScore(int score) {
        mBScore = mBScore + score;
        displayBScore(mBScore);
    }

    /**
     * 顯示清空得分的彈框
     */
    private void showResetDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle("提示")
                .setMessage("確認(rèn)要清空兩隊的得分嗎呜魄?")
                .setPositiveButton("確定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        resetScore();  //確定清空
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        // ignore
                    }
                });
        builder.show();
    }

    /**
     * 重置兩隊分?jǐn)?shù)
     */
    private void resetScore() {
        mAScore = 0;
        mBScore = 0;
        displayAScore(mAScore);
        displayBScore(mBScore);
    }

    /**
     * 顯示A隊得分
     *
     * @param score 分?jǐn)?shù)
     */
    private void displayAScore(int score) {
        mAScoreText.setText(String.valueOf(score));
    }

    /**
     * 顯示B隊得分
     *
     * @param score 分?jǐn)?shù)
     */
    private void displayBScore(int score) {
        mBScoreText.setText(String.valueOf(score));
    }
}

5.運行:展示效果

好了,代碼全部編寫完成的話莱衩,我們就可以運行查看效果了爵嗅。

ps:這里是為了分享筆記提前就把代碼寫好的,在實際開發(fā)過程中建議寫一段代碼就運行一次看看效果笨蚁,避免寫很多代碼在運行時出現(xiàn)問題睹晒,而且不易發(fā)現(xiàn)問題的原因趟庄。


效果圖1
效果圖2

6.總結(jié):通過這個APP學(xué)到了什么

  • 布局的編寫已經(jīng)簡單控件的使用
  • Java中變量的使用
  • 在編程不要把所有的代碼寫到一個方法里
  • 彈框的顯示考慮用戶體驗(當(dāng)然這是產(chǎn)品經(jīng)理的活)
  • 學(xué)會了一個APP從零到有的完整實現(xiàn)

好啦,一個籃球計分APP到這里就結(jié)束了伪很,歡迎各位學(xué)友點評指導(dǎo)岔激。

點擊下載籃球計分APP源碼

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市是掰,隨后出現(xiàn)的幾起案子虑鼎,更是在濱河造成了極大的恐慌,老刑警劉巖键痛,帶你破解...
    沈念sama閱讀 218,451評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件炫彩,死亡現(xiàn)場離奇詭異,居然都是意外死亡絮短,警方通過查閱死者的電腦和手機(jī)江兢,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來丁频,“玉大人杉允,你說我怎么就攤上這事∠铮” “怎么了叔磷?”我有些...
    開封第一講書人閱讀 164,782評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長奖磁。 經(jīng)常有香客問我改基,道長,這世上最難降的妖魔是什么咖为? 我笑而不...
    開封第一講書人閱讀 58,709評論 1 294
  • 正文 為了忘掉前任秕狰,我火速辦了婚禮,結(jié)果婚禮上躁染,老公的妹妹穿的比我還像新娘鸣哀。我一直安慰自己,他們只是感情好吞彤,可當(dāng)我...
    茶點故事閱讀 67,733評論 6 392
  • 文/花漫 我一把揭開白布我衬。 她就那樣靜靜地躺著,像睡著了一般备畦。 火紅的嫁衣襯著肌膚如雪低飒。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,578評論 1 305
  • 那天懂盐,我揣著相機(jī)與錄音褥赊,去河邊找鬼。 笑死莉恼,一個胖子當(dāng)著我的面吹牛拌喉,可吹牛的內(nèi)容都是我干的速那。 我是一名探鬼主播,決...
    沈念sama閱讀 40,320評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼尿背,長吁一口氣:“原來是場噩夢啊……” “哼端仰!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起田藐,我...
    開封第一講書人閱讀 39,241評論 0 276
  • 序言:老撾萬榮一對情侶失蹤荔烧,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后汽久,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體鹤竭,經(jīng)...
    沈念sama閱讀 45,686評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,878評論 3 336
  • 正文 我和宋清朗相戀三年景醇,在試婚紗的時候發(fā)現(xiàn)自己被綠了臀稚。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,992評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡三痰,死狀恐怖吧寺,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情散劫,我是刑警寧澤稚机,帶...
    沈念sama閱讀 35,715評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站舷丹,受9級特大地震影響抒钱,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜颜凯,卻給世界環(huán)境...
    茶點故事閱讀 41,336評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望仗扬。 院中可真熱鬧症概,春花似錦、人聲如沸早芭。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽退个。三九已至募壕,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間语盈,已是汗流浹背舱馅。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留刀荒,地道東北人代嗤。 一個月前我還...
    沈念sama閱讀 48,173評論 3 370
  • 正文 我出身青樓棘钞,卻偏偏與公主長得像,于是被迫代替她去往敵國和親干毅。 傳聞我的和親對象是個殘疾皇子宜猜,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,947評論 2 355

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

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,144評論 25 707
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件硝逢、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,104評論 4 62
  • 好好地一個周末姨拥。早上爬起來去郵寄快遞。折騰到中午才完事渠鸽。路癡的我每次出行只能靠Google map導(dǎo)航垫毙。奇...
    Ada秦小念閱讀 202評論 0 0
  • 在古人看來 好詩一定是描述一幅畫面的,比如“青山不老拱绑,為雪白頭”综芥。山,水猎拨,人膀藐,景色,不一而足红省,都是表象额各,都是知覺。...
    木卯丁閱讀 215評論 0 1