我们将向您展示如何创建一个简单的Kotlin提示计算器应用程序。 更具体地说,Kotlin 1.3.21,Android 4和Android Studio3。首先,对于那些开始开发Android应用程序的人来说,这篇文章会很有趣。 它使您能够了解它在应用程序中的作用以及工作方式。
当您需要计算决定将时间花在餐馆或咖啡馆的公司的小费金额时,这种计算器非常有用。 当然,并不是所有的,也不总是让侍者喝茶,这更多是西方的传统,但是在任何情况下开发这种应用程序的过程都很有趣。
我们提醒您: 对于所有“哈勃”读者来说,使用“哈勃”促销代码注册任何Skillbox课程时均可享受10,000卢布的折扣。
Skillbox建议:实用课程“ Mobile Developer PRO” 。
这是应用程序在过程中的外观:

您输入所需的总金额百分比,会议参加人数并获得结果-应该留下的提示数量。
开始使用
完整的应用程序界面如下:


第一步是
下载项目基础知识 。 在Android Studio 3.0或更高版本中打开它。 我们构建并启动该项目,然后看到一个白色屏幕。 一切都很好,应该是这样。


用户动作在项目中按时间顺序列出,以便所有内容都清晰易懂。 要查看它,请打开查看->工具窗口-> TODO。
我们研究该项目并打开colors.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"/>
现在,您可以使用
material.io工具自定义values目录样式或使用颜色进行播放。
现在项目看起来像这样:
如您所见,成本的计算是根据用户生成的数据进行的。帐户科发展
在费用部分(#2)之后,在LinearLayout中添加以下代码:
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@color/colorAccent"> <! — TODO #3: Build Bill Section → … </LinearLayout>
在TODO列表之后关闭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值。
我们开始测试项目,然后输入总损坏的参数(杯子,盘子破损等)开发“人员和技巧”部分
要添加吸头体积选择,请在新的LinearLayout部分(#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。ImageButton在文件夹中提供了具有写权限的图标。
我们完全复制该部分并添加以下内容(#5):
- ImageButton ID(subtractPeopleButton,addPeopleButton)
- TextView ID(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)
完成按钮
为了添加按钮单击支持,我们实现了类级别的View.OnClickListener(#7):
class 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)

现在,您可以添加总损失,提示和会议参与者的数量。 好吧,现在最重要的是...
成本核算科
此代码计算成本(#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):
class MainActivity : AppCompatActivity(), View.OnClickListener, TextWatcher {
然后,我们嵌入billEditText侦听器(#17):
billEditText.addTextChangedListener(this)
另外,添加代码以执行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) {}

好吧,现在绝对一切正常! 恭喜,您在Kotlin上编写了自己的小费计算器。

Skillbox建议: