用Android写出的简单计算器
首先是布局:用GridLayout写的布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ccc"
android:id="@+id/layout_cal"
app:columnCount="4"
app:rowCount="6">
<TextView
android:id="@+id/edit_text"
android:layout_margin="10dp"
android:layout_marginTop="20dp"
android:background="@drawable/edit_bg"
android:gravity="right"
android:padding="10dp"
android:textSize="30dp"
app:layout_columnSpan="4"
app:layout_columnWeight="1"
android:maxLength="14"
tools:text="0" />
<TextView
style="@style/operatorStyle"
android:text="c"
app:layout_columnWeight="1"
app:layout_rowWeight="1" />
<TextView
style="@style/operatorStyle"
android:text="+/-"
app:layout_columnWeight="1"
app:layout_rowWeight="1" />
<TextView
style="@style/operatorStyle"
android:text="/"
app:layout_columnWeight="1"
app:layout_rowWeight="1" />
<TextView
style="@style/operatorStyle"
android:text="*"
app:layout_columnWeight="1"
app:layout_rowWeight="1" />
<TextView
style="@style/numbersStyle"
android:text="7"
app:layout_columnWeight="1"
app:layout_rowWeight="1" />
<TextView
style="@style/numbersStyle"
android:text="8"
app:layout_columnWeight="1"
app:layout_rowWeight="1" />
<TextView
style="@style/numbersStyle"
android:text="9"
app:layout_columnWeight="1"
app:layout_rowWeight="1" />
<TextView
style="@style/operatorStyle"
android:text="-"
app:layout_columnWeight="1"
app:layout_rowWeight="1" />
<TextView
style="@style/numbersStyle"
android:text="4"
app:layout_columnWeight="1"
app:layout_rowWeight="1" />
<TextView
style="@style/numbersStyle"
android:text="5"
app:layout_columnWeight="1"
app:layout_rowWeight="1" />
<TextView
style="@style/numbersStyle"
android:text="6"
app:layout_columnWeight="1"
app:layout_rowWeight="1" />
<TextView
style="@style/operatorStyle"
android:text="+"
app:layout_columnWeight="1"
app:layout_rowWeight="1" />
<TextView
style="@style/numbersStyle"
android:text="1"
app:layout_columnWeight="1"
app:layout_rowWeight="1" />
<TextView
style="@style/numbersStyle"
android:text="2"
app:layout_columnWeight="1"
app:layout_rowWeight="1" />
<TextView
style="@style/numbersStyle"
android:text="3"
app:layout_columnWeight="1"
app:layout_rowWeight="1" />
<TextView
style="@style/operatorStyle"
android:text="="
android:background="@drawable/equals_bg"
app:layout_columnWeight="1"
app:layout_rowSpan="2"
app:layout_rowWeight="1" />
<TextView
style="@style/numbersStyle"
android:text="0"
app:layout_columnSpan="2"
app:layout_columnWeight="1"
app:layout_rowWeight="1" />
<TextView
style="@style/numbersStyle"
android:text="."
app:layout_columnWeight="1"
app:layout_rowWeight="1" />
</android.support.v7.widget.GridLayout>
Android代码:
package com.example.xiaohong.calculatordemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayout;
import android.view.View;
import android.widget.TextView;
import java.util.Locale;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView editView;
private String operator = "";
private float result = 0.0f;
private boolean flag = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cal_activity);
editView = (TextView) findViewById(R.id.edit_text);
// 对于一个布局,如果想对布局中的控件进行监听,而不想为每一个设置id.可以通过下面这个方法做到.
GridLayout grid = (GridLayout) findViewById(R.id.layout_cal);
for (int i = 0; i < grid.getChildCount(); i++) {
TextView text = (TextView) grid.getChildAt(i);
if (!text.equals(editView)) {
text.setOnClickListener(this);
}
}
}
@Override
public void onClick(View view) {
TextView text = (TextView) view;
StringBuilder builder = new StringBuilder(editView.getText().toString());
String str = text.getText().toString();
switch (str) {
case "c":
builder.delete(0, builder.length());
builder.append("0");
break;
case "+/-":
result = Float.valueOf(builder.toString());
result = -result;
builder.delete(0, builder.length());
builder.append(String.format(Locale.CANADA, result % 1 == 0 ? "%.0f" : "%.2f", result));
break;
// 这里去掉了break,这是我绝对想不出来的啊.
case "/":
case "*":
case "+":
case "-":
case "=":
float temp;
if (builder.length() > 0) {
temp = Float.parseFloat(builder.toString());
} else {
temp = 0;
}
if (!flag) {
builder.delete(0, builder.length());
try {
switch (operator) {
case "+":
result += temp;
break;
case "-":
result -= temp;
break;
case "*":
result *= temp;
break;
case "/":
result /= temp;
break;
default:
// 如果一直点"=",或者在第一次记录第一个运算数
result = temp;
break;
}
builder.append(String.format(Locale.CANADA, result % 1 == 0 ? "%.0f" : "%.2f", result));
} catch (Exception e) {
e.printStackTrace();
builder.append("erro");
}
}
operator = str;
flag = true;
break;
case ".":
if (!builder.toString().contains(".")) {
if (builder.length() == 0) {
builder.append("0");
}
builder.append(".");
}
break;
default:
// 在输入第二个运算符之前,将flag设为false,这样就可以保证在输入运算符时只记录最后一个运算符.棒棒哒...
if (flag) {
flag = false;
builder.delete(0, builder.length());
}
if ("0".equals(builder.toString())) {
builder.delete(0, builder.length());
builder.append(str);
} else {
builder.append(str);
}
break;
}
if (builder.length() > 14) {
editView.setText(String.format(Locale.CANADA, "%e", Float.valueOf(builder.toString())));
} else {
editView.setText(builder.toString());
}
}
}
效果: