学了一段时间Android了,一直都是在看,没有什么尝试,刚好最近大致学会了gridview配合simpleadpter的使用,于是想着动手练习一下,就选择了写一个最简单的计算器来实现。
只包含+-*/四种最基本的运算,不过即使是这么简单的小程序也搞了大半天才搞定。也是因为第一次写的时候比较随意,没有画流程图,方法也封装的好,结果后来优化调试的时候特别难受,最后一怒之下删除重新来过,这次注重了整体流程的把握和方法的封装写起来思路清晰了很多,不一会就调试实现了预期的功能,可以说是一次不错的尝试吧!
所以总结就是以后写代码之前尽量先画流程图,然后要注意代码规范和封装,避免文不加点出现过长的方法。
下面是Mainactivity的代码
[code name=java]
package com.fantasy.calculator;
import java.util.ArrayList;
import java.util.HashMap;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class MainActivity extends Activity implements OnItemClickListener {
private GridView gv; //键盘
private TextView display; //屏幕
private float a, b, sum = 0; //a,b,为两个运算数,sum为运算结果
private int operator = 0; //运算种类标志符
private int equalFlag=0; //按“=”次数标志
String sa = "", temp = ""; //用于接收输入的字符串
/*
* 主界面的图片资源
*/
int[] image = { R.drawable.number7, R.drawable.number8, R.drawable.number9,
R.drawable.division, R.drawable.number4, R.drawable.number5,
R.drawable.number6, R.drawable.multiply, R.drawable.number1,
R.drawable.number2, R.drawable.number3, R.drawable.minus,
R.drawable.number0, R.drawable.dot, R.drawable.equals,
R.drawable.add, R.drawable.clear, R.drawable.off };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display = (TextView) findViewById(R.id.textView1);
gv = (GridView) findViewById(R.id.gridView1);
gv.setAdapter(getAdapter(image));
gv.setOnItemClickListener(this);
}
/*
* 为int[]res获取适配的simpleadapter
*/
private SimpleAdapter getAdapter(int[] res) {
ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
for (int i = 0; i < res.length; i++) {
HashMap<String, Object> hashMap = new HashMap<String, Object>();
hashMap.put("button", res[i]);
data.add(hashMap);
}
SimpleAdapter myAdapter = new SimpleAdapter(this, data,
R.layout.buttonitem, new String[] { "button" },
new int[] { R.id.ib_keyboard });
return myAdapter;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
// 如果按下的是数字键则用temp接收并显示
if (position != 3 && position != 7 && position != 11 && position != 15
&& position != 14 && position != 16 && position != 17) {
temp = getResources().getStringArray(R.array.text)[position];
sa += temp;
b = Float.parseFloat(sa);
display.setText(sa);
}
/*
* 按下运算符和功能键的处理逻辑
*/
switch (position) {
case 3:// 除号按下
equalFlag = 0;
display.setText("\\");
//dividePressed();
operatorPressed(4);
break;
case 7:// 乘号按下
equalFlag = 0;
display.setText("*");
//multiplyPressed();
operatorPressed(3);
break;
case 11:// 减号按下
equalFlag = 0;
display.setText("-");
//subPressed();
operatorPressed(2);
break;
case 15:// 加号按下
equalFlag = 0;
display.setText("+");
//addPressed();
operatorPressed(1);
break;
case 14: // 等号按下
equalFlag++;
showResult();
break;
case 16: // clear按下
clear();
break;
case 17: // 退出按下
this.finish();
break;
default:
break;
}
}
private void addPressed() { //加号按下处理逻辑
// TODO Auto-generated method stub
if (a == 0) {
a = b;
b = 0;
sa = "";
temp = "";
operator = 1;
} else if (operator != 1) {
showResult();
a=sum;
operator = 1;
} else if (operator == 1) {
showResult();
a = sum;
}
}
private void subPressed() {
// TODO Auto-generated method stub
if (a == 0) {
a = b;
b = 0;
sa = "";
temp = "";
operator = 2;
} else if (operator != 2) {
showResult();
a=sum;
operator = 2;
} else if (operator == 2) {
showResult();
a = sum;
}
}
private void multiplyPressed() {
// TODO Auto-generated method stub
if (a == 0) {
a = b;
b = 0;
sa = "";
temp = "";
operator = 3;
} else if (operator != 3) {
showResult();
a=sum;
operator=3;
} else if (operator == 3) {
showResult();
a = sum;
}
}
private void dividePressed() {
// TODO Auto-generated method stub
if (a == 0) {
a = b;
b = 0;
sa = "";
temp = "";
operator = 4;
} else if (operator != 4) {
showResult();
a=sum;
operator = 4;
} else if (operator == 4) {
showResult();
a = sum;
}
}
private void operatorPressed(int sign) {
// TODO Auto-generated method stub
if (a == 0) {
a = b;
b = 0;
sa = "";
temp = "";
operator = sign;
} else if (operator != sign) {
showResult();
a=sum;
operator = sign;
} else if (operator == sign) {
showResult();
a = sum;
}
}
private void showResult() {
// TODO Auto-generated method stub
switch (operator) { //判断运算种类
case 4: //如果是除法
if (equalFlag <= 1) { //如果是第一次按等号 下同
sum = divide(a, b);
display.setText(a+"\\"+b+"="+sum );
sa = "";
temp = "";
} else if (equalFlag >= 2) { //如果是连续按等号 下同
a=sum;
sum = divide(a, b);
display.setText(a+"\\"+b+"="+sum );
}
break;
case 3:
if (equalFlag <= 1) {
sum = multiply(a, b);
display.setText(a+"*"+b+"="+sum );
sa = "";
temp = "";
} else if (equalFlag >= 2) {
a=sum;
sum = multiply(a, b);
display.setText(a+"*"+b+"="+sum );
}
break;
case 2:
if (equalFlag <= 1) {
sum = sub(a, b);
display.setText(a+"-"+b+"="+sum );
sa = "";
temp = "";
} else if (equalFlag >= 2) {
a=sum;
sum = sub(a, b);
display.setText(a+"-"+b+"="+sum );
}
break;
case 1:
if (equalFlag <= 1) {
sum = add(a, b);
display.setText(a+"+"+b+"="+sum );
sa = "";
temp = "";
} else if (equalFlag >= 2) {
a=sum;
sum = add(a, b);
display.setText(a+"+"+b+"="+sum );
}
break;
default:
break;
}
}
// 清空屏幕和缓存
private void clear() {
// TODO Auto-generated method stub
a = b = sum = 0;
operator = 0;
equalFlag=0;
sa = "";
display.setText("");
}
// 加法
private float add(float x, float y) {
return x + y;
}
// 减法
private float sub(float x, float y) {
return x - y;
}
// 乘法
private float multiply(float x, float y) {
return x * y;
}
// 除法
private float divide(float x, float y) {
if (y != 0) {
return (x / y);
} else {
return Float.NaN;
}
}
}
[/code]