主程序:MainActivity.java
package com.example.calcdemo;
import java.text.DecimalFormat;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.*;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.*;
public class MainActivity extends Activity implements OnClickListener,
OnTouchListener {
private EditText editText;
private Button[] btn1 = new Button[10];
private Button[] btn2 = new Button[8];
private int data1[] = { R.id.btn0, R.id.btn1, R.id.btn2, R.id.btn3,
R.id.btn4, R.id.btn5, R.id.btn6, R.id.btn7, R.id.btn8, R.id.btn9 };
private int data2[] = { R.id.btnChu, R.id.btnDian, R.id.btnJia,
R.id.btnJian, R.id.btnRes, R.id.btnSheng, R.id.ce, R.id.backspace };
private StringBuffer s = new StringBuffer();
private String s1 = null;
private String s2 = null;
private String temp = null;
private String split = null;
private Double sum1;
private Double sum2;
protected void onCreate(Bundle save) {
super.onCreate(save);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.edit_text);
initArray();
}
private void getRes() {
DecimalFormat dt = (DecimalFormat) DecimalFormat.getInstance(); dt.applyPattern("0.0000");
Double sum = null;
String t1 = temp;
String t = s.toString();
if (t.length() != 0
&& (t.contains("+") || t.contains("-") || t.contains("*") || t
.contains("/"))) {
if (temp.equals("/") || temp.equals("*") || temp.equals("+")) {
temp = "\\" + t1;
}
String ss[] = t.split(temp);
temp = t1;
sum1 = Double.parseDouble(ss[0]);
sum2 = Double.parseDouble(ss[1]);
if (temp.equals("/"))
sum = sum1 / sum2;
else if (temp.equals("*"))
sum = sum1 * sum2;
else if (temp.equals("+"))
sum = sum1 + sum2;
else if (temp.equals("-"))
sum = sum1 - sum2;
if (!(t1.contains("/") || t1.contains(".")))
dt.applyPattern("0");
editText.setText(dt.format(sum).toString());
s = new StringBuffer();
} else
editText.setText(t);
}
// 初始化Button数组
private void initArray() {
int len1 = data1.length;
int len2 = data2.length;
for (int i = 0; i < len1; i++) {
btn1[i] = (Button) findViewById(data1[i]);
btn1[i].setOnClickListener(this);
btn1[i].setOnTouchListener(this);
}
for (int i = 0; i < len2; i++) {
btn2[i] = (Button) findViewById(data2[i]);
btn2[i].setOnClickListener(this);
btn2[i].setOnTouchListener(this);
}
}
@Override
public void onClick(View v) {
int Id = v.getId();
// v.setBackgroundColor(Color.CYAN);
switch (Id) {
case R.id.btn0:
s.append(btn1[0].getText());
editText.setText(s);
break;
case R.id.btn1:
s.append(btn1[1].getText());
editText.setText(s);
break;
case R.id.btn2:
s.append(btn1[2].getText());
editText.setText(s);
break;
case R.id.btn3:
s.append(btn1[3].getText());
editText.setText(s);
break;
case R.id.btn4:
s.append(btn1[4].getText());
editText.setText(s);
break;
case R.id.btn5:
s.append(btn1[5].getText());
editText.setText(s);
break;
case R.id.btn6:
s.append(btn1[6].getText());
editText.setText(s);
break;
case R.id.btn7:
s.append(btn1[7].getText());
editText.setText(s);
break;
case R.id.btn8:
s.append(btn1[8].getText());
editText.setText(s);
break;
case R.id.btn9:
s.append(btn1[9].getText());
editText.setText(s);
break;
case R.id.backspace:
try {
s = new StringBuffer(s.substring(0, s.length() - 1));
editText.setText(s);
} catch (Exception e) {
editText.setText("");
}
break;
case R.id.btnChu:
temp = "/";
s.append(btn2[0].getText());
editText.setText(s);
break;
case R.id.btnDian:
s.append(btn2[1].getText());
editText.setText(s);
break;
case R.id.btnJia:
temp = "+";
s.append(btn2[2].getText());
editText.setText(s);
break;
case R.id.btnJian:
temp = "-";
s.append(btn2[3].getText());
editText.setText(s);
break;
case R.id.btnRes:
getRes();
break;
case R.id.btnSheng:
temp = "*";
s.append(btn2[5].getText());
editText.setText(s);
break;
case R.id.ce:
editText.setText("");
s = new StringBuffer();
break;
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// v.setBackgroundColor(Color.YELLOW);
return false;
}
布局文件: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:layout_gravity="center" android:background="#404040" android:orientation="vertical" >
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" >
<EditText android:id="@+id/edit_text" android:layout_width="match_parent" android:layout_height="70dp" android:background="@android:drawable/alert_light_frame" android:focusable="false" android:gravity="right" android:maxLines="1" android:textSize="30sp" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#404040" android:orientation="horizontal" >
<Button android:id="@+id/backspace" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Backspace" />
<Button android:id="@+id/ce" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="CE" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" >
<Button android:id="@+id/btn7" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="7" />
<Button android:id="@+id/btn8" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="8" />
<Button android:id="@+id/btn9" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="9" />
<Button android:id="@+id/btnChu" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="/" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" >
<Button android:id="@+id/btn4" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="4" />
<Button android:id="@+id/btn5" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="5" />
<Button android:id="@+id/btn6" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="6" />
<Button android:id="@+id/btnSheng" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="*" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" >
<Button android:id="@+id/btn1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="1" />
<Button android:id="@+id/btn2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="2" />
<Button android:id="@+id/btn3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="3" />
<Button android:id="@+id/btnJian" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="-" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" >
<Button android:id="@+id/btn0" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:text="0" />
<Button android:id="@+id/btnDian" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="." />
<Button android:id="@+id/btnJia" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="+" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" >
<Button android:id="@+id/btnRes" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="=" />
</LinearLayout>
</LinearLayout>