第一次接觸android,一個小型計算器再也不知道啥原理把代碼拷上瞭
[java]
package com.su.fun.calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
/**
*
* @author busy
*
*/
public class Calculator extends Activity implements OnClickListener{
//輸入狀態 true為覆蓋輸入 false為添加輸入
private boolean editFlag=true;
//計算狀態 true為進入計算 false為二元運算的替換
private boolean status=false;
//運算後的狀態
private boolean equstatus=false;
//異常狀態
private boolean ext=false;
//定義+ – * / 標示。
private final int ADD=1;
private final int SUB=2;
private final int MUL=3;
private final int DIV=4;
private final int DEF=-1;//default默認值
//對應數字按鈕和小數點按鈕。
private Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,bpoint;
//對應 + – * / 按鈕。
private Button badd,bsub,bmul,bp;
//對應清零按鈕和清空按鈕。
private Button bc,bce;
//對應開方和百分比按鈕。
private Button bsqrt,bper;
//對應倒數和正負數按鈕。
private Button binv,bsign;
//對應等於按鈕。
private Button bequ;
//對應回退按鈕。
private Button bb;
//對應顯示器。
private EditText value;
//結果。
private float result=0;
//操作數。
private float operand=0;
//正在進行的運算。
private int op=DEF;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initControl();//初始化。
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.bc)ext=false;
if(ext){
return;
}
switch (v.getId()) {
case R.id.b0:
addContent(0);
break;
case R.id.b1:
addContent(1);
break;
case R.id.b2:
addContent(2);
break;
case R.id.b3:
addContent(3);
break;
case R.id.b4:
addContent(4);
break;
case R.id.b5:
addContent(5);
break;
case R.id.b6:
addContent(6);
break;
case R.id.b7:
addContent(7);
break;
case R.id.b8:
addContent(8);
break;
case R.id.b9:
addContent(9);
break;
case R.id.bpoint://小數點
addPoint();
break;
case R.id.bb://退格
backSpace();
break;
case R.id.badd://'+'
saveResultBeforeCalculate();
op=ADD;
break;
case R.id.bsub://'-'
saveResultBeforeCalculate();
op=SUB;
break;
case R.id.bmul://'*'
saveResultBeforeCalculate();
op=MUL;
break;
case R.id.bp://'/'
saveResultBeforeCalculate();
op=DIV;
break;
case R.id.bequ://'='
equstatus=true;
calculate();
break;
case R.id.bsqrt:
sqrt();
break;
case R.id.binv:
inv();
break;
case R.id.bsign:
sign();
break;
case R.id.bper:
per();
break;
case R.id.bc:
crear();
break;
case R.id.bce:
value.setText(0+"");
break;
default:
break;
}
}
private void crear() {
result=0;
op=DEF;
editFlag=false;
status=false;
value.setText(0+"");
}
/**
* result=result*0.01
*/
private void per() {
editFlag=true;
float temp=getValueOfNow();
temp*=0.01;
value.setText(Float.toString(temp));
}
/**
* result=-result
*/
private void sign() {
editFlag=true;
float temp=getValueOfNow();
temp=-temp;
value.setText(Float.toString(temp));
}
/**
* 當result=0時,result=0
* 否則 result=1/result
*/
private void inv() {
editFlag=true;
float temp=getValueOfNow();
temp=temp==0?0:1/temp;
value.setText(Float.toString(temp));
}
/*
* 當result<=0 時 result=result
* 否則 result=sqrt(result)
*/
private void sqrt() {
editFlag=true;
float temp=getValueOfNow();
temp=temp>0?(float)Math.sqrt(temp):temp;
value.setText(Float.toString(temp));
}
/**
* 根據輸入狀態來決定覆蓋或插入
* @param c 輸入的數字 0-9
*/
private void addContent(int c){
System.out.println(c);
status=true;
String string=getStringOfNow();
if(string.trim().equals("0")||editFlag)string=String.valueOf(c);
else string=string+c;
editFlag=false;
value.setText(string);
inputStatus();
}
/**
* 輸入小數點若已存在則不處理
*/
private void addPoint(){
String string=getStringOfNow();
if(string.indexOf('.')==-1)value.setText(string+".");
inputStatus();
}
/**
* 退格處理若隻有一個字符怎清零
*/
private void backSpace(){
String string=getStringOfNow();
if(string.length()>1){
value.setText(string.substring(0, string.length()-1));
}else{
value.setText("0");
}
inputStatus();
}
/**
* 輸入時判斷其是否是計算狀態,如是怎清空狀態。
*/
private void inputStatus(){
operand=getValueOfNow();
if(equstatus){
result=operand;
op=DEF;
equstatus=false;
}
}
//計算前一次運算
private void calculate(){
editFlag=true;
status=false;
switch (op) {
case ADD:
result+=operand;
break;
case SUB:
result-=operand;
break;
case MUL:
result*=operand;
break;
case DIV:
if(operand==0){
value.setText("不能除0");
return;
}
result/=operand;
break;
default:
result=getValueOfNow();
break;
}
value.setText(Float.toString(result));
}
//(+ – * /)之前存入結果 www.aiwalls.com
private void saveResultBeforeCalculate(){
editFlag=true;
if(status)calculate();
else result=getValueOfNow();
}
/**
* 按鈕的初始化和事件的註冊。
*/
private void initControl() {
b1=(Button)findViewById(R.id.b1);
b2=(Button)findViewById(R.id.b2);
b3=(Button)findViewById(R.id.b3);
b4=(Button)findViewById(R.id.b4);
b5=(Button)findViewById(R.id.b5);
b6=(Button)findViewById(R.id.b6);
b7=(Button)findViewById(R.id.b7);
b8=(Button)findViewById(R.id.b8);
b9=(Button)findViewById(R.id.b9);
b0=(Button)findViewById(R.id.b0);
bpoint=(Button)findViewById(R.id.bpoint);
badd=(Button)findViewById(R.id.badd);
bsub=(Button)findViewById(R.id.bsub);
bmul=(Button)findViewById(R.id.bmul);
bp=(Button)findViewById(R.id.bp);
bc=(Button)findViewById(R.id.bc);
bce=(Button)findViewById(R.id.bce);
bsqrt=(Button)findViewById(R.id.bsqrt);
bper=(Button)findViewById(R.id.bper);
binv=(Button)findViewById(R.id.binv);
bsign=(Button)findViewById(R.id.bsign);
bequ=(Button)findViewById(R.id.bequ);
bb=(Button)findViewById(R.id.bb);
value=(EditText)findViewById(R.id.value);
value.setText("0");
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
b5.setOnClickListener(this);
b5.setOnClickListener(this);
b6.setOnClickListener(this);
b7.setOnClickListener(this);
b8.setOnClickListener(this);
b9.setOnClickListener(this);
b0.setOnClickListener(this);
bpoint.setOnClickListener(this);
badd.setOnClickListener(this);
bsub.setOnClickListener(this);
bmul.setOnClickListener(this);
bp.setOnClickListener(this);
bc.setOnClickListener(this);
bce.setOnClickListener(this);
bsqrt.setOnClickListener(this);
bper.setOnClickListener(this);
binv.setOnClickListener(this);
bsign.setOnClickListener(this);
bequ.setOnClickListener(this);
bb.setOnClickListener(this);
}
/**
*
* @return 當前顯示的值
* 異常則返回0
*/
private float getValueOfNow(){
String string=getStringOfNow();
float result=0;
try{
result=Float.parseFloat(string);
}catch (NumberFormatException e) {
ext=true;
}
return result;
}
/**
*
* @return 當前顯示的字符串
*/
private String getStringOfNow(){
return value.getText().toString();
}
}
[html]
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText android:id="@+id/value" android:singleLine="true"
android:editable="false" android:background="#ffff"
android:layout_height="wrap_content" android:layout_width="280dp"
android:layout_x="15dp" android:layout_y="10dp" android:textSize="18sp"></EditText>
<Button android:id="@+id/bb" android:layout_x="70dp"
android:layout_height="wrap_content" android:text="Backspace"
android:layout_width="wrap_content" android:layout_y="40dp"></Button>
<Button android:id="@+id/bce" android:layout_height="wrap_content"
android:text="CE" android:layout_x="190dp" android:layout_y="40dp"
android:layout_width="50dp"></Button>
<Button android:id="@+id/bc" android:layout_height="wrap_content"
android:text="C" android:layout_width="50dp" android:layout_x="250dp"
android:layout_y="40dp"></Button>
<Button android:id="@+id/b7" android:layout_height="wrap_content"
android:text="7" android:layout_width="50dp" android:layout_x="10dp"
android:layout_y="95dp"></Button>
<Button android:id="@+id/b8" android:layout_height="wrap_content"
android:text="8" android:layout_x="70dp" android:layout_y="95dp"
android:layout_width="50dp"></Button>
<Button android:id="@+id/b9" android:layout_height="wrap_content"
android:text="9" android:layout_x="130dp" android:layout_y="95dp"
android:layout_width="50dp"></Button>
<Button android:id="@+id/bp" android:layout_height="wrap_content"
android:text="/" android:layout_width="50dp" android:layout_x="190dp"
android:layout_y="95dp"></Button>
<Button android:id="@+id/bsqrt" android:layout_height="wrap_content"
android:text="sqrt" android:layout_x="250dp" android:layout_y="95dp"
android:layout_width="50dp"></Button>
<Button android:id="@+id/b4" android:layout_height="wrap_content"
android:text="4" android:layout_width="50dp" android:layout_x="10dp"
android:layout_y="150dp"></Button>
<Button android:id="@+id/b5" android:layout_height="wrap_content"
android:text="5" android:layout_width="50dp" android:layout_x="70dp"
android:layout_y="150dp"></Button>
<Button android:id="@+id/b6" android:layout_height="wrap_content"
android:text="6" android:layout_width="50dp" android:layout_x="130dp"
android:layout_y="150dp"></Button>
<Button android:id="@+id/bmul" android:layout_height="wrap_content"
android:text="*" android:layout_width="50dp" android:layout_x="190dp"
android:layout_y="150dp"></Button>
<Button android:id="@+id/bper" android:layout_height="wrap_content"
android:text="%" android:layout_width="50dp" android:layout_x="250dp"
android:layout_y="150dp"></Button>
<Button android:id="@+id/b1" android:layout_height="wrap_content"
android:text="1" android:layout_width="50dp" android:layout_x="10dp"
android:layout_y="205dp"></Button>
<Button android:id="@+id/b2" android:layout_height="wrap_content"
android:text="2" android:layout_width="50dp" android:layout_x="70dp"
android:layout_y="205dp"></Button>
<Button android:id="@+id/b3" android:layout_height="wrap_content"
android:text="3" android:layout_width="50dp" android:layout_x="130dp"
android:layout_y="205dp"></Button>
<Button android:id="@+id/bsub" android:layout_height="wrap_content"
android:text="-" android:layout_width="50dp" android:layout_x="190dp"
android:layout_y="205dp"></Button>
<Button android:id="@+id/binv" android:layout_height="wrap_content"
android:text="1/x" android:layout_width="50dp" android:layout_x="250dp"
android:layout_y="205dp"></Button>
<Button android:id="@+id/b0" android:layout_height="wrap_content"
android:text="0" android:layout_width="50dp" android:layout_x="10dp"
android:layout_y="260dp"></Button>
<Button android:id="@+id/bsign" android:layout_height="wrap_content"
android:text="-/+" android:layout_width="50dp" android:layout_x="70dp"
android:layout_y="260dp"></Button>
<Button android:id="@+id/bpoint" android:layout_height="wrap_content"
android:text="." android:layout_width="50dp" android:layout_x="130dp"
android:layout_y="260dp"></Button>
<Button android:id="@+id/badd" android:layout_height="wrap_content"
android:text="+" android:layout_width="50dp" android:layout_x="190dp"
android:layout_y="260dp"></Button>
<Button android:id="@+id/bequ" android:layout_height="wrap_content"
android:text="=" android:layout_width="50dp" android:layout_x="250dp"
android:layout_y="260dp"></Button>
</AbsoluteLayout>
摘自 編程小學生