SJ-0141组L2_课程篮球计分器总结

观看L2课程后,主要知识点为:

  • 布局里按钮和代码的关联:
    通过相同的方法名来映射关联
    例如:
                   <Button
                    android:id="@+id/cType1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:backgroundTint="@color/colorPrimary"
                    android:text="一分球"
                    android:textColor="@android:color/white"
                    android:onClick="addScore"/>
        public void addScore(View view){
        switch (view.getId()){
            case R.id.cType1:
                toAdd(1,1);
                break;
            case R.id.cType2:
                toAdd(2,1);
                break;
            case R.id.cType3:
                toAdd(3,1);
                break;
            case R.id.hType1:
                toAdd(1,0);
                break;
            case R.id.hType2:
                toAdd(2,0);
                break;
            case R.id.hType3:
                toAdd(3,0);
                break;
        }
    }

上面的xml中button中最后一行的onClick="addScore"对应着代码中的addScore方法。

  • 全局变量与局部变量

    全局变量:
    全局变量又称之为成员变量,定义在类的直属区域。
    局部变量:
    定义在方法的内部。
    全局变量与局部变量的区别:
    1. 作用域不同:全局变量的作用域为整个程序,而局部变量的作用域为当前函数或循环等。
    2. 内存存储方式不同:全局变量存储在全局数据区中,局部变量存储在栈区。
    3. 生命期不同:全局变量的生命期和主程序一样,随程序的销毁而销毁,局部变量在函数内部或循环内部,随函数的退出或循环退出就不存在了。
    4. 使用方式不同:全局变量在声明后程序的各个部分都可以用到,但是局部变量只能在局部使用。函数内部会优先使用局部变量再使用全局变量。
    需要注意的点:
    在使用变量时需要遵循的原则为:就近原则
    首先在局部范围找,有就使用;接着在成员位置找。


下面我们来实践一下:篮球计分器(为纪念国足1:0战胜韩国特改为 足球计分器)

我们先来看一下效果图:


足球计分器.gif
  • activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@mipmap/bg_bar">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#DF292421">

        <LinearLayout
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:layout_centerInParent="true"
            android:orientation="horizontal"
            android:weightSum="2">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/cScore"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="30dp"
                    android:text="1"
                    android:textColor="@android:color/white"
                    android:drawableLeft="@drawable/teamc"
                    android:textSize="48sp" />

                <Button
                    android:id="@+id/cType1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:backgroundTint="@color/colorPrimary"
                    android:text="一分球"
                    android:textColor="@android:color/white"
                    android:onClick="addScore"/>

                <Button
                    android:id="@+id/cType2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:backgroundTint="@color/colorPrimary"
                    android:text="二分球"
                    android:textColor="@android:color/white"
                    android:onClick="addScore"/>

                <Button
                    android:id="@+id/cType3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:backgroundTint="@color/colorPrimary"
                    android:text="三分球"
                    android:textColor="@android:color/white"
                    android:onClick="addScore"/>
            </LinearLayout>

            <RelativeLayout
                android:layout_width="1dp"
                android:layout_height="300dp"
                android:layout_gravity="center"
                android:background="@android:color/white"></RelativeLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/hScore"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="30dp"
                    android:text="0"
                    android:drawableLeft="@drawable/teamh"
                    android:textColor="@android:color/white"
                    android:textSize="48sp" />

                <Button
                    android:id="@+id/hType1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:backgroundTint="@color/colorPrimary"
                    android:text="一分球"
                    android:textColor="@android:color/white"
                    android:onClick="addScore"/>

                <Button
                    android:id="@+id/hType2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:backgroundTint="@color/colorPrimary"
                    android:text="二分球"
                    android:textColor="@android:color/white"
                    android:onClick="addScore"/>

                <Button
                    android:id="@+id/hType3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:backgroundTint="@color/colorPrimary"
                    android:text="三分球"
                    android:textColor="@android:color/white"
                    android:onClick="addScore"/>
            </LinearLayout>
        </LinearLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@id/content"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="30dp"
            android:text="足球计分器"
            android:textColor="@android:color/white"
            android:textSize="36dp"
            android:textStyle="bold" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:backgroundTint="@android:color/holo_red_light"
            android:text="重置"
            android:paddingLeft="50dp"
            android:paddingTop="20dp"
            android:paddingRight="50dp"
            android:paddingBottom="20dp"
            android:textColor="@android:color/white"
            android:layout_marginTop="40dp"
            android:layout_below="@id/content"
            android:layout_centerHorizontal="true"
            android:onClick="reSetScore"/>
    </RelativeLayout>
