<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
tools:context=".MainActivity"
android:orientation="vertical"
android:background="@drawable/p30">
<LinearLayout
android:layout_marginTop="55dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/text1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="输入第一个数:"
android:textSize="25sp"
android:layout_weight="1.25"
android:gravity="center"/>
<EditText
android:id="@+id/edt1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textSize="25sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<TextView
android:id="@+id/text2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="输入第二个数:"
android:textSize="25sp"
android:layout_weight="1.25"
android:gravity="center"/>
<EditText
android:id="@+id/edt2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="25sp"
android:gravity="center"/>
</LinearLayout>
<RadioGroup
android:id="@+id/rgp1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="45dp"
android:layout_marginLeft="35dp"
android:layout_marginRight="35dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rbn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:textSize="32sp"
android:layout_weight="1"
android:checked="false"/>
<RadioButton
android:id="@+id/rbn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:textSize="32sp"
android:layout_weight="1"
android:checked="false"/>
<RadioButton
android:id="@+id/rbn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*"
android:textSize="32sp"
android:layout_weight="1"
android:checked="false"/>
<RadioButton
android:id="@+id/rbn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
android:textSize="32sp"
android:layout_weight="1"
android:checked="false"/>
</RadioGroup>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp">
<TextView
android:id="@+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="计算结果为"
android:textSize="25sp"/>
<TextView
android:id="@+id/text4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_centerHorizontal="true"
android:layout_below="@+id/text3"
android:textSize="25sp"
android:textColor="#333fff"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:text="归零"
android:textSize="25sp"
android:background="#333fff"
android:onClick="test"/>
</RelativeLayout>
</LinearLayout>
package com.example.jsq;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
TextView text4;
EditText e1;
EditText e2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text4 = findViewById(R.id.text4);
final RadioButton c1 = findViewById(R.id.rbn1);
final RadioButton c2 = findViewById(R.id.rbn2);
final RadioButton c3 = findViewById(R.id.rbn3);
final RadioButton c4 = findViewById(R.id.rbn4);
RadioGroup rgp = (RadioGroup) findViewById(R.id.rgp1);
rgp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
final String t1;
final String t2;
e1 = findViewById(R.id.edt1);
e2 = findViewById(R.id.edt2);
t1 = e1.getText().toString();
t2 = e2.getText().toString();
float num1 = Float.parseFloat(t1);
float num2 = Float.parseFloat(t2);
if (c1.isChecked()) {
float plus = num1 + num2;
text4.setText(plus + "");
}
if (c2.isChecked()) {
float subtract = num1 - num2;
text4.setText(subtract + "");
}
if (c3.isChecked()) {
float ride = num1 * num2;
text4.setText(ride + "");
}
if (c4.isChecked()) {
float division = num1 / num2;
text4.setText(division + "");
}
}
});
}
public void test(View view) {
e1.setText("");
e2.setText("");
text4.setText("");
Toast.makeText(MainActivity.this, "已清空", Toast.LENGTH_SHORT).show();
}
}