<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="计算器"
android:textColor="#FF2196F3"
android:textSize="25sp"
android:textStyle="italic" />
<LinearLayout
android:id="@+id/l1_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tv1"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:background="#FFFFFF">
<TextView
android:id="@+id/tv_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="数值1:"
android:textColor="#000"
android:textSize="20sp" />
<EditText
android:id="@+id/et1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv1" />
</LinearLayout>
<LinearLayout
android:id="@+id/l2_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/l1_number"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:background="#FFFFFF">
<TextView
android:id="@+id/tv_number2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="数值2:"
android:textColor="#000"
android:textSize="20sp" />
<EditText
android:id="@+id/et2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv2" />
</LinearLayout>
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/l2_number"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="50dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="50dp"
android:gravity="center"
android:text="显示计算结果"
android:textSize="20sp"
android:textStyle="italic" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv2"
android:gravity="center"
android:onClick="click"
android:text="+"
android:textColor="#FFF44336"
android:textSize="25sp"
android:textStyle="italic" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv2"
android:layout_toRightOf="@id/btn1"
android:gravity="center"
android:onClick="click"
android:text="-"
android:textColor="#FFE91E63"
android:textSize="25sp"
android:textStyle="italic" />
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv2"
android:layout_toRightOf="@id/btn2"
android:gravity="center"
android:onClick="click"
android:text="*"
android:textColor="#FFF79E38"
android:textSize="25sp"
android:textStyle="italic" />
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv2"
android:layout_toRightOf="@id/btn3"
android:gravity="center"
android:onClick="click"
android:text="/"
android:textColor="#FFF79E38"
android:textSize="25sp"
android:textStyle="italic" />
</RelativeLayout>
package com.example.calculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View v) {
TextView tv2 = (TextView) findViewById(R.id.tv2);
EditText et1 = (EditText) findViewById(R.id.et1);
EditText et2 = (EditText) findViewById(R.id.et2);
double num1,num2,result=0;
num1=Integer.parseInt(et1.getText().toString());
num2=Integer.parseInt(et2.getText().toString());
switch (v.getId()) {
case R.id.btn1:
result=num1+num2;
break;
case R.id.btn2:
result=num1-num2;
break;
case R.id.btn3:
result=num1*num2;
break;
case R.id.btn4:
result=num1/num2;
break;
}
tv2.setText(result+"");
}
}
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<RelativeLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
>
<TextView
android:id=
"@+id/tv1"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_centerHorizontal=
"true"
android:gravity=
"center"
android:text=
"计算器"
android:textColor=
"#FF2196F3"
android:textSize=
"25sp"
android:textStyle=
"italic"
/>
<LinearLayout
android:id=
"@+id/l1_number"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:layout_below=
"@+id/tv1"
android:layout_centerVertical=
"true"
android:layout_marginLeft=
"10dp"
android:layout_marginTop=
"15dp"
android:layout_marginRight=
"10dp"
android:layout_marginBottom=
"5dp"
android:background=
"#FFFFFF"
>
<TextView
android:id=
"@+id/tv_number"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:padding=
"10dp"
android:text=
"数值1:"
android:textColor=
"#000"
android:textSize=
"20sp"
/>
<EditText
android:id=
"@+id/et1"
android:layout_width=
"fill_parent"
android:layout_height=
"wrap_content"
android:layout_below=
"@id/tv1"
/>
</LinearLayout>
<LinearLayout
android:id=
"@+id/l2_number"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:layout_below=
"@+id/l1_number"
android:layout_centerVertical=
"true"
android:layout_marginLeft=
"10dp"
android:layout_marginTop=
"15dp"
android:layout_marginRight=
"10dp"
android:layout_marginBottom=
"5dp"
android:background=
"#FFFFFF"
>
<TextView
android:id=
"@+id/tv_number2"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:padding=
"10dp"
android:text=
"数值2:"
android:textColor=
"#000"
android:textSize=
"20sp"
/>
<EditText
android:id=
"@+id/et2"
android:layout_width=
"fill_parent"
android:layout_height=
"wrap_content"
android:layout_below=
"@id/tv2"
/>
</LinearLayout>
<TextView
android:id=
"@+id/tv2"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_below=
"@id/l2_number"
android:layout_centerHorizontal=
"true"
android:layout_marginLeft=
"10dp"
android:layout_marginTop=
"50dp"
android:layout_marginRight=
"10dp"
android:layout_marginBottom=
"50dp"
android:gravity=
"center"
android:text=
"显示计算结果"
android:textSize=
"20sp"
android:textStyle=
"italic"
/>
<Button
android:id=
"@+id/btn1"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_below=
"@id/tv2"
android:gravity=
"center"
android:onClick=
"click"
android:text=
"+"
android:textColor=
"#FFF44336"
android:textSize=
"25sp"
android:textStyle=
"italic"
/>
<Button
android:id=
"@+id/btn2"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_below=
"@id/tv2"
android:layout_toRightOf=
"@id/btn1"
android:gravity=
"center"
android:onClick=
"click"
android:text=
"-"
android:textColor=
"#FFE91E63"
android:textSize=
"25sp"
android:textStyle=
"italic"
/>
<Button
android:id=
"@+id/btn3"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_below=
"@id/tv2"
android:layout_toRightOf=
"@id/btn2"
android:gravity=
"center"
android:onClick=
"click"
android:text=
"*"
android:textColor=
"#FFF79E38"
android:textSize=
"25sp"
android:textStyle=
"italic"
/>
<Button
android:id=
"@+id/btn4"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_below=
"@id/tv2"
android:layout_toRightOf=
"@id/btn3"
android:gravity=
"center"
android:onClick=
"click"
android:text=
"/"
android:textColor=
"#FFF79E38"
android:textSize=
"25sp"
android:textStyle=
"italic"
/>
</RelativeLayout>