Android开发实现计算器的例子
例子
代码如下 复制代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/warpcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<HorizontalScrollView
android:id="@+id/scrollview"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/resultText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right"
android:padding="8dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="@string/number0"
android:textSize="40sp" />
</HorizontalScrollView >
<TableLayout
android:id="@+id/buttonTable"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:stretchColumns="*" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:onClick="onClickListener"
android:text="@string/number7" />
<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:onClick="onClickListener"
android:text="@string/number8" />
<Button
android:id="@+id/button9"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:onClick="onClickListener"
android:text="@string/number9" />
<Button
android:id="@+id/buttonBackSpace"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:onClick="onClickListener"
android:text="@string/backspace" />
<Button
android:id="@+id/buttonClear"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:onClick="onClickListener"
android:text="@string/clear" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:onClick="onClickListener"
android:text="@string/number4" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:onClick="onClickListener"
android:text="@string/number5" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:onClick="onClickListener"
android:text="@string/number6" />
<Button
android:id="@+id/buttonMultiply"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:onClick="onClickListener"
android:text="@string/multiply" />
<Button
android:id="@+id/buttonDivide"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:onClick="onClickListener"
android:text="@string/divide" />
</TableRow>
</TableLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
<TableLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="3"
android:stretchColumns="*" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:onClick="onClickListener"
android:text="@string/number1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:onClick="onClickListener"
android:text="@string/number2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:onClick="onClickListener"
android:text="@string/number3" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<Button
android:id="@+id/button0"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:onClick="onClickListener"
android:text="@string/number0" />
<Button
android:id="@+id/buttonDot"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:onClick="onClickListener"
android:text="@stri(www.111cn.net)ng/dot" />
<Button
android:id="@+id/buttonNegSign"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:onClick="onClickListener"
android:text="@string/negSign" />
</TableRow>
</TableLayout>
<Button
android:id="@+id/buttonPlus"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:onClick="onClickListener"
android:text="@string/plus" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical" >
<Button
android:id="@+id/buttonMinus"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:onClick="onClickListener"
android:text="@string/minus" />
<Button
android:id="@+id/buttonEval"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:onClick="onClickListener"
android:text="@string/eval" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
MainActivity类和之前的java计算器差不多:
代码如下 复制代码
package me.jerrys.calc;
import java.math.BigDecimal;
import android.support.v7.app.ActionBarActivity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private TextView textView;
private BigDecimal lastNum = new BigDecimal("0");
private BigDecimal currentNum = new BigDecimal("0");
private String currentText;
private boolean typeClean = false;
private boolean hadCalced = false;
private MODE currentMode = MODE.PLUS;
private enum MODE {
PLUS, MINUS, MULTIPLY, DIVIDE
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.resultText);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onClickListener(View v) {
currentText = textView.getText().toString();
switch (v.getId()) {
case R.id.button0:
addNumber("0");
break;
case R.id.button1:
addNumber("1");
break;
case R.id.button2:
addNumber("2");
break;
case R.id.button3:
addNumber("3");
break;
case R.id.button4:
addNumber("4");
break;
case R.id.button5:
addNumber("5");
break;
case R.id.button6:
addNumber("6");
break;
case R.id.button7:
addNumber("7");
break;
case R.id.button8:
addNumber("8");
break;
case R.id.button9:
addNumber("9");
break;
case R.id.buttonNegSign:
addSign();
break;
case R.id.buttonClear:
clear();
break;
case R.id.buttonBackSpace:
backSpace();
break;
case R.id.buttonPlus:
setMode(MODE.PLUS);
break;
case R.id.buttonMinus:
setMode(MODE.MINUS);
break;
case R.id.buttonMultiply:
setMode(MODE.MULTIPLY);
break;
case R.id.buttonDivide:
setMode(MODE.DIVIDE);
break;
case R.id.buttonEval:
calc();
break;
case R.id.buttonDot:
addNumber(".");
break;
}
}
private void calc() {
if (hadCalced && typeClean) return;
currentNum = new BigDecimal(currentText);
try {
switch (currentMode) {
case PLUS:
lastNum = lastNum.add(currentNum);
break;
case MINUS:
lastNum = lastNum.subtract(currentNum);
break;
case MULTIPLY:
lastNum = lastNum.multiply(currentNum);
break;
case DIVIDE:
lastNum = lastNum.divide(currentNum, 16,
BigDecimal.ROUND_HALF_DOWN);
break;
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "发生错误:"+e.getMessage(), Toast.LENGTH_SHORT).show();
return;
}
String resultText = lastNum.toString();
if (resultText.indexOf(".") > 0) { // 消除小数多余位数
resultText = resultText.replaceAll("0+?$", "");
resultText = resultText.replaceAll("[.]$", "");
}
setResultText(resultText);
typeClean = true;
hadCalced = false;
}
private void setMode(MODE mode) {
if (!hadCalced && !typeClean) {
calc();
currentMode = mode;
return;
}
lastNum = new BigDecimal(currentText);
currentMode = mode;
typeClean = true;
hadCalced = true;
}
private void addSign() {
if (currentText.equals("0") || currentText.equals("")) return;
if (currentText.startsWith("-")) setResultText(currentText.substring(1));
else setResultText("-" + currentText);
}
private void clear() {
currentNum = new BigDecimal("0");
lastNum = new BigDecimal("0");
typeClean = false;
currentMode = MODE.PLUS;
setResultText("0");
}
private void setResultText(String string) {
textView.setText(string);
}
public void addNumber(String num) {
String resultText = currentText;
if (typeClean == true) {
resultText = "0";
typeClean = false;
}
if (num.equals(".") && currentText.lastIndexOf(".") != -1) return;
if ((resultText.equals("0") || resultText.equals(""))
&& !num.equals("."))
resultText = "";
resultText = resultText + num;
setResultText(resultText);
}
private void backSpace() {
if (currentText.equals("0") || currentText.equals("")) return;
if (currentText.length() == 1) {
setResultText("0");
return;
}
setResultText(currentText.substring(0, currentText.length() - 1));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_about) {
new AlertDialog.Builder(this).setTitle("关于").setMessage("【Android www.111cn.net简易计算器】n作者 JerryLocken博客 n源码 /android-simple-calc").setPositiveButton("确定", null).show();
return true;
}
return super.onOptionsItemSelected(item);
}
}
from:http://www.111cn.net/sj/android/60746.htm