Android Kotlin----basketball简单程序

在程序中需要注意的几点:

  MVC设计模式:

  1. M层:适合做一些业务逻辑处理,比如数据库存取操作,网络操作,复杂的算法,耗时的任务等都在model层处理。
  2. V层:应用层中处理数据显示的部分,XML布局可以视为V层,显示Model层的数据结果。
  3. C层:在Android中,Activity处理用户交互问题,因此可以认为Activity是控制器,Activity读取V视图层的数据(eg.读取当前EditText控件的数据),控制用户输入(eg.EditText控件数据的输入),并向Model发送数据请求(eg.发起网络请求等)

  Intent意图:本次主要用到Intent的Activity跳转和数据的传递。

  屏幕旋转保存和恢复即时数据:onSaveInstanceState()函数和onRestoreInstanceState()函数的使用。


Model层:Player.kt

package com.example.dpl.basketball.Model

import android.os.Parcel
import android.os.Parcelable

/**
 * Created by dpl on 2018/6/21.
 */
class Player constructor(var league:String,var skill:String):Parcelable{
    constructor(parcel: Parcel) : this(
            parcel.readString(),
            parcel.readString()){
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
            parcel.writeString(league)
            parcel.writeString(skill)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<Player> {
        override fun createFromParcel(parcel: Parcel): Player {
            return Player(parcel)
        }

        override fun newArray(size: Int): Array<Player?> {
            return arrayOfNulls(size)
        }
    }

}

Utlilties类:Constants.kt

package com.example.dpl.basketball.Utillties

/**
 * Created by dpl on 2018/6/20.
 */
const val EXTRA_PLAYER="player"  //定义一常数

Controller层:

   WelcomeActivity.kt

package com.example.dpl.basketball.Controller

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import com.example.dpl.basketball.R
import kotlinx.android.synthetic.main.activity_welcome.*

class WelcomeActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_welcome)
        getStartedBtn.setOnClickListener {
            val leagueIntent=Intent(this, LeagueActivity::class.java)//::符号:得到类的Class对象
            startActivity(leagueIntent)
        }
    }
}

   LeagueActivity.kt

package com.example.dpl.basketball.Controller

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import com.example.dpl.basketball.Model.Player
import com.example.dpl.basketball.R
import com.example.dpl.basketball.Utillties.EXTRA_PLAYER
import kotlinx.android.synthetic.main.activity_league.*

class LeagueActivity : AppCompatActivity() {

    var player=Player("","")    //定义空数据对象
    override fun onSaveInstanceState(outState: Bundle?) {
        super.onSaveInstanceState(outState)
        outState?.putParcelable(EXTRA_PLAYER,player)   //解决屏幕旋转带来的数据销毁,用于保存及时数据
    }

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

    override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
        super.onRestoreInstanceState(savedInstanceState)
        if (savedInstanceState!=null){
            player=savedInstanceState.getParcelable(EXTRA_PLAYER)  //屏幕旋转后,用于恢复数据
        }
    }

    fun onMensClicked(view: View){
        womensLeagueBtn.isChecked=false
        coedLeagueBtn.isChecked=false
        player.league="Mens"
    }

    fun onWomensClicked(view: View){
        mensLeagueBtn.isChecked=false
        coedLeagueBtn.isChecked=false
        player.league="Womens"
    }

    fun onCoedClicked(view: View){
        womensLeagueBtn.isChecked=false
        mensLeagueBtn.isChecked=false
        player.league="Co-ed"
    }

    fun leagueNextClicked(view:View){
        if(player.league!="") {
            val skillActivity = Intent(this, SkillActivity::class.java)
            skillActivity.putExtra(EXTRA_PLAYER,player)
            startActivity(skillActivity)
        }else{
            Toast.makeText(this,"请选择一个选项",Toast.LENGTH_SHORT).show()
        }
    }
}

   SkillActivity.kt

