参考下面代码
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:layout_height="wrap_content">
android:id="@+id/tvResult"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:height="50dp"
android:text="@string/tvResult"
/>
android:layout_height="wrap_content">
android:id="@+id/btnBackspace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="150dp"
android:layout_marginLeft="10dp"
android:text="@string/btnbackspace"/>
android:id="@+id/btnCE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="150dp"
android:text="@string/btnCE"/>
android:layout_height="wrap_content">
android:id="@+id/btn7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:width="75dp"
android:text="@string/btn7"/>
android:id="@+id/btn8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btn8"/>
android:id="@+id/btn9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btn9"/>
android:id="@+id/btnDiv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btnDiv"/>
android:layout_height="wrap_content">
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:width="75dp"
android:text="@string/btn4"/>
android:id="@+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btn5"/>
android:id="@+id/btn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btn6"/>
android:id="@+id/btnMul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btnMul"/>
android:layout_height="wrap_content">
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:width="75dp"
android:text="@string/btn1"/>
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btn2"/>
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btn3"/>
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btnAdd"/>
android:layout_height="wrap_content">
android:id="@+id/btn0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:width="75dp"
android:text="@string/btn0"/>
android:id="@+id/btnC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btnC"/>
android:id="@+id/btnEqu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btnEqu"/>
android:id="@+id/btnSub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btnSub"/>
---------------------------
来自网页的消息
---------------------------
代码已经复制到粘贴板!
---------------------------
确定
---------------------------
package org.hemingway.calculater;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class CalculaterMain extends Activity implements View.OnClickListener {
private EditText output;
private EditText input;
private Button btn0;
private Button btn1;
private Button btn2;
private Button btn3;
private Button btn4;
private Button btn5;
private Button btn6;
private Button btn7;
private Button btn8;
private Button btn9;
private Button btnadd;
private Button btnsubtract;
private Button btnmultiply;
private Button btndivide;
private Button btnclear;
private Button btnresult;
private String str;//保存数字
private String strold;
private char act;//记录符号
private int count;//判断要计算的次数,如果超过一个符号,先算出来一部分
private int result;//计算的结果
private int err;//无错误的时候为0
private int flag;//一个标志,如果为1,可以响应运算消息,如果为0,不响应运算消息,只有前面是数字才可以响应运算消息
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
output=(EditText)findViewById(R.id.output);
output.setText(R.string.output);
input=(EditText)findViewById(R.id.input);
input.setText(R.string.input);
btn0=(Button)findViewById(R.id.zero);
btn0.setText(R.string.zero);
btn1=(Button)findViewById(R.id.one);
btn1.setText(R.string.one);
btn2=(Button)findViewById(R.id.two);
btn2.setText(R.string.two);
btn3=(Button)findViewById(R.id.three);
btn3.setText(R.string.three);
btn4=(Button)findViewById(R.id.four);
btn4.setText(R.string.four);
btn5=(Button)findViewById(R.id.five);
btn5.setText(R.string.five);
btn6=(Button)findViewById(R.id.six);
btn6.setText(R.string.six);
btn7=(Button)findViewById(R.id.seven);
btn7.setText(R.string.seven);
btn8=(Button)findViewById(R.id.eight);
btn8.setText(R.string.eight);
btn9=(Button)findViewById(R.id.nine);
btn9.setText(R.string.nine);
btnadd=(Button)findViewById(R.id.add);
btnadd.setText(R.string.add);
btnsubtract=(Button)findViewById(R.id.subtract);
btnsubtract.setText(R.string.subtract);
btnmultiply=(Button)findViewById(R.id.multiply);
btnmultiply.setText(R.string.multiply);
btndivide=(Button)findViewById(R.id.divide);
btndivide.setText(R.string.divide);
btnclear=(Button)findViewById(R.id.clear);
btnclear.setText(R.string.clear);
btnresult=(Button)findViewById(R.id.result);
btnresult.setText(R.string.result);
btn0.setOnClickListener(this);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
btn6.setOnClickListener(this);
btn7.setOnClickListener(this);
btn8.setOnClickListener(this);
btn9.setOnClickListener(this);
btnadd.setOnClickListener(this);
btnsubtract.setOnClickListener(this);
btnmultiply.setOnClickListener(this);
btndivide.setOnClickListener(this);
btnclear.setOnClickListener(this);
btnresult.setOnClickListener(this);
act=' ';
str="";
strold="";
count=0;
result=0;
err=0;//处理异常,默认无异常
flag=0;//默认不能响应,按下第一个数字后改为1,可以响应
}
public void onClick(View view) {
// TODO Auto-generated method stub
switch(view.getId()){
case R.id.zero: num(0) ;
break;
case R.id.one: num(1) ;
break;
case R.id.two: num(2) ;
break;
case R.id.three: num(3) ;
break;
case R.id.four: num(4) ;
break;
case R.id.five: num(5) ;
break;
case R.id.six: num(6) ;
break;
case R.id.seven: num(7) ;
break;
case R.id.eight: num(8) ;
break;
case R.id.nine: num(9) ;
break;
case R.id.add: add() ;
break;
case R.id.subtract: sub() ;
break;
case R.id.multiply: multiply() ;
break;
case R.id.divide: divide() ;
break;
case R.id.clear: clear();
break;
case R.id.result:
result() ;
if(err!=1&&flag!=0)
output.setText(String.valueOf(result));
break;
default:
break;
}
input.setText(strold+act+str);
}
public void num(int n){
str=str+String.valueOf(n);
flag=1;
}
public void add(){
if(flag!=0)
{
check();
act='+';
flag=0;
}
}
public void sub(){
if(flag!=0)
{
check();
act='-';
flag=0;
}
}
public void multiply(){
if(flag!=0)
{
check();
act='*';
flag=0;
}
}
public void divide(){
if(flag!=0)
{
check();
act='/';
flag=0;
}
}
public void clear(){
str=strold="";
count=0;
act=' ';
result=0;
flag=0;
input.setText(strold+act+str);
output.setText("");
}
public void check(){
if(count>=1)
{
result();
str=String.valueOf(result);
}
strold=str;
str="";
count++;
}
public void result(){
int a,b;
a=Integer.parseInt(strold);
b=Integer.parseInt(str);
if(flag!=0)
{
if(b==0&&act=='/') {
clear();
output.setText("除数不能为零!");
err=1;
}
result=0;
if(err!=1){
switch(act){
case '+':
result=a+b;
break;
case '-':
result=a-b;
break;
case '*':
result=a*b;
break;
case '/':
result=a/b;
break;
default:
break;
}
}
}
}
public void onConfigurationChanged(Configuration newConfig){
if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
//横向
setContentView(R.layout.main);
}else{
//竖向
setContentView(R.layout.main);
}
}
}
R.java
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
package org.hemingway.calculater;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int ic_launcher=0x7f020000;
public static final int icon=0x7f020001;
}
public static final class id {
public static final int add=0x7f050005;
public static final int clear=0x7f05000f;
public static final int divide=0x7f050011;
public static final int eight=0x7f050003;
public static final int five=0x7f050007;
public static final int four=0x7f050006;
public static final int input=0x7f050000;
public static final int multiply=0x7f05000d;
public static final int nine=0x7f050004;
public static final int one=0x7f05000a;
public static final int output=0x7f050001;
public static final int result=0x7f050010;
public static final int seven=0x7f050002;
public static final int six=0x7f050008;
public static final int subtract=0x7f050009;
public static final int three=0x7f05000c;
public static final int two=0x7f05000b;
public static final int zero=0x7f05000e;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int add=0x7f04000e;
public static final int app_name=0x7f040001;
public static final int clear=0x7f040012;
public static final int divide=0x7f040011;
public static final int eight=0x7f04000c;
public static final int five=0x7f040009;
public static final int four=0x7f040008;
public static final int hello=0x7f040000;
public static final int input=0x7f040002;
public static final int multiply=0x7f040010;
public static final int nine=0x7f04000d;
public static final int one=0x7f040005;
public static final int output=0x7f040003;
public static final int result=0x7f040013;
public static final int seven=0x7f04000b;
public static final int six=0x7f04000a;
public static final int subtract=0x7f04000f;
public static final int three=0x7f040007;
public static final int two=0x7f040006;
public static final int zero=0x7f040004;
}
}
标签:android,计算器,原理