Te hanga i te tatau tatau ki Kotlin: me pehea te mahi?

Te hanga i te tatau tatau ki Kotlin: me pehea te mahi?

Ka korerotia e matou ki a koe me pehea te hanga i tetahi tono ngawari mo te tatau i nga tohutohu i Kotlin. Ko te mea tika, Kotlin 1.3.21, Android 4, Android Studio 3. Ko te tuhinga ka tino pai, tuatahi, mo te hunga e timata ana i to raatau haerenga ki te whakawhanaketanga tono Android. Ka taea e koe te mohio he aha me pehea te mahi i roto i te tono.

Ka whai hua tenei tatauranga ina hiahia koe ki te tatau i te nui o nga tohutohu mai i tetahi kamupene e whakatau ana ki te noho wa ki tetahi wharekai, kawhe ranei. Ae ra, kaore nga tangata katoa e tuku he tohu ki nga kaitarai; he tikanga ake tenei ki te Hauauru, engari he mea whakamere te tukanga ki te whakawhanake i taua tono.

Ka whakamahara matou: mo nga kaipānui katoa o "Habr" - he utu mo te 10 rubles i te wa e whakauru ana ki tetahi akoranga Skillbox ma te whakamahi i te waehere whakatairanga "Habr".

Ka tūtohu a Skillbox: Akoranga mahi "Kaiwhakawhanake pūkoro PRO.

Koinei te ahua o te tono e mahi ana:

Te hanga i te tatau tatau ki Kotlin: me pehea te mahi?

Ka uru koe ki te ōrau e hiahiatia ana o te tapeke moni, te maha o nga kaiuru hui, ka whiwhi i te hua - te nui o te tohu ka waiho e koe.

Tīmata

He penei te ahua o te atanga tono katoa:
Te hanga i te tatau tatau ki Kotlin: me pehea te mahi?

Te hanga i te tatau tatau ki Kotlin: me pehea te mahi?

Mahi tuatahi - te tikiake i te turanga kaupapa. Whakatuwheratia ki te Android Studio 3.0, i muri mai ranei. Ka hangahia, ka whakarewahia e matou te kaupapa ka kite i tetahi mata ma. Kei te pai nga mea katoa, me pena tonu.

Te hanga i te tatau tatau ki Kotlin: me pehea te mahi?

Te hanga i te tatau tatau ki Kotlin: me pehea te mahi?

Ko nga mahi a nga kaiwhakamahi ka tuhia ki roto i te kaupapa i roto i te raupapa o te waa kia marama ai nga mea katoa. Hei tiro, tuwhera Tirohanga -> Utauta Windows -> TODO.

Ka akohia e matou te kaupapa me te whakatuwhera i nga tae.xml hei arotake i te papanga tae. Kei roto i te strings.xml he raraunga kuputuhi (waitohu), me styles.xml he maha nga tauira momotuhi.

Te whanaketanga waahanga utu