package com.example.dpl.basketball.Controller

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import com.example.dpl.basketball.Model.Player
import com.example.dpl.basketball.R
import com.example.dpl.basketball.Utillties.EXTRA_PLAYER
import kotlinx.android.synthetic.main.activity_skill.*

class SkillActivity : AppCompatActivity() {

    lateinit var player: Player     //延迟初始化变量
    override fun onSaveInstanceState(outState: Bundle?) {
        super.onSaveInstanceState(outState)
        outState?.putParcelable(EXTRA_PLAYER,player)   //解决屏幕旋转带来的数据销毁,用于保存及时数据
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_skill)
        player=intent.getParcelableExtra(EXTRA_PLAYER)
        //Toast.makeText(this,"您选择的是${league}",Toast.LENGTH_SHORT).show()
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
        super.onRestoreInstanceState(savedInstanceState)
        if (savedInstanceState!=null){
            player=savedInstanceState.getParcelable(EXTRA_PLAYER)  //屏幕旋转后,用于恢复数据
        }
    }

    fun onBeginnerClicked(view:View){
        ballerSkillBtn.isChecked=false
        player.skill="beginner"
    }

    fun onBallerClicked(view: View){
        beginnerSkillBtn.isChecked=false
        player.skill="baller"
    }

    fun onFinishClicked(view: View){
        if(player.skill!=""){
            val finishActivity=Intent(this, FinishActivity::class.java)
            finishActivity.putExtra(EXTRA_PLAYER,player)
            startActivity(finishActivity)
        }else{
            Toast.makeText(this,"请选择一个选项!",Toast.LENGTH_SHORT).show()
        }
    }
}

   FinishActivity.kt

package com.example.dpl.basketball.Controller

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import com.example.dpl.basketball.Model.Player
import com.example.dpl.basketball.R
import com.example.dpl.basketball.Utillties.EXTRA_PLAYER
import kotlinx.android.synthetic.main.activity_finish.*

class FinishActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_finish)
        val player=intent.getParcelableExtra<Player>(EXTRA_PLAYER)
        searchLeagueSkillText.text="Looking for a ${player.league} ${player.skill} league near you..."
    }
}