</RelativeLayout>
  • MainActivity.class
package com.bf.score.score1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    //中国计分板整数变量的初始值
    private int mCnum = 1;
    //韩国计分板整数变量的初始值
    private int mHnum = 0;
    //中国计分板
    private TextView mCscoreTV;
    //韩国计分板
    private TextView mHscoreTV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //加载布局文件
        setContentView(R.layout.activity_main);
        //初始化两个队的计分板
        mCscoreTV = (TextView) findViewById(R.id.cScore);
        mHscoreTV = (TextView) findViewById(R.id.hScore);
    }
    /*
    * 响应布局文件中加分button点击后代码执行的方法
    * */
    public void addScore(View view){
        /*switch(..){ case }语句
        * 此格式的语句是跟if...else...相同的逻辑判断语句
        * switch的小括号里面传的是需要判断的目标
        * case后面为是否匹配的值
        * */
        switch (view.getId()){
            case R.id.cType1:
                toAdd(1,1);
                break;
            case R.id.cType2:
                toAdd(2,1);
                break;
            case R.id.cType3:
                toAdd(3,1);
                break;
            case R.id.hType1:
                toAdd(1,0);
                break;
            case R.id.hType2:
                toAdd(2,0);
                break;
            case R.id.hType3:
                toAdd(3,0);
                break;
        }
    }

    /**
     * 执行加分的计算,并且赋值给对应的计分板
     * @param type
     *      为了区分给那一个计分板加分
     *      1是中国
     *      0是韩国
     * @param num
     *       需要加几分
     */
    private void toAdd(int num,int type){
        if(type == 1){
            // 为中国队加分
            mCnum = mCnum + num;
            mCscoreTV.setText(mCnum+"");
        }else if(type == 0){
            //为韩国队加分
            mHnum = mHnum + num;
            mHscoreTV.setText(mHnum+"");
        }
    }
    /*
    * 重置计分器
    * */
    public void reSetScore(View view){
        mCnum = 1;
        mCscoreTV.setText(mCnum+"");
        mHnum = 0;
        mHscoreTV.setText(mHnum+"");
    }
}

以上就是足球计分器的主要代码,有很详细的注释
有什么建议留言,大家一起学习进步。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,869评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,716评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,223评论 0 357
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,047评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,089评论 6 395
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,839评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,516评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,410评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,920评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,052评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,179评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,868评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,522评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,070评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,186评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,487评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,162评论 2 356

推荐阅读更多精彩内容

  • 一、温故而知新 1. 内存不够怎么办 内存简单分配策略的问题地址空间不隔离内存使用效率低程序运行的地址不确定 关于...
    SeanCST阅读 7,815评论 0 27
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,152评论 30 470
  • 刚刚出去不久,陪母亲回老家探亲的朋友,撇下母亲,自己又赶回来,我问为啥呢? “损友啊!都是损友闹的。”原来啊,此君...
    卫茳阅读 724评论 4 3
  • ①看像素的比赛,留意的话会发现像素组成的卡通人物,但是卡通人物的颜色少,就成了发范围多少个像素对应着某个数字区间,...
    60476a8c85d4阅读 229评论 0 0
  • CSS格式化排版 1、字体 我们可以使用css样式为网页中的文字设置字体、字号、颜色等样式属性。下面我们来看一个例...
    张文靖同学阅读 1,287评论 0 3