使用kotlin写的android简单计算器demo,适合初入门的朋友参考使用,
完整项目代码地址:https://github.com/linwenbing/KotlinCounterDemo
1.项目代码:
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.View.OnClickListener
import android.widget.Button
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity(), OnClickListener {
private var firstNumber = ""//第一个数字
private var nextNumber = ""//第二个数字
private var flag = ""//计算方式
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btn_c.setOnClickListener(this)
btn_sign.setOnClickListener(this)
btn_seven.setOnClickListener(this)
btn_eight.setOnClickListener(this)
btn_nine.setOnClickListener(this)
btn_five.setOnClickListener(this)
btn_four.setOnClickListener(this)
btn_six.setOnClickListener(this)
btn_three.setOnClickListener(this)
btn_two.setOnClickListener(this)
btn_one.setOnClickListener(this)
btn_zreo.setOnClickListener(this)
btn_multiplication.setOnClickListener(this)
btn_subtract.setOnClickListener(this)
btn_addition.setOnClickListener(this)
btn_equal.setOnClickListener(this)
}
override fun onClick(v: View?) {
doClick("" + (v as Button).text)
}
private fun doClick(value:String){
when(value){
"+","-","*","÷"->{
if (firstNumber?.isNotEmpty() && nextNumber?.isEmpty()){
flag = value
}else if(firstNumber?.isNotEmpty() && nextNumber?.isNotEmpty()){
flag = value
doCount()
}
}
"=" ->{
if(firstNumber?.isNotEmpty() && nextNumber?.isNotEmpty()){
doCount()
flag = ""
}
}
"C" ->{
firstNumber = ""
nextNumber = ""
tv_show.text = ""
flag = ""
}
else ->{
if (flag?.isNotEmpty()){
if (nextNumber?.isEmpty() && value?.equals("0")){
Toast.makeText(this,"除数不能为0",Toast.LENGTH_SHORT).show()
}else{
nextNumber += value
tv_show.text = nextNumber
}
}else{
firstNumber += value
tv_show.text = firstNumber
}
}
}
}
/**
* 计算方法
*/
private fun doCount(){
var result = 0.0
when(flag){
"+" -> result = firstNumber.toDouble()+nextNumber.toDouble()
"-" -> result =firstNumber.toDouble()-nextNumber.toDouble()
"*" -> result =firstNumber.toDouble()*nextNumber.toDouble()
"÷" -> {
result = firstNumber.toDouble()/nextNumber.toDouble()
}
}
firstNumber = result.toString()
nextNumber = ""
tv_show.text = result.toString()
}
}
2.布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv_show"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="50dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/btn_c"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="C"
/>
<Button
android:id="@+id/btn_sign"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="÷"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/btn_seven"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7"
/>
<Button
android:id="@+id/btn_eight"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="8"
/>
<Button
android:id="@+id/btn_nine"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="9"
/>
<Button
android:id="@+id/btn_multiplication"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="*"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/btn_four"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="4"
/>
<Button
android:id="@+id/btn_five"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5"
/>
<Button
android:id="@+id/btn_six"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6"
/>
<Button
android:id="@+id/btn_subtract"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/btn_one"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1"
/>
<Button
android:id="@+id/btn_two"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2"
/>
<Button
android:id="@+id/btn_three"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3"
/>
<Button
android:id="@+id/btn_addition"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/btn_zreo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0"
/>
<Button
android:id="@+id/btn_equal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="="
/>
</LinearLayout>
</LinearLayout>