package com.example.myapplication4; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.RadioGroup; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { public RadioGroup radioGroup; EditText one1; EditText two1; TextView jg; Integer d1; Integer d2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); radioGroup=findViewById(R.id.rg); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, int i) { switch (i){ case R.id.jia:jia();break; case R.id.jian:jian();break; case R.id.cheng:cheng();break; case R.id.chu:chu();break; } } }); } private void chu(){ jg=findViewById(R.id.tv1); two1=findViewById(R.id.two); String on=one1.getText().toString(); String tw=two1.getText().toString(); d1=Integer.valueOf(on); d2=Integer.valueOf(tw); if (d2==0||d1==0) { Toast.makeText(MainActivity.this, "除数不能为0", Toast.LENGTH_SHORT).show(); }else { String st = Integer.toString(d1 / d2); jg.setText(st); } } public void cheng(){ jg=findViewById(R.id.tv1); one1=findViewById(R.id.one); two1=findViewById(R.id.two); String on=one1.getText().toString(); String tw=two1.getText().toString(); d1=Integer.valueOf(on); d2=Integer.valueOf(tw); String st=Integer.toString(d1*d2); jg.setText(st); } public void jia() { jg = findViewById(R.id.tv1); one1 = findViewById(R.id.one); two1 = findViewById(R.id.two); String on = one1.getText().toString(); String tw = two1.getText().toString(); d1 = Integer.valueOf(on); d2 = Integer.valueOf(tw); String st = Integer.toString(d1 + d2); jg.setText(st); } public void jian(){ jg=findViewById(R.id.tv1); one1=findViewById(R.id.one); two1=findViewById(R.id.two); String on=one1.getText().toString(); String tw=two1.getText().toString(); d1=Integer.valueOf(on); d2=Integer.valueOf(tw); String st=Integer.toString(d1-d2); jg.setText(st); } public void qkz(View view){ jg.setText(""); one1.setText(""); two1.setText(""); } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" android:background="#777777" tools:context=".MainActivity"> <LinearLayout android:id="@+id/line1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:background="#FFFFFF" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="输入第一个数" android:textColor="#1B1B1B" /> <EditText android:id="@+id/one" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="第一个数" android:textColorHint="#1CB9B3"> </EditText> </LinearLayout> <LinearLayout android:id="@+id/line2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="150dp" android:background="#FFFFFF" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="输入第二个数" android:textColor="#1B1B1B" /> <EditText android:id="@+id/two" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="第二个数" android:textColorHint="#1CB9B3" /> </LinearLayout> <RadioGroup android:id="@+id/rg" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_below="@id/line2" android:layout_marginTop="80dp" > <RadioButton android:id="@+id/jia" android:text="+" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <RadioButton android:id="@+id/jian" android:text="-" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <RadioButton android:id="@+id/cheng" android:text="*" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <RadioButton android:id="@+id/chu" android:layout_width="124dp" android:layout_height="wrap_content" android:text="/" /> </RadioGroup> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="400dp" android:background="#FFFFFF" android:orientation="horizontal"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginRight="20dp" android:layout_marginLeft="20dp" android:text="清除" android:onClick="qkz" android:textSize="25dp"/> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:id="@+id/tv1" > </TextView> </RelativeLayout>