MainActivity.java
package com.example.calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener {
private EditText et;
private Button bt_clear;
private Button bt_del;
private Button bt_mul;
private Button bt_div;
private Button bt_sub;
private Button bt_add;
private Button bt_point;
private Button bt_equal;
private Button bt_1;
private Button bt_2;
private Button bt_3;
private Button bt_4;
private Button bt_5;
private Button bt_6;
private Button bt_7;
private Button bt_8;
private Button bt_9;
private Button bt_0;
private Boolean clearFlag = false;// 清空标示
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
// 初始化控件
bt_0 = (Button) findViewById(R.id.bt_0);
bt_1 = (Button) findViewById(R.id.bt_1);
bt_2 = (Button) findViewById(R.id.bt_2);
bt_3 = (Button) findViewById(R.id.bt_3);
bt_4 = (Button) findViewById(R.id.bt_4);
bt_5 = (Button) findViewById(R.id.bt_5);
bt_6 = (Button) findViewById(R.id.bt_6);
bt_7 = (Button) findViewById(R.id.bt_7);
bt_8 = (Button) findViewById(R.id.bt_8);
bt_9 = (Button) findViewById(R.id.bt_9);
bt_clear = (Button) findViewById(R.id.bt_clear);
bt_del = (Button) findViewById(R.id.bt_del);
bt_mul = (Button) findViewById(R.id.bt_mul);
bt_div = (Button) findViewById(R.id.bt_div);
bt_sub = (Button) findViewById(R.id.bt_sub);
bt_add = (Button) findViewById(R.id.bt_add);
bt_point = (Button) findViewById(R.id.bt_point);
bt_equal = (Button) findViewById(R.id.bt_equal);
et = (EditText) findViewById(R.id.edit_input1);
// 通过接口实现监听
bt_0.setOnClickListener(this);
bt_1.setOnClickListener(this);
bt_2.setOnClickListener(this);
bt_3.setOnClickListener(this);
bt_4.setOnClickListener(this);
bt_5.setOnClickListener(this);
bt_6.setOnClickListener(this);
bt_7.setOnClickListener(this);
bt_8.setOnClickListener(this);
bt_9.setOnClickListener(this);
bt_point.setOnClickListener(this);
bt_add.setOnClickListener(this);
bt_sub.setOnClickListener(this);
bt_mul.setOnClickListener(this);
bt_div.setOnClickListener(this);
bt_equal.setOnClickListener(this);
bt_clear.setOnClickListener(this);
bt_del.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String str = et.getText().toString();
switch (v.getId()) {
case R.id.bt_0:
case R.id.bt_1:
case R.id.bt_2:
case R.id.bt_3:
case R.id.bt_4:
case R.id.bt_5:
case R.id.bt_6:
case R.id.bt_7:
case R.id.bt_8:
case R.id.bt_9:
case R.id.bt_point:
// 如果已经按过“=”,则清屏
if (clearFlag) {
et.setText("");
clearFlag = false;
}
// 输入框中添加数字、小数点
et.setText(str + ((Button) v).getText());
break;
case R.id.bt_add:
case R.id.bt_sub:
case R.id.bt_mul:
case R.id.bt_div:
// 如果已经按过“=”,则清屏
if (clearFlag) {
et.setText("");
clearFlag = false;
}
// 输入框中添加运算符,并在运算符前后添加空格
et.setText(str + " " + ((Button) v).getText() + " ");
break;
case R.id.bt_clear:
// 清空输入框
et.setText("");
break;
case R.id.bt_del:
// 输入框存在内容
if (str != null && !str.equals("")) {
et.setText(str.substring(0, str.length() - 1));
}
break;
case R.id.bt_equal:
getResult();
break;
default:
break;
}
}
/*
* 运算结果
*/
private void getResult() {
String exp = et.getText().toString();
// 如果输入为空
if (exp == null || exp.equals("")) {
return;
}
// 如果不存在空格
if (!exp.contains(" ")) {
return;
}
// 如果已按过等于号
if (clearFlag) {
clearFlag = false;
return;
}
clearFlag = true;
double result = 0;
String s1 = exp.substring(0, exp.indexOf(" "));// 获取运算符前的数字
String s2 = exp.substring(exp.indexOf(" ") + 3);// 获取运算符后的数字
String op = exp.substring(exp.indexOf(" ") + 1, exp.indexOf(" ") + 2);// 获取运算符
// 如果运算符前的数字都不为空
if (!s1.equals("") && !s2.equals("") && !s2.equals(" +")
&& !s2.equals(" -") && !s2.equals(" *") && !s2.equals(" /")) {
// 强制转换为double类型的数字
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
if (op.equals("+")) {
result = d1 + d2;
} else if (op.equals("-")) {
result = d1 - d2;
} else if (op.equals("*")) {
result = d1 * d2;
} else if (op.equals("/")) {
if (d2 == 0) {
result = 0;
}
result = d1 / d2;
}
// 如果数字中不含有小数点或者不是除法
if (!s1.contains(".") && !s2.contains(".") && !op.equals("/")) {
int i = (int) result;
et.setText(i + "");
} else {
et.setText(result + "");
}
}
// 如果输入的第一个数字为空
else if (s1.equals("") && !s2.equals("")) {
et.setText(exp);
}
// 如果输入的第二个数字为空
else if (!s1.equals("") && s2.equals("")) {
double d1 = Double.parseDouble(s1);
if (op.equals("+")) {
result = d1;
} else if (op.equals("-")) {
result = 0 - d1;
} else if (op.equals("*")) {
result = 0;
} else if (op.equals("/")) {
result = 0;
}
// 如果数字中不含有小数点
if (!s2.contains(".")) {
int i = (int) result;
et.setText(i + "");
} else {
et.setText(result + "");
}
}
// 如果输入的两个数字为空
else if (s1.equals("") && s2.equals("")) {
et.setText("");
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
android:orientation="vertical" >
<EditText
android:id="@+id/edit_input1"
android:layout_width="309dp"
android:layout_height="60dp"
android:textSize="30sp"
android:background="@drawable/bg_white"
android:editable="false"
android:gravity="right|bottom" >
</EditText>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="@+id/bt_clear"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@drawable/whtie_selector"
android:gravity="bottom|center_horizontal"
android:text="@string/bt_clear"
android:textSize="25sp" >
</Button>
<Button
android:id="@+id/bt_del"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="15dp"
android:background="@drawable/whtie_selector"
android:gravity="bottom|center_horizontal"
android:text="@string/bt_del"
android:textSize="25sp" >
</Button>
<Button
android:id="@+id/bt_mul"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="15dp"
android:background="@drawable/whtie_selector"
android:gravity="bottom|center_horizontal"
android:text="@string/bt_mul"
android:textSize="25sp" >
</Button>
<Button
android:id="@+id/bt_div"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="15dp"
android:background="@drawable/whtie_selector"
android:gravity="bottom|center_horizontal"
android:text="@string/bt_divide"
android:textSize="25sp" >
</Button>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="@+id/bt_7"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@drawable/whtie_selector"
android:gravity="bottom|center_horizontal"
android:text="@string/number7"
android:textSize="25sp" >
</Button>
<Button
android:id="@+id/bt_8"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="15dp"
android:background="@drawable/whtie_selector"
android:gravity="bottom|center_horizontal"
android:text="@string/number8"
android:textSize="25sp" >
</Button>
<Button
android:id="@+id/bt_9"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="15dp"
android:background="@drawable/whtie_selector"
android:gravity="bottom|center_horizontal"
android:text="@string/number9"
android:textSize="25sp" >
</Button>
<Button
android:id="@+id/bt_sub"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="15dp"
android:background="@drawable/whtie_selector"
android:gravity="bottom|center_horizontal"
android:text="@string/bt_sub"
android:textSize="25sp" >
</Button>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="@+id/bt_4"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@drawable/whtie_selector"
android:gravity="bottom|center_horizontal"
android:text="@string/number4"
android:textSize="25sp" >
</Button>
<Button
android:id="@+id/bt_5"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="15dp"
android:background="@drawable/whtie_selector"
android:gravity="bottom|center_horizontal"
android:text="@string/number5"
android:textSize="25sp" >
</Button>
<Button
android:id="@+id/bt_6"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="15dp"
android:background="@drawable/whtie_selector"
android:gravity="bottom|center_horizontal"
android:text="@string/number6"
android:textSize="25sp" >
</Button>
<Button
android:id="@+id/bt_add"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="15dp"
android:background="@drawable/whtie_selector"
android:gravity="bottom|center_horizontal"
android:text="@string/bt_add"
android:textSize="25sp" >
</Button>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="@+id/bt_1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="15dp"
android:background="@drawable/whtie_selector"
android:gravity="bottom|center_horizontal"
android:text="@string/number1"
android:textSize="25sp" >
</Button>
<Button
android:id="@+id/bt_2"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="15dp"
android:background="@drawable/whtie_selector"
android:gravity="bottom|center_horizontal"
android:text="@string/number2"
android:textSize="25sp" >
</Button>
<Button
android:id="@+id/bt_3"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="15dp"
android:background="@drawable/whtie_selector"
android:gravity="bottom|center_horizontal"
android:text="@string/number3"
android:textSize="25sp" >
</Button>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="@+id/bt_0"
android:layout_width="130dp"
android:layout_height="60dp"
android:layout_marginLeft="15dp"
android:background="@drawable/whtie_selector"
android:gravity="bottom|center_horizontal"
android:text="@string/number0"
android:textSize="25sp" >
</Button>
<Button
android:id="@+id/bt_point"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="15dp"
android:background="@drawable/whtie_selector"
android:gravity="bottom|center_horizontal"
android:text="@string/bt_point"
android:textSize="25sp" >
</Button>
</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/bt_equal"
android:layout_width="60dp"
android:layout_height="130dp"
android:layout_marginLeft="15dp"
android:background="@drawable/whtie_selector"
android:gravity="bottom|center_horizontal"
android:text="@string/bt_equal"
android:textSize="25sp" />
</LinearLayout>
</LinearLayout>
注:还有两个小问题待以后学习深入后解决:
1、除法如8/8后显示1.0;
2、运算“=”后,没有清屏;