Android計算器嘗試

學了一段時間Android了,一直都是在看,沒有什麼嘗試,恰好最近大體學會了gridview配合simpleadpter的使用,因而想着動手練習一下,就選擇了寫一個最簡單的計算器來實現。java

只包含+-*/四種最基本的運算,不過即便是這麼簡單的小程序也搞了大半天才搞定。也是由於第一次寫的時候比較隨意,沒有畫流程圖,方法也封裝的好,結果後來優化調試的時候特別難受,最後一怒之下刪除從新來過,此次注重了總體流程的把握和方法的封裝寫起來思路清晰了不少,不一會就調試實現了預期的功能,能夠說是一次不錯的嘗試吧!android

  因此總結就是之後寫代碼以前儘可能先畫流程圖,而後要注意代碼規範和封裝,避免文不加點出現過長的方法。小程序

下面是Mainactivity的代碼緩存

[code name=java]app

package com.fantasy.calculator;ide

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 };this

@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);spa

}調試

/*
* 爲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]

相關文章
相關標籤/搜索