Kotlin學(xué)習(xí)系列之籃球計分器

今天給大家?guī)淼氖且粋€Kotlin版本的籃球計分器,之前用Java寫過一遍,今天就用Kotlin重新寫了一遍,大家也可以看一下這兩個版本有什么不同,通過上次介紹的那些知識點,寫出一個這樣的小程序還沒什么問題的,這個之前也是參加Google Study Jam學(xué)習(xí)中的一個小練習(xí),但是我感覺這個平常打球什么的時候還是可以用上,同時這個比那個練習(xí)稍微做了點改變,同時后面我也會繼續(xù)完善這個小項目,增添更多的功能,好java版本地址java版本大家可以去這個地址看,下面跟大家說下Kotlin這個版本是如何寫的首先先看一下頁面

界面

界面就是這樣的一個簡單界面,可以輸入兩隊名稱,然后點擊分數(shù)就可以在總分數(shù)那里添加,用了LinearLayout布局,兩邊完全一致,最下面有一個歸零的按鈕。
現(xiàn)在上下寫法
首先肯定先是把Mainactivity轉(zhuǎn)成Kotlin文件,這里上次已經(jīng)分享過了,過程都一樣就不細說了,不過有個地方需要注意在app的gradle文件中需要添加apply plugin: 'kotlin-android-extensions'這行代碼,不然編譯的時候會報錯,這里需要注意一下。
xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.zhouzhongyi.kotlinmatch.MainActivity">

    <LinearLayout
        android:id="@+id/ll_point"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:orientation="vertical">

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="30dp"
                android:hint="請輸入主隊名稱" />

            <TextView
                android:id="@+id/tv_homepoint"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:text="00"
                android:textSize="50dp" />

            <Button
                android:id="@+id/bt_hpoint3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="30dp"
                android:onClick="addThreeForHome"
                android:text="+3" />

            <Button
                android:id="@+id/bt_hpoint2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:onClick="addTwoForHome"
                android:text="+2" />

            <Button
                android:id="@+id/bt_hpoint1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:onClick="addOneForHome"
                android:text="+1" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:orientation="vertical">

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="30dp"
                android:hint="請輸入客隊名稱" />

            <TextView
                android:id="@+id/tv_guestpoint"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:text="00"
                android:textSize="50dp" />

            <Button
                android:id="@+id/bt_gpoint3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="30dp"
                android:onClick="addThreeForGuest"
                android:text="+3" />

            <Button
                android:id="@+id/bt_gpoint2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:onClick="addTwoForGuest"
                android:text="+2" />

            <Button
                android:id="@+id/bt_gpoint1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:onClick="addOneForGuest"
                android:text="+1" />

        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:gravity="center">

        <Button
            android:id="@+id/bt_reset"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:onClick="Reset"
            android:text="Reset" />

    </LinearLayout>
</RelativeLayout>

大家看下就好,這里都很簡單
然后是Kotlin代碼

class MainActivity : AppCompatActivity() {
    var scoreHome = 0//這里它自己可以判斷出是Int型,所以就不用寫: Int
    var scoreGuest = 0


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    fun displayForHome(score: Int) {
        tv_homepoint.text = score.toString()
    }

    fun addThreeForHome(view: View) {
        scoreHome = scoreHome + 3
        displayForHome(scoreHome)
    }

    fun addTwoForHome(view: View) {
        scoreHome = scoreHome + 2
        displayForHome(scoreHome)
    }

    fun addOneForHome(view: View) {
        scoreHome = scoreHome + 1
        displayForHome(scoreHome)
    }

    fun displayForGuest(score: Int){
        tv_guestpoint.text = score.toString()
    }
    fun addThreeForGuest(view: View){
        scoreGuest = scoreGuest + 3
        displayForGuest(scoreGuest)
    }
    fun addTwoForGuest(view: View){
        scoreGuest = scoreGuest +2
        displayForGuest(scoreGuest)
    }
    fun addOneForGuest(view: View){
        scoreGuest = scoreGuest +1
        displayForGuest(scoreGuest)
    }
    fun Reset(view: View){
        scoreHome = 0
        scoreGuest = 0
        displayForHome(scoreHome)
        displayForGuest(scoreGuest)

    }

}

好了,總共就這么多代碼,大家可以看一下,邏輯也非常簡單,大家也可以看下跟Java版本有什么不一樣的地方,同時我這里也把地址放上來,大家也可以去下載下來看一下,大家自己有什么想法也可以往上添加~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容