学过一些简单的布局与编写代码的原理,模仿Windows的计算器,编写了一个简单的手机版本的。下一步是模仿iphone的计算器。下面是运行的效果图:
创建项目,命名为Calculator,首先编写布局文件,这里布局使用TableLayout,都是整行整列,使用它很方便,下面是布局文件的内容:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
>
<TableLayout
android:layout_marginTop="8px"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableRow>
<TextView
android:id="@+id/equation"
android:layout_weight="1"
android:gravity="right"
android:textColor="@color/black"
android:textSize="17px"
/>
</TableRow>
<TableRow>
<TextView
android:id="@+id/result"
android:layout_weight="1"
android:gravity="right"
android:textColor="@color/black"
android:textSize="23px"
/>
</TableRow>
<TableRow>
<Button
android:id="@+id/id7"
android:text="7"
android:layout_weight="1"
/>
<Button
android:id="@+id/id8"
android:text="8"
android:layout_weight="1"
/>
<Button
android:id="@+id/id9"
android:text="9"
android:layout_weight="1"
/>
<Button
android:id="@+id/idDivid"
android:text="/"
android:layout_weight="1"
/>
</TableRow>
<TableRow>
<Button
android:id="@+id/id4"
android:text="4"
android:layout_weight="1"
/>
<Button
android:id="@+id/id5"
android:text="5"
android:layout_weight="1"
/>
<Button
android:id="@+id/id6"
android:text="6"
android:layout_weight="1"
/>
<Button
android:id="@+id/idMultiple"
android:text="*"
android:layout_weight="1"
/>
</TableRow>
<TableRow>
<Button
android:id="@+id/id1"
android:text="1"
android:layout_weight="1"
/>
<Button
android:id="@+id/id2"
android:text="2"
android:layout_weight="1"
/>
<Button
android:id="@+id/id3"
android:text="3"
android:layout_weight="1"
/>
<Button
android:id="@+id/idMinus"
android:text="-"
android:layout_weight="1"
/>
</TableRow>
<TableRow>
<Button
android:id="@+id/id0"
android:text="0"
android:layout_weight="1"
/>
<Button
android:id="@+id/iddot"
android:text="."
android:layout_weight="1"
/>
<Button
android:id="@+id/idequal"
android:text="="
android:layout_weight="1"
/>
<Button
android:id="@+id/idPlus"
android:text="+"
android:layout_weight="1"
/>
</TableRow>
</TableLayout>
</LinearLayout>
然后是编写主Activity,这里把按钮分为两组,一组是数字,一组是运算符,分别给它们添加Listener,下面是代码:
package com.sinaapp.sheldonchen;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
public class CalculatorActivity extends Activity {
protected final static String TAG = "CALCULATOR_ACTIVITY";
private static int[] numbers = { R.id.id0, R.id.id1, R.id.id2, R.id.id3,
R.id.id4, R.id.id5, R.id.id6, R.id.id7, R.id.id8, R.id.id9 };
private static int[] ops = { R.id.idPlus, R.id.idMinus, R.id.idMultiple,
R.id.idDivid, R.id.iddot, R.id.idequal };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView equationView = (TextView)this.findViewById(R.id.equation);
TextView resultView = (TextView) this.findViewById(R.id.result);
for (int number : numbers) {
Button button = (Button) this.findViewById(number);
button.setOnClickListener(new NumberButtonListener(resultView, equationView));
}
for (int op : ops) {
Button button = (Button)this.findViewById(op);
button.setOnClickListener(new OperationButtonListener(resultView, equationView));
}
}
}
然后是处理数字的Listener:
package com.sinaapp.sheldonchen;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class NumberButtonListener implements OnClickListener {
protected static final String TAG = "NUMBER_BUTTON";
private TextView textView;
private TextView equationView;
public static boolean isNew = true;
public NumberButtonListener(TextView textView, TextView equationView) {
this.textView = textView;
this.equationView = equationView;
}
@Override
public void onClick(View v) {
Button b = null;
if (v instanceof Button) {
b = (Button) v;
String result = textView.getText().toString();
String text = b.getText().toString();
if ((result.length() == 0 && text.equals("0"))
|| result.trim().equals("0") || isNew == true) {
this.equationView.setText("");
result = text;
isNew = false;
} else {
result += text;
}
textView.setText(result);
}
}
}
接着是处理运算符的Listener:
package com.sinaapp.sheldonchen;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class OperationButtonListener implements OnClickListener {
protected static final String TAG = "OPERATION_BUTTON";
private TextView resultView;
private TextView equationView;
private static String nowResult = null;
private static List<String> list = new LinkedList<String>();
private static int index = 0;
private static List<String> opList = new ArrayList<String>();
public OperationButtonListener(TextView resultView, TextView equationView) {
this.resultView = resultView;
this.equationView = equationView;
opList.add("+");
opList.add("-");
opList.add("*");
opList.add("/");
}
@Override
public void onClick(View v) {
Button button = null;
if (v instanceof Button) {
Log.d(TAG, "list size: " + list.size());
String result = resultView.getText().toString();
button = (Button) v;
int id = button.getId();
if (id == R.id.iddot) {
if (!result.contains(".")) {
result += ".";
}
resultView.setText(result);
} else if (id == R.id.idequal) {
if (list.size() == 2) {
String equation = this.equationView.getText().toString();
if (equation.trim().equals("")) {
equation = result;
} else {
equation = equation + " " + result;
}
this.equationView.setText(equation);
double opt1 = Double.valueOf(list.get(0));
String operation = list.get(1);
list.remove(0);
list.remove(0);
double opt2 = Double.valueOf(result);
if (operation.trim().equals("+")) {
nowResult = String.valueOf((opt1 + opt2));
} else if (operation.trim().equals("-")) {
nowResult = String.valueOf((opt1 - opt2));
} else if (operation.trim().equals("*")) {
nowResult = String.valueOf((opt1 * opt2));
} else if (operation.trim().equals("/")) {
nowResult = String.valueOf((opt1 / opt2));
}
index = 0;
NumberButtonListener.isNew = true;
Log.v(TAG, "result: " + nowResult);
this.resultView.setText(nowResult);
}
} else {
String op = button.getText().toString();
if (list.size() == 0 || opList.contains(list.get(list.size() - 1))) {
list.add(index, result);
index++;
list.add(index, op);
index++;
}
this.resultView.setText("");
String equation = this.equationView.getText().toString();
if (equation.trim().equals("")) {
equation = result;
} else {
equation = equation + " " + result;
}
this.equationView.setText(equation + " " + op);
if (list.size() == 4) {
double opt1 = Double.valueOf((String) list.get(0));
String operation = (String) list.get(1);
Log.v(TAG, "operation: " + operation);
double opt2 = Double.valueOf((String) list.get(2));
if (operation.trim().equals("+")) {
nowResult = String.valueOf((opt1 + opt2));
} else if (operation.trim().equals("-")) {
nowResult = String.valueOf((opt1 - opt2));
} else if (operation.trim().equals("*")) {
nowResult = String.valueOf((opt1 * opt2));
} else if (operation.trim().equals("/")) {
nowResult = String.valueOf((opt1 / opt2));
}
list.set(0, nowResult);
list.set(1, list.get(3));
index = 2;
list.remove(2);
list.remove(2);
Log.v(TAG, "result: " + nowResult);
NumberButtonListener.isNew = true;
this.resultView.setText(nowResult);
}
}
}
}
}
最后在res的values文件夹里,添加一个color.xml文件,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="black">#000000</color>
</resources>
还需要一个背景图片,只要添加到drawable目录下,再把layout修改相应的值即可。