package com.example.android_layout;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
private Button one, two, three, four, five, six, seven, eight, nine, point,
zero, equal, plus, minus, multiply, except, del, clear;
private EditText editText;
private Double resultDouble = 0.0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText = (EditText) findViewById(R.id.msg);
editText.setCursorVisible(false);
one = (Button) findViewById(R.id.btnone);
one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String str = editText.getText().toString();
String eq = resultDouble.toString();
if (eq.substring(eq.indexOf(".") + 1).equals("0")) {
eq = eq.substring(0, eq.indexOf("."));
}
if (str.equals("0") || str.equals(eq) || str.equals("Infinity") || str.equals("Infinity")) {
str = "1";
} else {
str = str + "1";
}
editText.setText(str);
}
});
two = (Button) findViewById(R.id.btntwo);
two.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
String str = editText.getText().toString();
String eq = resultDouble.toString();
if (eq.substring(eq.indexOf(".") + 1).equals("0")) {
eq = eq.substring(0, eq.indexOf("."));
}
if (str.equals("0") || str.equals(eq) || str.equals("Infinity")) {
str = "2";
} else {
str = str + "2";
}
editText.setText(str);
}
});
three = (Button) findViewById(R.id.btnthree);
three.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String str = editText.getText().toString();
String eq = resultDouble.toString();
if (eq.substring(eq.indexOf(".") + 1).equals("0")) {
eq = eq.substring(0, eq.indexOf("."));
}
if (str.equals("0") || str.equals(eq) || str.equals("Infinity")) {
str = "3";
} else {
str = str + "3";
}
editText.setText(str);
}
});
four = (Button) findViewById(R.id.btnfour);
four.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String str = editText.getText().toString();
String eq = resultDouble.toString();
if (eq.substring(eq.indexOf(".") + 1).equals("0")) {
eq = eq.substring(0, eq.indexOf("."));
}
if (str.equals("0") || str.equals(eq) || str.equals("Infinity")) {
str = "4";
} else {
str = str + "4";
}
editText.setText(str);
}
});
five = (Button) findViewById(R.id.btnfive);
five.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String str = editText.getText().toString();
String eq = resultDouble.toString();
if (eq.substring(eq.indexOf(".") + 1).equals("0")) {
eq = eq.substring(0, eq.indexOf("."));
}
if (str.equals("0") || str.equals(eq) || str.equals("Infinity")) {
str = "5";
} else {
str = str + "5";
}
editText.setText(str);
}
});
six = (Button) findViewById(R.id.btnsix);
six.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String str = editText.getText().toString();
String eq = resultDouble.toString();
if (eq.substring(eq.indexOf(".") + 1).equals("0")) {
eq = eq.substring(0, eq.indexOf("."));
}
if (str.equals("0") || str.equals(eq) || str.equals("Infinity")) {
str = "6";
} else {
str = str + "6";
}
editText.setText(str);
}
});
seven = (Button) findViewById(R.id.btnseven);
seven.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String str = editText.getText().toString();
String eq = resultDouble.toString();
if (eq.substring(eq.indexOf(".") + 1).equals("0")) {
eq = eq.substring(0, eq.indexOf("."));
}
if (str.equals("0") || str.equals(eq) || str.equals("Infinity")) {
str = "7";
} else {
str = str + "7";
}
editText.setText(str);
}
});
eight = (Button) findViewById(R.id.btneight);
eight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String str = editText.getText().toString();
String eq = resultDouble.toString();
if (eq.substring(eq.indexOf(".") + 1).equals("0")) {
eq = eq.substring(0, eq.indexOf("."));
}
if (str.equals("0") || str.equals(eq) || str.equals("Infinity")) {
str = "8";
} else {
str = str + "8";
}
editText.setText(str);
}
});
nine = (Button) findViewById(R.id.btnnine);
nine.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String str = editText.getText().toString();
String eq = resultDouble.toString();
if (eq.substring(eq.indexOf(".") + 1).equals("0")) {
eq = eq.substring(0, eq.indexOf("."));
}
if (str.equals("0") || str.equals(eq) || str.equals("Infinity")) {
str = "9";
} else {
str = str + "9";
}
editText.setText(str);
}
});
zero = (Button) findViewById(R.id.btnzero);
zero.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String str = editText.getText().toString();
String eq = resultDouble.toString();
if (eq.substring(eq.indexOf(".") + 1).equals("0")) {
eq = eq.substring(0, eq.indexOf("."));
}
if (str.equals("0") || str.equals(eq) || str.equals("Infinity")) {
str = "0";
} else {
str = str + "0";
}
editText.setText(str);
}
});
point = (Button) findViewById(R.id.btnpoint);
point.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String str = editText.getText().toString();
String eq = resultDouble.toString();
if (eq.substring(eq.indexOf(".") + 1).equals("0")) {
eq = eq.substring(0, eq.indexOf("."));
}
if (str.length() == 0 || str.equals(eq)) {
str = "0.";
} else if (str.indexOf(".") < 0) {
str = str + ".";
}
editText.setText(str);
}
});
del = (Button) findViewById(R.id.btndel);
del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String str = editText.getText().toString();
if (str.length() > 0) {
String eq = resultDouble.toString();
if (str.equals(eq)) {
str = "0";
} else {
str = str.substring(0, str.length() - 1);
}
}
if (str.length() == 0)
str = "0";
editText.setText(str);
}
});
clear = (Button) findViewById(R.id.btnclear);
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
editText.setText("0");
editText.setSelection(0);
}
});
plus = (Button) findViewById(R.id.btnplus);
plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String str = editText.getText().toString();
if (str.endsWith("+") || str.endsWith("-") || str.endsWith("×")
|| str.endsWith("÷")) {
str = str.substring(0, str.length() - 1) + "+";
} else {
str = str + "+";
}
editText.setText(str);
}
});
multiply = (Button) findViewById(R.id.btnmultiply);
multiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String str = editText.getText().toString();
if (str.endsWith("+") || str.endsWith("-") || str.endsWith("×")
|| str.endsWith("÷")) {
str = str.substring(0, str.length() - 1) + "×";
} else {
str = str + "×";
}
editText.setText(str);
}
});
minus = (Button) findViewById(R.id.btnminus);
minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String str = editText.getText().toString();
if (str.endsWith("+") || str.endsWith("-") || str.endsWith("×")
|| str.endsWith("÷")) {
str = str.substring(0, str.length() - 1) + "-";
} else {
str = str + "-";
}
editText.setText(str);
}
});
except = (Button) findViewById(R.id.btnexcept);
except.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String str = editText.getText().toString();
if (str.endsWith("+") || str.endsWith("-") || str.endsWith("×")
|| str.endsWith("÷")) {
str = str.substring(0, str.length() - 1) + "÷";
} else {
str = str + "÷";
}
editText.setText(str);
}
});
equal = (Button) findViewById(R.id.btnequal);
equal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String str = editText.getText().toString();
resultDouble = Calculate(str);
String iszero = resultDouble.toString().substring(
resultDouble.toString().indexOf(".") + 1);
if (iszero.equals("0")) {
str = resultDouble.toString().substring(0,
resultDouble.toString().indexOf("."));
} else {
str = resultDouble.toString();
}
editText.setText(str);
}
});
}
protected Double Calculate(String str) {
List<String> symbol = getsymbol(str);
List<Double> num = getnum(str);
if(num.size()==symbol.size())
num.add(0.0);
if (symbol != null) {
for (int i = 0; i < symbol.size(); i++) {
if (symbol.get(i).equals("×") || symbol.get(i).equals("÷")) {
String sign = symbol.remove(i);
Double n1 = num.remove(i);
Double n2 = num.remove(i);
Double result = 0.0;
if (sign.equals("×")) {
result = n1 * n2;
} else {
result = n1 / n2;
}
num.add(i, result);
i = i - 1;
}
}
}
while (!symbol.isEmpty()) {
String sign = symbol.remove(0);
Double n1 = num.remove(0);
Double n2 = num.remove(0);
Double result = 0.0;
if (sign.equals("+")) {
result = n1 + n2;
} else {
result = n1 - n2;
}
num.add(0, result);
}
return num.remove(0);
}
private List<Double> getnum(String str) {
StringTokenizer tokenizer = new StringTokenizer(str, "+-×÷");
List<Double> ops = new ArrayList<Double>();
while (tokenizer.hasMoreElements()) {
String string = tokenizer.nextElement().toString();
Double double1 = Double.parseDouble(string);
ops.add(double1);
}
return ops;
}
private List<String> getsymbol(String str) {
StringTokenizer tokenizer = new StringTokenizer(str, "0123456789.");
List<String> ops = new ArrayList<String>();
while (tokenizer.hasMoreElements()) {
String string = tokenizer.nextToken();
ops.add(string);
}
return ops;
}
}
第一次做手机软件,也许有很多问题,欢迎大家来指出
下载地址http://download.csdn.net/detail/ymq_2012/7028217