කොටලින් හි සරල ඉඟි ගණනය කිරීමේ යෙදුමක් නිර්මාණය කරන්නේ කෙසේදැයි අපි ඔබට පෙන්වන්නෙමු. වඩාත් නිවැරදිව කිවහොත්, Kotlin 1.3.21, Android 4, Android Studio 3. ලිපිය රසවත් වනු ඇත, පළමුවෙන්ම, Android යෙදුම් සංවර්ධනය කිරීමේදී ඔවුන්ගේ ගමන ආරම්භ කරන අය සඳහා. යෙදුම තුළ ක්රියා කරන්නේ කුමක්ද සහ කෙසේද යන්න තේරුම් ගැනීමට එය ඔබට ඉඩ සලසයි.
අවන්හලක හෝ කැෆේ එකක කාලය ගත කිරීමට තීරණය කරන සමාගමකින් ඉඟි ප්රමාණය ගණනය කිරීමට අවශ්ය වන විට එවැනි කැල්ක්යුලේටරය ප්රයෝජනවත් වේ. ඇත්ත වශයෙන්ම, සෑම කෙනෙකුම සහ සෑම විටම වේටර්වරුන් සඳහා තේ පිටත් නොවේ, මෙය බටහිර සම්ප්රදායට වඩා වැඩි ය, නමුත් එවැනි යෙදුමක් සංවර්ධනය කිරීමේ ක්රියාවලිය ඕනෑම අවස්ථාවක සිත්ගන්නා සුළුය.
අපි ඔබට මතක් කරමු: "Habr" හි සියලුම පාඨකයින් සඳහා - "Habr" ප්රවර්ධන කේතය භාවිතයෙන් ඕනෑම Skillbox පාඨමාලාවකට ලියාපදිංචි වන විට රූබල් 10 ක වට්ටමක්.
Skillbox නිර්දේශ කරයි: ප්රායෝගික පාඨමාලාව
"ජංගම සංවර්ධක PRO .
යෙදුම ක්රියාත්මක වන විට එය පෙනෙන්නේ කෙසේද යන්න මෙන්න:
ඔබ මුළු මුදලෙන් අපේක්ෂිත ප්රතිශතය, රැස්වීමට සහභාගී වන සංඛ්යාව ඇතුළත් කර ප්රති result ලය ලබා ගන්න - ඉතිරි කළ යුතු ඉඟි ප්රමාණය.
ආරම්භ කිරීම
යෙදුමේ සම්පූර්ණ අතුරු මුහුණත මේ වගේ ය:
පළමු ක්රියාව -
සෑම දෙයක්ම පැහැදිලි වන පරිදි පරිශීලක ක්රියා කාලානුක්රමික අනුපිළිවෙලින් ව්යාපෘතියේ ලියා ඇත. එය බැලීමට, View -> Tool Windows -> TODO විවෘත කරන්න.
අපි ව්යාපෘතිය අධ්යයනය කර වර්ණ මාලාව ඇගයීමට color.xml විවෘත කරමු. strings.xml හි පෙළ දත්ත (ශීර්ෂ පාඨ) අඩංගු වන අතර styles.xml හි අකුරු සැකිලි කිහිපයක් අඩංගු වේ.
පිරිවැය අංශය සංවර්ධනය
Activity_main.xml විවෘත කර පහත කේතය 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"/>
දැන් ඔබට අගයන් නාමාවලිය හැඩගස්වා ගැනීමට හෝ වර්ණ සමඟ සෙල්ලම් කිරීමට හැකිය
දැන් ව්යාපෘතිය මේ වගේ ය:
ඔබට පෙනෙන පරිදි, පිරිවැය ගණනය කිරීම පරිශීලකයා විසින් ඇතුළත් කරන ලද දත්ත මත පදනම් වේ.
ගිණුම් අංශය සංවර්ධනය කිරීම
වියදම් අංශයෙන් (#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>
TODOs ලැයිස්තුවෙන් පසුව LinearLayout වසන්න, ඉන්පසු නව කේතය එකතු කරන්න, එය 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"/>
යෙදුමේ ප්රධාන කාර්යය වන්නේ අවන්හලේ රැස්වීම්වල එක් එක් සහභාගිවන්නන් සඳහා තනි පිරිවැය ගණනය කිරීම බැවින්, costPerPersonTextView ප්රධාන කාර්යභාරය ඉටු කරයි.
EditText ආදානය එක් පේළියකට සීමා කරයි, මෙම පරාමිතිය NumberDecimal inputType වෙත සැකසිය යුතුය.
අපි පරීක්ෂණය සඳහා ව්යාපෘතිය ආරම්භ කර සම්පූර්ණ හානිය (කැඩුණු කෝප්ප, තහඩු, ආදිය) පරාමිතීන් ඇතුළත් කරන්න.
"ජනතාව සහ ඉඟි" කොටස සංවර්ධනය කිරීම
ඉඟි මුදල තේරීම්කාරකයක් එක් කිරීමට, පහත කේතය නව රේඛීය පිරිසැලසුම් කොටසකට අලවන්න (#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>
ඉඟි මුදල නිවැරදිව ගණනය කිරීමට මෙම කේත කොටස අවශ්ය වේ. පෙරනිමි පෙළ අගය 20 වේ. ImageButtons සඳහා ලිවීමේ අවසර ඇති ෆෝල්ඩරයක අයිකන සපයා ඇත.
සම්පූර්ණ කොටස පිටපත් කර පහත සඳහන් (#5) එකතු කරන්න:
- ImageButton ids (අඩු කරන්නPeopleButton, addPeopleButton)
- TextView ids(numberOfPeopleStaticText, numberOfPeopleTextView)
- numberOfPeopleTextView සඳහා DefaultText (4 විය යුතුය).
දැන්, යෙදුම ආරම්භ කරන විට, ඉන්වොයිස් මුදල එකතු කළ හැකිය, එකතු කරන්න / අඩු කරන්න බොත්තම් ද ක්රියා කරයි, නමුත් මෙතෙක් කිසිවක් සිදු නොවේ.
දර්ශන එකතු කරන්න
MainActivity.kt විවෘත කර මෙය 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
}
බොත්තම් අවසන් කිරීම
බොත්තම ක්ලික් කිරීම් සඳහා සහය එක් කිරීමට, පන්ති මට්ටමින් View.OnClickListener ක්රියාත්මක කරන්න (#7):
පන්තියේ MainActivity: AppCompatActivity(), View.OnClickListener {
දැන් ව්යාපෘතිය සම්පාදනය කිරීම ක්රියා නොකරනු ඇත, ඔබට තවත් පියවර කිහිපයක් සිදු කිරීමට අවශ්ය වේ (# 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()
}
}
බොත්තම් සහ ස්විච අනුව, Kotlin සෑම දෙයක්ම ඉතා සිසිල් ලෙස සංවිධානය කර ඇත! සියලුම වර්ධක සහ අඩු කිරීමේ කාර්යයන් සඳහා පහත කේතය එක් කරන්න
(#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()
}
}
මෙහිදී කේතය උපරිම අගයන් (MAX_TIP සහ MAX_PEOPLE) සමඟ වර්ධක ශ්රිත ආරක්ෂා කරයි. ඊට අමතරව, කේතය අවම අගයන් (MIN_TIP සහ MIN_PEOPLE) සමඟ අඩු කිරීමේ කාර්යයන් ආරක්ෂා කරයි.
දැන් අපි initViews ශ්රිතයේ (#13) අසන්නන්ට බොත්තම් බඳිමු:
private fun initViews() {
...
addTipButton.setOnClickListener(this)
subtractTipButton.setOnClickListener(this)
addPeopleButton.setOnClickListener(this)
subtractPeopleButton.setOnClickListener(this)
//TODO #15: Bind EditText to TextWatcher
}
ඔබට දැන් සම්පූර්ණ හානිය, ඉඟි සහ රැස්වීමට සහභාගිවන්නන් සංඛ්යාව එක් කළ හැක. හරි දැන් වැදගත්ම දේ...
පිරිවැය අංශය
මෙම කේතය පිරිවැය ගණනය කරයි (#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)
}
හොඳයි, මෙහි කාර්යයක් හඳුන්වනු ලබන අතර එමඟින් සමාගමේ පුද්ගලයින් සංඛ්යාව සැලකිල්ලට ගෙන ඉඟිය ගණනය කිරීමට හැකි වේ (#15):
private fun incrementTip() {
…
}
private fun decrementTip() {
…
}
private fun incrementPeople() {
…
}
private fun decrementPeople() {
…
}
අපි යෙදුම දියත් කරමු. එය විශිෂ්ට ලෙස පෙනෙන අතර ක්රියා කරයි. නමුත් එය වඩා හොඳ විය හැකිය.
ඔබ බිල් මුදල ඉවත් කර ඉඟි හෝ මිතුරන් ගණන වැඩි කිරීමට උත්සාහ කළහොත්, තවමත් ශුන්ය පිරිවැය සඳහා චෙක්පතක් නොමැති නිසා යෙදුම බිඳ වැටේ. එපමණක් නොව, ඔබ ඉන්වොයිස් මුදල වෙනස් කිරීමට උත්සාහ කරන්නේ නම්, ගාස්තු යාවත්කාලීන නොවේ.
අවසාන පියවර
TextWatcher එක් කරන්න (#16):
පන්තියේ ප්රධාන ක්රියාකාරකම: AppCompatActivity(), View.OnClickListener, TextWatcher {
ඉන්පසු අපි billEditText සවන්දෙන්නා (#17):
billEditText.addTextChangedListener(මෙය)
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) {}
හොඳයි, දැන් සියල්ල ක්රියා කරයි! සුභ පැතුම්, ඔබ ඔබේම "ටිපින් කැල්කියුලේටරය" කොට්ලින් හි ලියා ඇත.
Skillbox නිර්දේශ කරයි:
- වසර දෙකක ප්රායෝගික පාඨමාලාව
"මම PRO වෙබ් සංවර්ධකයෙක්" .- මාර්ගගත පාඨමාලාව
"C# සංවර්ධක" .- ප්රායෝගික වසර පාඨමාලාව
"0 සිට PRO දක්වා PHP සංවර්ධකයා" .
මූලාශ්රය: www.habr.com