当前位置:   article > 正文

使用kotlin写的android简单计算器demo_android studio使用kotlin语言实现计算器

android studio使用kotlin语言实现计算器

使用kotlin写的android简单计算器demo,适合初入门的朋友参考使用,

完整项目代码地址:https://github.com/linwenbing/KotlinCounterDemo

1.项目代码:

  1. import android.support.v7.app.AppCompatActivity
  2. import android.os.Bundle
  3. import android.util.Log
  4. import android.view.View
  5. import android.view.View.OnClickListener
  6. import android.widget.Button
  7. import android.widget.Toast
  8. import kotlinx.android.synthetic.main.activity_main.*
  9. class MainActivity : AppCompatActivity(), OnClickListener {
  10. private var firstNumber = ""//第一个数字
  11. private var nextNumber = ""//第二个数字
  12. private var flag = ""//计算方式
  13. override fun onCreate(savedInstanceState: Bundle?) {
  14. super.onCreate(savedInstanceState)
  15. setContentView(R.layout.activity_main)
  16. btn_c.setOnClickListener(this)
  17. btn_sign.setOnClickListener(this)
  18. btn_seven.setOnClickListener(this)
  19. btn_eight.setOnClickListener(this)
  20. btn_nine.setOnClickListener(this)
  21. btn_five.setOnClickListener(this)
  22. btn_four.setOnClickListener(this)
  23. btn_six.setOnClickListener(this)
  24. btn_three.setOnClickListener(this)
  25. btn_two.setOnClickListener(this)
  26. btn_one.setOnClickListener(this)
  27. btn_zreo.setOnClickListener(this)
  28. btn_multiplication.setOnClickListener(this)
  29. btn_subtract.setOnClickListener(this)
  30. btn_addition.setOnClickListener(this)
  31. btn_equal.setOnClickListener(this)
  32. }
  33. override fun onClick(v: View?) {
  34. doClick("" + (v as Button).text)
  35. }
  36. private fun doClick(value:String){
  37. when(value){
  38. "+","-","*","÷"->{
  39. if (firstNumber?.isNotEmpty() && nextNumber?.isEmpty()){
  40. flag = value
  41. }else if(firstNumber?.isNotEmpty() && nextNumber?.isNotEmpty()){
  42. flag = value
  43. doCount()
  44. }
  45. }
  46. "=" ->{
  47. if(firstNumber?.isNotEmpty() && nextNumber?.isNotEmpty()){
  48. doCount()
  49. flag = ""
  50. }
  51. }
  52. "C" ->{
  53. firstNumber = ""
  54. nextNumber = ""
  55. tv_show.text = ""
  56. flag = ""
  57. }
  58. else ->{
  59. if (flag?.isNotEmpty()){
  60. if (nextNumber?.isEmpty() && value?.equals("0")){
  61. Toast.makeText(this,"除数不能为0",Toast.LENGTH_SHORT).show()
  62. }else{
  63. nextNumber += value
  64. tv_show.text = nextNumber
  65. }
  66. }else{
  67. firstNumber += value
  68. tv_show.text = firstNumber
  69. }
  70. }
  71. }
  72. }
  73. /**
  74. * 计算方法
  75. */
  76. private fun doCount(){
  77. var result = 0.0
  78. when(flag){
  79. "+" -> result = firstNumber.toDouble()+nextNumber.toDouble()
  80. "-" -> result =firstNumber.toDouble()-nextNumber.toDouble()
  81. "*" -> result =firstNumber.toDouble()*nextNumber.toDouble()
  82. "÷" -> {
  83. result = firstNumber.toDouble()/nextNumber.toDouble()
  84. }
  85. }
  86. firstNumber = result.toString()
  87. nextNumber = ""
  88. tv_show.text = result.toString()
  89. }
  90. }

