布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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"> //加载背景图片 <ImageView android:layout_width="wrap_content" android:layout_height="match_parent" android:background="@drawable/jsq" /> <Button android:id="@+id/bt1" android:text="+" android:textSize="25dp" android:textColor="#ffffff" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="200dp" android:layout_marginRight="20dp" android:onClick="click_bt1" tools:ignore="OnClick"> </Button> <Button android:id="@+id/bt2" android:text="-" android:textSize="25dp" android:textColor="#ffffff" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/bt1" android:layout_alignTop="@id/bt1" android:layout_marginRight="20dp" android:onClick="click_bt2" /> <Button android:id="@+id/bt3" android:text="×" android:textSize="25dp" android:textColor="#ffffff" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/bt2" android:layout_alignTop="@id/bt2" android:layout_marginRight="20dp" android:onClick="click_bt3" tools:ignore="OnClick" /> <Button android:id="@+id/bt4" android:text="÷" android:textSize="25dp" android:textColor="#ffffff" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/bt3" android:layout_alignTop="@id/bt3" android:onClick="click_bt4" tools:ignore="OnClick" /> <EditText android:id="@+id/et1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="输入第一个数" android:layout_centerHorizontal="true" android:layout_marginTop="44dp" /> <EditText android:id="@+id/et2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="输入第一个数" android:layout_centerHorizontal="true" android:layout_marginTop="110dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="结果:" android:textSize="35dp" android:layout_marginTop="280dp" /> <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:textSize="35dp" android:layout_marginLeft="120dp" android:layout_marginTop="280dp" /> </RelativeLayout>
实现
package com.example.myapplication; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import org.w3c.dom.Text; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button bt1; private Button bt2; private Button bt3; private Button bt4; private EditText et1, et2; private TextView tv1; private double d1, d2, sum = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt1 = (Button) findViewById(R.id.bt1); bt2 =(Button) findViewById(R.id.bt1); bt3 = (Button) findViewById(R.id.bt1); bt4 = (Button) findViewById(R.id.bt1); et1 =(EditText) findViewById(R.id.et1); et2 =(EditText) findViewById(R.id.et2); tv1 = (TextView) findViewById(R.id.tv1); bt1.setOnClickListener(this); bt2.setOnClickListener(this); bt3.setOnClickListener(this); bt4.setOnClickListener(this); } @Override public void onClick(View view) { d1=Integer.parseInt(et1.getText().toString()); d2=Integer.parseInt(et2.getText().toString()); sum=d1+d2; String q = String.valueOf(sum); tv1.setText(d1+"+"+d2+"="+q); } public void click_bt2(View view) { d1=Integer.parseInt(et1.getText().toString()); d2=Integer.parseInt(et2.getText().toString()); sum=d1-d2; String q = String.valueOf(sum); tv1.setText(d1+"—"+d2+"="+q); } public void click_bt3(View view) { d1=Integer.parseInt(et1.getText().toString()); d2=Integer.parseInt(et2.getText().toString()); sum=d1*d2; String q = String.valueOf(sum); tv1.setText(d1+"×"+d2+"="+q); } public void click_bt4(View view) { d1=Integer.parseInt(et1.getText().toString()); d2=Integer.parseInt(et2.getText().toString()); sum=d1/d2; String q = String.valueOf(sum); tv1.setText(d1+"÷"+d2+"="+q); } }