package com.example.c;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button bt1,bt2,bt3,bt4,bt5,bt6,bt7,bt8,bt9,bt0,btjia,btjian,btcheng,btchu,btCencel,btdeng;
TextView textView;
int operate;
double num1 =0,num2 =0,result =0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.tv);
bt0 = findViewById(R.id.bt0);
bt1 = findViewById(R.id.bt1);
bt2 = findViewById(R.id.bt2);
bt3 = findViewById(R.id.bt3);
bt4 = findViewById(R.id.bt4);
bt5 = findViewById(R.id.bt5);
bt6 = findViewById(R.id.bt6);
bt7 = findViewById(R.id.bt7);
bt8 = findViewById(R.id.bt8);
bt9 = findViewById(R.id.bt9);
btjia = findViewById(R.id.btjia);
btjian= findViewById(R.id.btjian);
btcheng = findViewById(R.id.btcheng);
btchu = findViewById(R.id.btchu);
btCencel = findViewById(R.id.btCencel);
btdeng = findViewById(R.id.btdeng);
bt0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String sbt0 = textView.getText().toString();
sbt0+="0";
textView.setText(sbt0);
}
});
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String sbt1 = textView.getText().toString();
sbt1+="1";
textView.setText(sbt1);
}
});
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String sbt2 = textView.getText().toString();
sbt2+="2";
textView.setText(sbt2);
}
});
bt3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String sbt3 = textView.getText().toString();
sbt3+="3";
textView.setText(sbt3);
}
});
bt4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String sbt4 = textView.getText().toString();
sbt4+="4";
textView.setText(sbt4);
}
});
bt5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String sbt5 = textView.getText().toString();
sbt5+="5";
textView.setText(sbt5);
}
});
bt6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String sbt6 = textView.getText().toString();
sbt6+="6";
textView.setText(sbt6);
}
});
bt7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String sbt7 = textView.getText().toString();
sbt7+="7";
textView.setText(sbt7);
}
});
bt8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String sbt8 = textView.getText().toString();
sbt8+="8";
textView.setText(sbt8);
}
});
bt9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String sbt9 = textView.getText().toString();
sbt9+="9";
textView.setText(sbt9);
}
});
btjia.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String sbjia = textView.getText().toString();
if (sbjia.equals(null)) {
return;
}
num1 = Double.valueOf(sbjia);
textView.setText(null);
operate = 1;
}
});
btjian.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String sbjian = textView.getText().toString();
if (sbjian.equals(null)) {
return;
}
num1 = Double.valueOf(sbjian);
textView.setText(null);
operate = 2;
}
});
btcheng.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String sbcheng = textView.getText().toString();
if (sbcheng.equals(null)) {
return;
}
num1 = Double.valueOf(sbcheng);
textView.setText(null);
operate = 3;
}
});
btchu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String sbchu = textView.getText().toString();
if (sbchu.equals(null)) {
return;
}
num1 = Double.valueOf(sbchu);
textView.setText(null);
operate = 4;
}
});
btdeng.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String btdeng = textView.getText().toString();
num2 = Double.valueOf(btdeng);
textView.setText(null);
switch (operate){
case 1:
result = num1+num2;
textView.setText(String.valueOf(num1)+"+"+String.valueOf(num2)+"="+String.valueOf(result));
break;
case 2:
result = num1-num2;
textView.setText(String.valueOf(num1)+"-"+String.valueOf(num2)+"="+String.valueOf(result));
break;
case 3:
result = num1*num2;
textView.setText(String.valueOf(num1)+"*"+String.valueOf(num2)+"="+String.valueOf(result));
break;
case 4:
result = num1/num2;
textView.setText(String.valueOf(num1)+"÷"+String.valueOf(num2)+"="+String.valueOf(result));
break;
}
}
});
btCencel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
textView.setText(null);
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<Button
android:id="@+id/bt1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="200dp"
android:text="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/bt2"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:layout_marginTop="200dp"
android:text="2"
android:textAllCaps="false"
app:layout_constraintStart_toEndOf="@+id/bt1"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/bt3"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:layout_marginTop="200dp"
android:text="3"
app:layout_constraintStart_toEndOf="@+id/bt2"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/bt4"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:layout_marginTop="200dp"
android:text="4"
app:layout_constraintStart_toEndOf="@+id/bt3"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/bt5"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:layout_marginTop="200dp"
android:text="5"
app:layout_constraintStart_toEndOf="@+id/bt4"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btjia"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:layout_marginTop="200dp"
android:text="+"
app:layout_constraintStart_toEndOf="@+id/bt5"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btjian"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:layout_marginTop="200dp"
android:text="-"
app:layout_constraintStart_toEndOf="@+id/btjia"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/bt6"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="2dp"
android:text="6"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/bt1" />
<Button
android:id="@+id/bt7"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:text="7"
app:layout_constraintStart_toEndOf="@+id/bt6"
app:layout_constraintTop_toBottomOf="@+id/bt2" />
<Button
android:id="@+id/bt8"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:text="8"
app:layout_constraintStart_toEndOf="@+id/bt7"
app:layout_constraintTop_toBottomOf="@+id/bt2" />
<Button
android:id="@+id/bt9"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:text="9"
app:layout_constraintStart_toEndOf="@+id/bt8"
app:layout_constraintTop_toBottomOf="@+id/bt2" />
<Button
android:id="@+id/bt0"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:text="0"
app:layout_constraintStart_toEndOf="@+id/bt9"
app:layout_constraintTop_toBottomOf="@+id/bt2" />
<Button
android:id="@+id/btcheng"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:text="*"
app:layout_constraintStart_toEndOf="@+id/bt0"
app:layout_constraintTop_toBottomOf="@+id/bt2" />
<Button
android:id="@+id/btchu"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:text="÷"
app:layout_constraintStart_toEndOf="@+id/btcheng"
app:layout_constraintTop_toBottomOf="@+id/bt2" />
<TextView
android:id="@+id/tv"
android:layout_width="300dp"
android:layout_height="100dp"
android:layout_marginStart="52dp"
android:layout_marginTop="36dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text=""
android:textColor="#2196F3"
android:textSize="20dp"
android:textStyle="bold"
android:gravity="right"
android:background="@drawable/tvborder"
/>
<Button
android:id="@+id/btCencel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"
tools:layout_editor_absoluteX="23dp"
tools:layout_editor_absoluteY="306dp" />
<Button
android:id="@+id/btdeng"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="179dp"
android:layout_marginTop="8dp"
android:text="="
app:layout_constraintStart_toEndOf="@+id/btCencel"
app:layout_constraintTop_toBottomOf="@+id/btcheng" />
</androidx.constraintlayout.widget.ConstraintLayout>