样式如下图:
1、布局文件:
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"
tools:context=".MainActivity">
android:id="@+id/jieguo"
android:layout_width="match_parent"
android:layout_height="128dp"
android:textSize="30sp"
android:textColor="@android:color/holo_green_dark"
app:layout_constraintBottom_toBottomOf="@id/ac"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="17dp"
tools:layout_editor_absoluteY="71dp" />
android:id="@+id/ac"
android:layout_width="match_parent"
android:layout_height="51dp"
android:text="c"
app:layout_constraintBottom_toTopOf="@id/btn1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/jieguo"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="270dp" />
android:id="@+id/btn1"
android:layout_width="92dp"
android:layout_height="50dp"
android:text="1"
app:layout_constraintBottom_toTopOf="@id/btn4"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/btn2"
app:layout_constraintTop_toBottomOf="@id/ac"
tools:layout_editor_absoluteY="394dp" />
android:id="@+id/btn2"
android:layout_width="92dp"
android:layout_height="50dp"
android:text="2"
app:layout_constraintBaseline_toBaselineOf="@+id/btn1"
app:layout_constraintLeft_toRightOf="@+id/btn1"
app:layout_constraintRight_toLeftOf="@+id/btn3"
app:layout_constraintTop_toBottomOf="@id/ac"
app:layout_constraintBottom_toTopOf="@+id/btn5"
tools:layout_editor_absoluteX="116dp"
tools:layout_editor_absoluteY="394dp" />
android:id="@+id/btn3"
android:layout_width="92dp"
android:layout_height="50dp"
android:text="3"
app:layout_constraintBaseline_toBaselineOf="@+id/btn2"
app:layout_constraintLeft_toRightOf="@+id/btn2"
app:layout_constraintRight_toLeftOf="@+id/jiafa"
app:layout_constraintTop_toBottomOf="@id/ac"
app:layout_constraintBottom_toTopOf="@+id/btn6"
tools:layout_editor_absoluteX="227dp"
tools:layout_editor_absoluteY="389dp" />
android:id="@+id/jiafa"
android:layout_width="92dp"
android:layout_height="50dp"
android:text="+"
app:layout_constraintBaseline_toBaselineOf="@+id/btn3"
app:layout_constraintLeft_toRightOf="@+id/btn3"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/ac"
app:layout_constraintBottom_toTopOf="@+id/jianfa"
tools:layout_editor_absoluteX="338dp"
tools:layout_editor_absoluteY="389dp" />
android:id="@+id/btn4"
android:layout_width="92dp"
android:layout_height="50dp"
android:text="4"
app:layout_constraintBottom_toTopOf="@id/btn7"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/btn5"
app:layout_constraintTop_toBottomOf="@id/btn1"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="508dp" />
android:id="@+id/btn5"
android:layout_width="92dp"
android:layout_height="50dp"
android:text="5"
app:layout_constraintBaseline_toBaselineOf="@+id/btn4"
app:layout_constraintLeft_toRightOf="@+id/btn4"
app:layout_constraintRight_toLeftOf="@+id/btn6"
app:layout_constraintTop_toBottomOf="@id/btn2"
app:layout_constraintBottom_toTopOf="@+id/btn8"
tools:layout_editor_absoluteX="120dp"
tools:layout_editor_absoluteY="506dp" />
android:id="@+id/btn6"
android:layout_width="92dp"
android:layout_height="50dp"
android:text="6"
app:layout_constraintBaseline_toBaselineOf="@+id/btn5"
app:layout_constraintLeft_toRightOf="@+id/btn5"
app:layout_constraintRight_toLeftOf="@+id/jianfa"
app:layout_constraintTop_toBottomOf="@id/btn3"
app:layout_constraintBottom_toTopOf="@+id/btn9"
/>
android:id="@+id/jianfa"
android:layout_width="92dp"
android:layout_height="50dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="-"
app:layout_constraintBaseline_toBaselineOf="@+id/btn6"
app:layout_constraintBottom_toTopOf="@+id/chengfa"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toRightOf="@+id/btn6"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/jiafa" />
android:id="@+id/btn7"
android:layout_width="92dp"
android:layout_height="50dp"
android:text="7"
app:layout_constraintBottom_toTopOf="@id/deng"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/btn8"
app:layout_constraintTop_toBottomOf="@id/btn4"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="602dp" />
android:id="@+id/btn8"
android:layout_width="92dp"
android:layout_height="50dp"
android:text="8"
app:layout_constraintBaseline_toBaselineOf="@+id/btn7"
app:layout_constraintLeft_toRightOf="@id/btn7"
app:layout_constraintRight_toLeftOf="@id/btn9"
app:layout_constraintTop_toBottomOf="@id/btn5"
app:layout_constraintBottom_toTopOf="@+id/btn0"
tools:layout_editor_absoluteX="121dp"
tools:layout_editor_absoluteY="602dp" />
android:id="@+id/btn9"
android:layout_width="92dp"
android:layout_height="50dp"
android:text="9"
app:layout_constraintBaseline_toBaselineOf="@+id/btn8"
app:layout_constraintLeft_toRightOf="@id/btn8"
app:layout_constraintRight_toLeftOf="@id/chengfa"
app:layout_constraintTop_toBottomOf="@id/btn6"
app:layout_constraintBottom_toTopOf="@+id/xiaoshudian"
tools:layout_editor_absoluteX="222dp"
tools:layout_editor_absoluteY="602dp" />
android:id="@+id/chengfa"
android:layout_width="92dp"
android:layout_height="50dp"
android:text="*"
app:layout_constraintBaseline_toBaselineOf="@+id/btn9"
app:layout_constraintLeft_toRightOf="@id/btn9"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/jianfa"
app:layout_constraintBottom_toTopOf="@+id/chufa"
tools:layout_editor_absoluteX="338dp"
tools:layout_editor_absoluteY="602dp" />
android:id="@+id/deng"
android:layout_width="92dp"
android:layout_height="50dp"
android:text="="
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/btn0"
app:layout_constraintTop_toBottomOf="@id/btn7"
tools:layout_editor_absoluteX="14dp"
tools:layout_editor_absoluteY="666dp" />
android:id="@+id/btn0"
android:layout_width="92dp"
android:layout_height="50dp"
android:text="0"
app:layout_constraintBaseline_toBaselineOf="@+id/deng"
app:layout_constraintLeft_toRightOf="@id/deng"
app:layout_constraintRight_toLeftOf="@id/xiaoshudian"
app:layout_constraintTop_toBottomOf="@id/btn8"
app:layout_constraintBottom_toBottomOf="parent"
tools:layout_editor_absoluteX="117dp"
tools:layout_editor_absoluteY="664dp" />
android:id="@+id/xiaoshudian"
android:layout_width="92dp"
android:layout_height="50dp"
android:text="."
app:layout_constraintBaseline_toBaselineOf="@+id/btn0"
app:layout_constraintLeft_toRightOf="@id/btn0"
app:layout_constraintRight_toLeftOf="@id/chufa"
app:layout_constraintTop_toBottomOf="@id/btn9"
app:layout_constraintBottom_toBottomOf="parent"
tools:layout_editor_absoluteX="223dp"
tools:layout_editor_absoluteY="667dp" />
android:id="@+id/chufa"
android:layout_width="92dp"
android:layout_height="50dp"
android:text="/"
app:layout_constraintBaseline_toBaselineOf="@+id/xiaoshudian"
app:layout_constraintLeft_toRightOf="@id/xiaoshudian"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/chengfa"
app:layout_constraintBottom_toBottomOf="parent"
tools:layout_editor_absoluteX="338dp"
tools:layout_editor_absoluteY="664dp" />
2、MainActivity.kt文件
class MainActivity : AppCompatActivity(), View.OnClickListener {
var firstNum=""
var secondNum=""
var op="" //操作符号
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btn0.setOnClickListener (this)
btn1.setOnClickListener(this)
btn2.setOnClickListener(this)
btn3.setOnClickListener(this)
btn4.setOnClickListener(this)
btn5.setOnClickListener(this)
btn6.setOnClickListener(this)
btn7.setOnClickListener(this)
btn8.setOnClickListener(this)
btn9.setOnClickListener(this)
xiaoshudian.setOnClickListener(this)//小数点
ac.setOnClickListener(this)//清除
jiafa.setOnClickListener(this)//加法
jianfa.setOnClickListener(this)//减法
chengfa.setOnClickListener(this)//乘法
chufa.setOnClickListener(this)//除法
deng.setOnClickListener (this)//等于
jieguo.setOnClickListener(this)//打印计算结果
}
override fun onClick(v: View?) {
doClick(""+(vas Button).text)
}
private fun doClick(value:String){
when(value){
"+","-","*","/"->{
if (firstNum?.isNotEmpty()&&secondNum?.isEmpty()){
op=value
}else if(firstNum?.isNotEmpty()&&secondNum?.isNotEmpty()){
op=value
dojisuan()
}
}
"="->{
if(firstNum?.isNotEmpty() &&secondNum?.isNotEmpty()){
dojisuan()
op=""
}
}
"c"->{
firstNum =""
secondNum =""
op =""
jieguo.text =""
}
else->{
/* println(op?.isNotEmpty())*/
if (op?.isNotEmpty()) {
if (op.equals("/")&&secondNum?.isEmpty() && value?.equals("0")) {
Toast.makeText(this, "输入错误", Toast.LENGTH_SHORT).show()
}else {
secondNum += value
jieguo.text=secondNum
}
}else {
firstNum += value
jieguo.text=firstNum
}
}
}
}
fun dojisuan(){
var result=0.0
when(op){
"+"-> result=firstNum.toDouble() +secondNum.toDouble()
"-"-> result=firstNum.toDouble() -secondNum.toDouble()
"*"-> result=firstNum.toDouble() *secondNum.toDouble()
"/"-> result=firstNum.toDouble() /secondNum.toDouble()
}
jieguo.text = result.toString()
firstNum=result.toString()
secondNum=""
}
}