package com.example.chapter03;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.example.chapter03.R;
public class jisuanqi extends AppCompatActivity {
private EditText num1;
private EditText num2;
private EditText mathlabel;
private EditText num3;
private Button btn;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.active_jisuanqi);
num1=findViewById(R.id.numberEdittext1);
num2=findViewById(R.id.numberEdittext2);
mathlabel=findViewById(R.id.numberEdittext3);
num3=findViewById(R.id.numterEdittext4);
btn=findViewById(R.id.button);
btn.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
Double numthree=null;
double numone=0;
double numtwo=0;
String mathlabelone=null;
try {
numone=Double.parseDouble(num1.getText().toString());
numtwo=Double.parseDouble(num2.getText().toString());
mathlabelone=mathlabel.getText().toString();
}catch (NumberFormatException e){
num3.setText(R.string.hello);
return true;
}
if (mathlabelone.equals("+")){
numthree=numone+numtwo;
}
else if (mathlabelone.equals("-")){
numthree=numone-numtwo;
}
else if (mathlabelone.equals("*")){
numthree=numone*numtwo;
}
else if (mathlabelone.equals("/")){
numthree=numone/numtwo;
}
num3.setText(numthree.toString());
return true;
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity2">
<TextView
android:id="@+id/textView"
android:layout_width="66dp"
android:layout_height="33dp"
android:layout_marginStart="60dp"
android:layout_marginTop="84dp"
android:text="计算数1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/numberEdittext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginTop="72dp"
android:ems="10"
android:inputType="textPersonName"
android:text=""
app:layout_constraintStart_toEndOf="@+id/textView"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="60dp"
android:layout_marginTop="32dp"
android:text="计算数2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<EditText
android:id="@+id/numberEdittext2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="56dp"
android:layout_marginTop="20dp"
android:ems="10"
android:inputType="textPersonName"
android:text=""
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toBottomOf="@+id/numberEdittext1" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="104dp"
android:text="运算符"
app:layout_constraintTop_toBottomOf="@+id/textView"
tools:layout_editor_absoluteX="58dp" />
<EditText
android:id="@+id/numberEdittext3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="64dp"
android:layout_marginTop="88dp"
android:ems="10"
android:inputType="textPersonName"
android:text=""
app:layout_constraintStart_toEndOf="@+id/textView3"
app:layout_constraintTop_toBottomOf="@+id/numberEdittext1" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="161dp"
android:layout_marginTop="200dp"
android:text="计算"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/numberEdittext1" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="60dp"
android:layout_marginTop="312dp"
android:text="结果"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<EditText
android:id="@+id/numterEdittext4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="72dp"
android:layout_marginTop="292dp"
android:ems="10"
android:inputType="textPersonName"
android:minHeight="48dp"
android:text=""
app:layout_constraintStart_toEndOf="@+id/textView4"
app:layout_constraintTop_toBottomOf="@+id/numberEdittext1" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity2">
<TextView
android:id="@+id/textView"
android:layout_width="66dp"
android:layout_height="33dp"
android:layout_marginStart="60dp"
android:layout_marginTop="84dp"
android:text="计算数1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/numberEdittext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginTop="72dp"
android:ems="10"
android:inputType="textPersonName"
android:text=""
app:layout_constraintStart_toEndOf="@+id/textView"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="60dp"
android:layout_marginTop="32dp"
android:text="计算数2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<EditText
android:id="@+id/numberEdittext2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="56dp"
android:layout_marginTop="20dp"
android:ems="10"
android:inputType="textPersonName"
android:text=""
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toBottomOf="@+id/numberEdittext1" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="104dp"
android:text="运算符"
app:layout_constraintTop_toBottomOf="@+id/textView"
tools:layout_editor_absoluteX="58dp" />
<EditText
android:id="@+id/numberEdittext3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="64dp"
android:layout_marginTop="88dp"
android:ems="10"
android:inputType="textPersonName"
android:text=""
app:layout_constraintStart_toEndOf="@+id/textView3"
app:layout_constraintTop_toBottomOf="@+id/numberEdittext1" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="161dp"
android:layout_marginTop="200dp"
android:text="计算"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/numberEdittext1" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="60dp"
android:layout_marginTop="312dp"
android:text="结果"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<EditText
android:id="@+id/numterEdittext4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="72dp"
android:layout_marginTop="292dp"
android:ems="10"
android:inputType="textPersonName"
android:minHeight="48dp"
android:text=""
app:layout_constraintStart_toEndOf="@+id/textView4"
app:layout_constraintTop_toBottomOf="@+id/numberEdittext1" />
</androidx.constraintlayout.widget.ConstraintLayout>