View层:

    activity_welcome.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.dpl.basketball.Controller.WelcomeActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@drawable/swoosh_button"
        android:scaleType="centerCrop"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        app:srcCompat="@drawable/welcomescreenbg"
        android:contentDescription="@string/background_image" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="88dp"
        android:layout_height="30dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="24dp"
        android:scaleType="fitCenter"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/imageView"
        app:srcCompat="@drawable/swooshlogo"
        android:contentDescription="@string/app_logo" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="55dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="80dp"
        android:fontFamily="@font/montserrat"
        android:text="@string/go_all_in"
        android:textColor="@android:color/white"
        android:textSize="46sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView2" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:fontFamily="@font/montserrat"
        android:text="@string/on_ging_all_out"
        android:textColor="@android:color/white"
        android:textSize="22sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="192dp"
        android:layout_height="37dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="32dp"
        android:text="@string/find_a_team_in_you_city_and_dominate_the_league"
        android:textAlignment="center"
        android:textColor="@android:color/white"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />

    <Button
        android:id="@+id/getStartedBtn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:background="@drawable/swoosh_button"
        android:fontFamily="@font/montserrat"
        android:text="@string/get_started"
        android:textColor="@android:color/white"
        android:textSize="14sp"
        app:layout_constraintBottom_toBottomOf="@+id/imageView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>

    activity_league.xml   

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.dpl.basketball.Controller.LeagueActivity">

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scaleType="centerCrop"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0"
        app:srcCompat="@drawable/desiredleaguebg"
        android:contentDescription="@string/background_image"/>

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="88dp"
        android:layout_height="30dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="24dp"
        android:scaleType="fitCenter"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/swooshlogo"
        android:contentDescription="@string/app_logo" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="25dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="32dp"
        android:fontFamily="@font/montserrat"
        android:text="@string/desired_league"
        android:textColor="@android:color/white"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView2" />

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="361dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="40dp"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView4">

        <ToggleButton
            android:id="@+id/mensLeagueBtn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/swoosh_toggle"
            android:fontFamily="@font/montserrat"
            android:onClick="onMensClicked"
            android:text="@string/mens"
            android:textColor="@android:color/white"
            android:textOff="@string/mens"
            android:textOn="@string/mens"
            android:textSize="14sp"
            tools:layout_editor_absoluteX="161dp"
            tools:layout_editor_absoluteY="235dp" />

        <ToggleButton
            android:id="@+id/womensLeagueBtn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:layout_marginTop="16dp"
            android:background="@drawable/swoosh_toggle"
            android:fontFamily="@font/montserrat"
            android:onClick="onWomensClicked"
            android:text="@string/womens"
            android:textColor="@android:color/white"
            android:textOff="@string/womens"
            android:textOn="@string/womens" />

        <ToggleButton
            android:id="@+id/coedLeagueBtn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/swoosh_toggle"
            android:fontFamily="@font/montserrat"
            android:onClick="onCoedClicked"
            android:text="@string/co_ed"
            android:textColor="@android:color/white"
            android:textOff="@string/co_ed"
            android:textOn="@string/co_ed" />
    </LinearLayout>

    <Button
        android:id="@+id/leagueNextBtn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:background="@drawable/swoosh_toggle"
        android:fontFamily="@font/montserrat"
        android:onClick="leagueNextClicked"
        android:text="@string/next"
        android:textColor="@android:color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>

    activity_skill.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.dpl.basketball.Controller.SkillActivity">

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scaleType="centerCrop"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/beginnerballerbg"
        android:contentDescription="@string/background_image" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="88dp"
        android:layout_height="30dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="24dp"
        android:contentDescription="@string/app_logo"
        android:scaleType="fitCenter"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/swooshlogo" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="24dp"
        android:fontFamily="@font/montserrat"
        android:text="@string/i_am_a"
        android:textColor="@android:color/white"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView2" />

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="32dp"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView5">

        <ToggleButton
            android:id="@+id/beginnerSkillBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:layout_weight="1"
            android:background="@drawable/swoosh_toggle"
            android:fontFamily="@font/montserrat"
            android:onClick="onBeginnerClicked"
            android:text="@string/beginner"
            android:textColor="@android:color/white"
            android:textOff="@string/beginner"
            android:textOn="@string/beginner"
            tools:layout_editor_absoluteX="36dp"
            tools:layout_editor_absoluteY="209dp" />

        <ToggleButton
            android:id="@+id/ballerSkillBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_weight="1"
            android:background="@drawable/swoosh_toggle"
            android:fontFamily="@font/montserrat"
            android:onClick="onBallerClicked"
            android:text="@string/baller"
            android:textColor="@android:color/white"
            android:textOff="@string/baller"
            android:textOn="@string/baller" />
    </LinearLayout>

    <Button
        android:id="@+id/finishSkillBtn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:background="@drawable/swoosh_toggle"
        android:fontFamily="@font/montserrat"
        android:onClick="onFinishClicked"
        android:text="@string/finish"
        android:textColor="@android:color/white"
        android:textSize="14sp"
        app:layout_constraintBottom_toBottomOf="@+id/imageView4"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>

    activity_finish.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.dpl.basketball.Controller.FinishActivity">

    <ImageView
        android:id="@+id/imageView5"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scaleType="centerCrop"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/exercisebg"/>

    <TextView
        android:id="@+id/searchLeagueSkillText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:textAlignment="center"
        android:textColor="@android:color/white"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.45"
        tools:text="@string/search_league" /><!--可动态变动内容-->

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="24dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/searchLeagueSkillText" />
</android.support.constraint.ConstraintLayout>

运行效果图:


程序源码:https://github.com/dpl12/basketball

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值