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+"");
    }
}

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

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

友情链接更多精彩内容