<img src="https://img-blog.csdn.net/20140628140140500?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxNDAwMTcxNw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);" />
在package cn.bzu.edu.computer包里:
package cn.bzu.edu.computer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>private EditText etNum1;
<span style="white-space:pre"> </span>private EditText etNum2;
<span style="white-space:pre"> </span>private TextView tvOp;
<span style="white-space:pre"> </span>private TextView tvResult;
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>protected void onCreate(Bundle savedInstanceState) {
<span style="white-space:pre"> </span>super.onCreate(savedInstanceState);
<span style="white-space:pre"> </span>setContentView(R.layout.activity_main);
<span style="white-space:pre"> </span>initViews();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>private void initViews() {
<span style="white-space:pre"> </span>etNum1=(EditText)this.findViewById(R.id.etNum1);
<span style="white-space:pre"> </span>etNum2=(EditText)this.findViewById(R.id.etNum2);
<span style="white-space:pre"> </span>tvOp=(TextView)this.findViewById(R.id.tvOp);
<span style="white-space:pre"> </span>tvResult=(TextView)this.findViewById(R.id.tvResult);
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public boolean onCreateOptionsMenu(Menu menu) {
<span style="white-space:pre"> </span>// Inflate the menu; this adds items to the action bar if it is present.
<span style="white-space:pre"> </span>getMenuInflater().inflate(R.menu.main, menu);
<span style="white-space:pre"> </span>return true;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>@Override
/*<span style="white-space:pre"> </span>public boolean onOptionsItemSelected(MenuItem item) {
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>switch (item.getItemId()) {
<span style="white-space:pre"> </span>case R.id.about:
<span style="white-space:pre"> </span>Toast.makeText(this, "about", Toast.LENGTH_SHORT).show();
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case R.id.exit:
<span style="white-space:pre"> </span>Toast.makeText(this, "exit", Toast.LENGTH_SHORT).show();
<span style="white-space:pre"> </span>default:
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>return super.onOptionsItemSelected(item);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>};*/
<span style="white-space:pre"> </span>public boolean onOptionsItemSelected(MenuItem item) {
<span style="white-space:pre"> </span>switch (item.getItemId()) {
<span style="white-space:pre"> </span>case R.id.about:
<span style="white-space:pre"> </span>Toast.makeText(this, "about", Toast.LENGTH_SHORT).show();
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case R.id.exit:
<span style="white-space:pre"> </span>Toast.makeText(this, "exit", Toast.LENGTH_SHORT).show();
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>default:
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>return super.onOptionsItemSelected(item);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public void add(View view){
<span style="white-space:pre"> </span>String strNum1=etNum1.getText().toString();
<span style="white-space:pre"> </span>String strNum2=etNum2.getText().toString();
<span style="white-space:pre"> </span>double num1=Double.parseDouble(strNum1);
<span style="white-space:pre"> </span>double num2=Double.parseDouble(strNum2);
<span style="white-space:pre"> </span>tvOp.setText(" + ");
<span style="white-space:pre"> </span>tvResult.setText(""+cn.bzu.edu.computer.model.Calculator.add(num1,num2));
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>public void sub(View view){
<span style="white-space:pre"> </span>String strNum1=etNum1.getText().toString();
<span style="white-space:pre"> </span>String strNum2=etNum2.getText().toString();
<span style="white-space:pre"> </span>double num1=Double.parseDouble(strNum1);
<span style="white-space:pre"> </span>double num2=Double.parseDouble(strNum2);
<span style="white-space:pre"> </span>tvOp.setText(" - ");
<span style="white-space:pre"> </span>tvResult.setText(""+cn.bzu.edu.computer.model.Calculator.sub(num1,num2));
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public void multiply(View view){
<span style="white-space:pre"> </span>String strNum1=etNum1.getText().toString();
<span style="white-space:pre"> </span>String strNum2=etNum2.getText().toString();
<span style="white-space:pre"> </span>double num1=Double.parseDouble(strNum1);
<span style="white-space:pre"> </span>double num2=Double.parseDouble(strNum2);
<span style="white-space:pre"> </span>tvOp.setText(" * ");
<span style="white-space:pre"> </span>tvResult.setText(""+cn.bzu.edu.computer.model.Calculator.multiply(num1,num2));
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>public void divide (View view){
<span style="white-space:pre"> </span>String strNum1=etNum1.getText().toString();
<span style="white-space:pre"> </span>String strNum2=etNum2.getText().toString();
<span style="white-space:pre"> </span>double num1=Double.parseDouble(strNum1);
<span style="white-space:pre"> </span>double num2=Double.parseDouble(strNum2);
<span style="white-space:pre"> </span>tvOp.setText(" % ");
<span style="white-space:pre"> </span>tvResult.setText(""+cn.bzu.edu.computer.model.Calculator.divide(num1,num2));
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
在<span style="font-family: Arial, Helvetica, sans-serif;">package cn.bzu.edu.computer.model包里写:</span>
package cn.bzu.edu.computer.model;
public class Calculator {
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>public static double add ( double num1,double num2){<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>return num1+num2;<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public static double sub ( double num1,double num2){<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>return num1-num2;<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public static double multiply ( double num1,double num2){<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>return num1*num2;<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public static double divide ( double num1,double num2){<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>if(num2==0)
<span style="white-space:pre"> </span>throw new ArithmeticException("除数不能为0");
<span style="white-space:pre"> </span>else
<span style="white-space:pre"> </span>return num1%num2;<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
}
<span style="white-space:pre"> </span>
}
在layout布局文件中写:
<TableLayout 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:background="#ffcc00"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TableRow>
<EditText
android:id="@+id/etNum1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:drawable/editbox_background"
android:inputType="number|phone" />
<EditText
android:id="@+id/tvOp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number|phone" />
<EditText
android:id="@+id/etNum2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:drawable/editbox_background"
android:inputType="number|phone" />
<EditText
android:id="@+id/e"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number|phone"
android:text="@string/dengyu"
android:textSize="15pt" />
<EditText
android:id="@+id/tvResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
android:inputType="number|phone"
/>
</TableRow>
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
android:onClick="add"
android:text="@string/add" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
android:onClick="sub"
android:text="@string/sub" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
android:onClick="multiply"
android:text="@string/multiply" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
android:onClick="divide"
android:text="@string/divide" />
</TableRow>
</TableLayout>