<?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:background="@color/colorPrimary" android:orientation="vertical"> <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="20dp" android:layout_height="wrap_content" android:text="输入第一个数:" android:textSize="30sp" 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="15dp"> <TextView android:id="@+id/text2" android:layout_width="20dp" android:layout_height="wrap_content" android:text="输入第二个数:" android:textSize="30sp" 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/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:onClick="test"/> </RelativeLayout> </LinearLayout>
package com.example.work; 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 text; EditText l1; EditText l2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text= 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; l1 = findViewById(R.id.edt1); l2 = findViewById(R.id.edt2); t1 = l1.getText().toString(); t2 = l2.getText().toString(); float num1 = Float.parseFloat(t1); float num2 = Float.parseFloat(t2); if (c1.isChecked()) { float he = num1 + num2; text.setText("输出结果为"+he); } if (c2.isChecked()) { float cha = num1 - num2; text.setText("输出结果为"+cha); } if (c3.isChecked()) { float ji = num1 * num2; text.setText("输出结果为"+ji); } if (c4.isChecked()) { float shang = num1 / num2; text.setText( "输出结果为"+shang); } } }); } public void test(View view) { l1.setText(""); l2.setText(""); text.setText(""); Toast.makeText(MainActivity.this, "已清除", Toast.LENGTH_SHORT).show(); } }