2.布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:context=".MainActivity">
  9. <TextView
  10. android:id="@+id/tv_show"
  11. android:layout_width="match_parent"
  12. android:layout_height="50dp"
  13. android:layout_marginTop="50dp"
  14. />
  15. <LinearLayout
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content"
  18. android:orientation="horizontal"
  19. >
  20. <Button
  21. android:id="@+id/btn_c"
  22. android:layout_width="0dp"
  23. android:layout_height="wrap_content"
  24. android:layout_weight="3"
  25. android:text="C"
  26. />
  27. <Button
  28. android:id="@+id/btn_sign"
  29. android:layout_width="0dp"
  30. android:layout_height="wrap_content"
  31. android:layout_weight="1"
  32. android:text="÷"
  33. />
  34. </LinearLayout>
  35. <LinearLayout
  36. android:layout_width="match_parent"
  37. android:layout_height="wrap_content"
  38. android:orientation="horizontal"
  39. >
  40. <Button
  41. android:id="@+id/btn_seven"
  42. android:layout_width="0dp"
  43. android:layout_height="wrap_content"
  44. android:layout_weight="1"
  45. android:text="7"
  46. />
  47. <Button
  48. android:id="@+id/btn_eight"
  49. android:layout_width="0dp"
  50. android:layout_height="wrap_content"
  51. android:layout_weight="1"
  52. android:text="8"
  53. />
  54. <Button
  55. android:id="@+id/btn_nine"
  56. android:layout_width="0dp"
  57. android:layout_height="wrap_content"
  58. android:layout_weight="1"
  59. android:text="9"
  60. />
  61. <Button
  62. android:id="@+id/btn_multiplication"
  63. android:layout_width="0dp"
  64. android:layout_height="wrap_content"
  65. android:layout_weight="1"
  66. android:text="*"
  67. />
  68. </LinearLayout>
  69. <LinearLayout
  70. android:layout_width="match_parent"
  71. android:layout_height="wrap_content"
  72. android:orientation="horizontal"
  73. >
  74. <Button
  75. android:id="@+id/btn_four"
  76. android:layout_width="0dp"
  77. android:layout_height="wrap_content"
  78. android:layout_weight="1"
  79. android:text="4"
  80. />
  81. <Button
  82. android:id="@+id/btn_five"
  83. android:layout_width="0dp"
  84. android:layout_height="wrap_content"
  85. android:layout_weight="1"
  86. android:text="5"
  87. />
  88. <Button
  89. android:id="@+id/btn_six"
  90. android:layout_width="0dp"
  91. android:layout_height="wrap_content"
  92. android:layout_weight="1"
  93. android:text="6"
  94. />
  95. <Button
  96. android:id="@+id/btn_subtract"
  97. android:layout_width="0dp"
  98. android:layout_height="wrap_content"
  99. android:layout_weight="1"
  100. android:text="-"
  101. />
  102. </LinearLayout>
  103. <LinearLayout
  104. android:layout_width="match_parent"
  105. android:layout_height="wrap_content"
  106. android:orientation="horizontal"
  107. >
  108. <Button
  109. android:id="@+id/btn_one"
  110. android:layout_width="0dp"
  111. android:layout_height="wrap_content"
  112. android:layout_weight="1"
  113. android:text="1"
  114. />
  115. <Button
  116. android:id="@+id/btn_two"
  117. android:layout_width="0dp"
  118. android:layout_height="wrap_content"
  119. android:layout_weight="1"
  120. android:text="2"
  121. />
  122. <Button
  123. android:id="@+id/btn_three"
  124. android:layout_width="0dp"
  125. android:layout_height="wrap_content"
  126. android:layout_weight="1"
  127. android:text="3"
  128. />
  129. <Button
  130. android:id="@+id/btn_addition"
  131. android:layout_width="0dp"
  132. android:layout_height="wrap_content"
  133. android:layout_weight="1"
  134. android:text="+"
  135. />
  136. </LinearLayout>
  137. <LinearLayout
  138. android:layout_width="match_parent"
  139. android:layout_height="wrap_content"
  140. android:orientation="horizontal"
  141. >
  142. <Button
  143. android:id="@+id/btn_zreo"
  144. android:layout_width="0dp"
  145. android:layout_height="wrap_content"
  146. android:layout_weight="1"
  147. android:text="0"
  148. />
  149. <Button
  150. android:id="@+id/btn_equal"
  151. android:layout_width="0dp"
  152. android:layout_height="wrap_content"
  153. android:layout_weight="1"
  154. android:text="="
  155. />
  156. </LinearLayout>
  157. </LinearLayout>

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/999940
推荐阅读
相关标签
  

闽ICP备14008679号