Whakatuwheratia te activity_main.xml me te taapiri i te waehere i raro nei ki te LinearLayout (#1):

<TextView
    android_id="@+id/expensePerPersonTextView"
    android_layout_width="match_parent"
    android_layout_height="wrap_content"
    android_paddingTop="30dp"
    style="@style/h1Bold"
    android_textColor="@color/colorAccent"
    android_text="0"/>
 
<TextView
    android_layout_width="match_parent"
    android_layout_height="wrap_content"
    android_paddingBottom="25dp"
    style="@style/h2"
    android_textColor="@color/colorAccent"
    android_text="@string/perPersonStaticText"/>

Inaianei ka taea e koe te tarai i te raarangi uara, te takaro ranei me nga tae ma te whakamahi taputapu rauemi.io.

Inaianei he penei te ahua o te kaupapa:

Te hanga i te tatau tatau ki Kotlin: me pehea te mahi?
Ka taea e koe te kite, ka tatauhia nga utu i runga i nga raraunga i whakauruhia e te kaiwhakamahi.

Te whanaketanga o te wahanga kaute

Tāpirihia te waehere i raro nei ki te LinearLayout i muri i te Wāhanga Whakapaunga (#2):

<LinearLayout
    android_layout_width="match_parent"
    android_layout_height="match_parent"
    android_orientation="vertical"
    android_background="@color/colorAccent">
 
<! — TODO #3: Build Bill Section →
 
… 
</LinearLayout>

Ka kati matou i te LinearLayout i muri i te rarangi o nga TODO, ka taapirihia he waehere hou, ka tuu ki roto i te LinearLayout (#3):

<TextView
      android_layout_margin="15dp"
      android_layout_width="match_parent"
      android_layout_height="wrap_content"
      android_textColor="@color/colorWhite"
      style="@style/h4"
      android_text="@string/billStaticText"/>
 
<EditText
      android_id="@+id/billEditText"
      android_layout_width="match_parent"
      android_layout_height="wrap_content"
      android_textColor="@color/colorWhite"
      android_inputType="numberDecimal"
      android_maxLines="1"
      style="@style/h2Bold"
      android_text="0"/>

I te mea ko te mahi matua o te tono he tatau i nga utu takitahi mo ia kaiuru ki tetahi huihuinga wharekai, ko costPerPersonTextView te mahi matua.

Ka whakawhāitihia e EditText te tāuru ki te raina kotahi, me tautuhi tenei tawhā ki te NumberDecimal inputType.

Te hanga i te tatau tatau ki Kotlin: me pehea te mahi?
Ka whakarewahia e matou te kaupapa mo te whakamatautau me te whakauru i nga tawhā mo te kino nui (nga kapu pakaru, pereti, me etahi atu)

Te whakawhanaketanga o te waahanga "Tangata me nga Tohutohu".

Hei taapiri i te kowhiringa rōrahi matamata, whakapirihia te waehere i raro nei ki te waahanga LinearLayout hou (#4):

<TextView
      android_layout_margin="15dp"
      android_layout_width="match_parent"
      android_layout_height="wrap_content"
      android_textColor="@color/colorWhite"
      style="@style/h4"
      android_text="@string/tipStaticText"/>
 
<LinearLayout
      android_layout_width="match_parent"
      android_layout_height="wrap_content"
      android_orientation="horizontal">
 
<ImageButton
        android_id="@+id/subtractTipButton"
        style="@style/operationButton"
        android_layout_marginLeft="20dp"
        android_layout_marginStart="20dp"
        android_src="@drawable/subtract"/>
 
<TextView
        android_id="@+id/tipTextView"
        android_layout_margin="15dp"
        android_layout_width="0dp"
        android_layout_height="wrap_content"
        android_textColor="@color/colorWhite"
        android_layout_weight="1"
        style="@style/h2Bold"
        android_text="20%"/>
 
<ImageButton
        android_id="@+id/addTipButton"
        style="@style/operationButton"
        android_layout_marginEnd="20dp"
        android_layout_marginRight="20dp"
        android_src="@drawable/add"/>
 
</LinearLayout>

Ko tenei waahanga o te waehere e tika ana kia tika te tatau i te moni matamata. Ko te uara kuputuhi taunoa ko te 20. Ko nga ImageButtons e whakaratohia ana me nga tohu kei roto i te kōpaki me nga whakaaetanga tuhi.

Tāruatia katoatia te wāhanga ka tāpirihia te mea e whai ake nei (#5):

  • ids ImageButton (tangoPeopleButton, addPeopleButton)
  • Kuputuhi Kuputuhi(numberOfPeopleStaticText, numberOfPeopleTextView)
  • Kuputuhi Taunoa mo numberOfPeopleTextView (me 4).

Te hanga i te tatau tatau ki Kotlin: me pehea te mahi?

Na, ka whakarewahia e koe te tono, ka whai waahi ki te taapiri i te nama nama, ka mahi ano nga paatene "Taapiri / Tango", engari kaore ano kia puta.

Tāpiri Tirohanga

Whakatuwherahia te MainActivity.kt ka taapiri atu ki te mahi initViews (#6):

private fun initViews() {
        expensePerPersonTextView = findViewById(R.id.expensePerPersonTextView)
        billEditText = findViewById(R.id.billEditText)
 
addTipButton = findViewById(R.id.addTipButton)
        tipTextView = findViewById(R.id.tipTextView)
        subtractTipButton = findViewById(R.id.subtractTipButton)
 
addPeopleButton = findViewById(R.id.addPeopleButton)
        numberOfPeopleTextView = findViewById(R.id.numberOfPeopleTextView)
        subtractPeopleButton = findViewById(R.id.subtractPeopleButton)
 
//TODO #8: Bind Buttons to Listener
 
//TODO #16: Bind EditText to TextWatcher
 
}

Te whakaoti i nga paatene

Hei taapiri tautoko mo nga paatene paatene, ka whakatinanahia e matou te View.OnClickListener ki te taumata o te akomanga (#7):

akomanga MainActivity: AppCompatActivity(), View.OnClickListener {

Kare e taea te whakahiato i te kaupapa i naianei; me mahi etahi atu kaupae (#8):

override fun onClick(v: View?) {
        when (v?.id) {
            R.id.addTipButton -> incrementTip()
            R.id.subtractTipButton -> decrementTip()
            R.id.addPeopleButton -> incrementPeople()
            R.id.subtractPeopleButton -> decrementPeople()
        }
    }

Mo nga patene me nga huringa, ka whakarite a Kotlin i nga mea katoa tino pai! Tāpirihia te waehere i raro nei ki nga mahi pikinga me te whakaheke
(#9 – #12):

private fun incrementTip() {
        if (tipPercent != MAX_TIP) {
            tipPercent += TIP_INCREMENT_PERCENT
            tipTextView.text = String.format("%d%%", tipPercent)
        }
    }
 
private fun decrementTip() {
        if (tipPercent != MIN_TIP) {
            tipPercent -= TIP_INCREMENT_PERCENT
            tipTextView.text = String.format("%d%%", tipPercent)
        }
    }
 
private fun incrementPeople() {
        if (numberOfPeople != MAX_PEOPLE) {
            numberOfPeople += PEOPLE_INCREMENT_VALUE
            numberOfPeopleTextView.text = numberOfPeople.toString()
        }
    }
 
private fun decrementPeople() {
        if (numberOfPeople != MIN_PEOPLE) {
            numberOfPeople -= PEOPLE_INCREMENT_VALUE
            numberOfPeopleTextView.text = numberOfPeople.toString()
        }
    }

I konei ka tiakina e te waehere nga mahi pikinga me nga uara teitei (MAX_TIP & MAX_PEOPLE). Hei taapiri, ka tiakina e te waehere nga mahi whakaheke me nga uara iti (MIN_TIP & MIN_PEOPLE).

Inaianei ka honoa e matou nga paatene ki te hunga whakarongo ki te mahi initViews (#13):

private fun initViews() {
 
...
 
addTipButton.setOnClickListener(this)
        subtractTipButton.setOnClickListener(this)
 
addPeopleButton.setOnClickListener(this)
        subtractPeopleButton.setOnClickListener(this)
 
//TODO #15: Bind EditText to TextWatcher
}

Te hanga i te tatau tatau ki Kotlin: me pehea te mahi?

Ka taea e koe te taapiri katoa o nga pakaru, nga tohutohu, me te maha o nga kaiuru hui. Inaianei ko te mea nui ...

Wāhanga tātai utu

Ka tatauhia e tenei waehere nga utu (#14):

private fun calculateExpense() {
 
val totalBill = billEditText.text.toString().toDouble()
 
val totalExpense = ((HUNDRED_PERCENT + tipPercent) / HUNDRED_PERCENT) * totalBill
        val individualExpense = totalExpense / numberOfPeople
 
expensePerPersonTextView.text = String.format("$%.2f", individualExpense)
 
}

Ana, ka kiia he mahi e taea ai te whai whakaaro ki te maha o nga tangata o te kamupene me te tatau i nga tohutohu (#15):

private fun incrementTip() {
 
…
 
}
 
private fun decrementTip() {
 
…
 
}
 
private fun incrementPeople() {
 
…
 
}
 
private fun decrementPeople() {
 
…
 
}

Kia whakarewahia te tono. He ahua me te mahi pai. Engari ka pai ake pea.

Mena ka ngana koe ki te tango i te nui o te pire ka piki ake te maha o nga tohutohu, hoa ranei, ka pakaru te tono na te mea kaore ano kia tirohia te uara utu kore. I tua atu, ki te ngana koe ki te whakarereke i te nui o te pire, kaore e whakahouhia nga utu.

Nga mahi whakamutunga

Tāpiri Kuputuhi (#16):

akomanga MainActivity: AppCompatActivity(), View.OnClickListener, TextWatcher {

Kātahi ka tāmau i te kaiwhakarongo billEditText (#17):

billEditText.addTextChangedListener(tenei)

I tua atu, ka taapirihia he waehere hei mahi i te TextWatcher (#18):

override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        if (!billEditText.text.isEmpty()) {
            calculateExpense()
        }
    }
override fun afterTextChanged(s: Editable?) {}

    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}

Te hanga i te tatau tatau ki Kotlin: me pehea te mahi?

Ana, inaianei kua tino mahi nga mea katoa! Tena koe, kua tuhia e koe taau ake "Tika Tatau" ki Kotlin.

Te hanga i te tatau tatau ki Kotlin: me pehea te mahi?

Ka tūtohu a Skillbox:

Source: will.com

Tāpiri i te kōrero