Tsim ib lub tshuab xam zauv hauv Kotlin: nws ua haujlwm li cas?
Peb qhia koj yuav ua li cas los tsim ib daim ntawv thov yooj yim rau kev suav cov lus qhia hauv Kotlin. Ntau precisely, Kotlin 1.3.21, Android 4, Android Studio 3. Cov kab lus yuav nthuav, ua ntej ntawm tag nrho cov, rau cov neeg uas tab tom pib taug kev hauv Android daim ntawv thov kev loj hlob. Nws tso cai rau koj kom nkag siab txog dab tsi thiab nws ua haujlwm li cas hauv daim ntawv thov.
Lub laij lej no yuav los ua ke thaum koj xav tau suav cov lus qhia los ntawm lub tuam txhab uas txiav txim siab siv sijhawm hauv tsev noj mov lossis chaw noj mov. Tau kawg, tsis yog txhua tus ib txwm tso cov lus qhia rau cov neeg ua haujlwm; qhov no yog ntau dua ntawm Western kab lis kev cai, tab sis cov txheej txheem ntawm kev tsim cov ntawv thov no nthuav dav rau txhua qhov xwm txheej.
Txij li lub luag haujlwm tseem ceeb ntawm daim ntawv thov yog suav tus nqi rau txhua tus neeg koom nrog hauv tsev noj mov sib sau ua ke, costPerPersonTextView ua lub luag haujlwm tseem ceeb.
EditText txwv cov tswv yim rau ib kab, qhov parameter no yuav tsum tau teem rau NumberDecimal inputType.
Tshooj cai no yog tsim nyog los xam cov nqi lus kom raug. Lub neej ntawd tus nqi ntawv yog 20. ImageButtons yog muab nrog cov cim hauv daim nplaub tshev nrog kev tso cai sau ntawv.
Luam ntu tag nrho thiab ntxiv cov hauv qab no (#5):
Tam sim no, thaum koj tso daim ntawv thov, muaj lub sijhawm los ntxiv cov nqi them nqi, cov khawm "Ntxiv / rho tawm" kuj ua haujlwm, tab sis tsis muaj dab tsi tshwm sim.
Hais txog cov nyees khawm thiab cov keyboards, Kotlin npaj txhua yam txias heev! Ntxiv cov cai hauv qab no rau txhua qhov increment thiab decrement functions
(#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()
}
}
Ntawm no cov cai tiv thaiv cov increment functions nrog cov nqi siab tshaj (MAX_TIP & MAX_PEOPLE). Tsis tas li ntawd, cov cai tiv thaiv qhov kev txo qis ua haujlwm nrog qhov tsawg kawg nkaus (MIN_TIP & MIN_PEOPLE).
Tam sim no peb koom nrog cov nyees khawm nrog cov neeg mloog hauv initViews muaj nuj nqi (#13):
private fun initViews() {
...
addTipButton.setOnClickListener(this)
subtractTipButton.setOnClickListener(this)
addPeopleButton.setOnClickListener(this)
subtractPeopleButton.setOnClickListener(this)
//TODO #15: Bind EditText to TextWatcher
}
Tam sim no koj tuaj yeem ntxiv tag nrho cov kev puas tsuaj, cov lus qhia, thiab tus naj npawb ntawm cov neeg tuaj koom. Zoo, tam sim no qhov tseem ceeb tshaj plaws ...
Nqe lus suav
Cov cai no suav cov nqi (#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)
}
Zoo, ntawm no yog ib txoj haujlwm hu ua uas ua rau nws muaj peev xwm coj mus rau hauv tus lej ntawm cov neeg hauv tuam txhab thiab suav cov lus qhia (#15):
private fun incrementTip() {
…
}
private fun decrementTip() {
…
}
private fun incrementPeople() {
…
}
private fun decrementPeople() {
…
}
Cia peb pib daim ntawv thov. Nws zoo nkaus li thiab ua haujlwm zoo. Tab sis nws yuav zoo dua.
Yog tias koj sim tshem tawm cov nqi thiab tom qab ntawd nce cov lus qhia lossis cov phooj ywg, daim ntawv thov yuav poob vim tias tseem tsis tau kuaj xyuas tus nqi xoom. Ntxiv mus, yog tias koj sim hloov tus nqi ntawm cov nqi, cov nqi yuav tsis hloov kho.