android计算器布局及逻辑,Android计算器简单逻辑实现实例分享

package com.example.androidlessontwo;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

public class MainActivity extends Activity  {

private Button[] buttonNum=new Button[11];

private Button[] buttonComand=new Button[5];

private TextView input=null;

private TextView rl=null;

private Button   buttonClear=null;

private boolean firstFlag=true;

private double result=0.0;

private String lastCommand;

public void MyCalculator()

{

result = 0.0;

firstFlag=true;

lastCommand="=";

}

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

buttonNum[0]=(Button) findViewById(R.id.num0);

buttonNum[1]=(Button) findViewById(R.id.num1);

buttonNum[2]=(Button) findViewById(R.id.num2);

buttonNum[3]=(Button) findViewById(R.id.num3);

buttonNum[4]=(Button) findViewById(R.id.num4);

buttonNum[5]=(Button) findViewById(R.id.num5);

buttonNum[6]=(Button) findViewById(R.id.num6);

buttonNum[7]=(Button) findViewById(R.id.num7);

buttonNum[8]=(Button) findViewById(R.id.num8);

buttonNum[9]=(Button) findViewById(R.id.num9);

buttonNum[10]=(Button) findViewById(R.id.point);

buttonComand[0]=(Button) findViewById(R.id.add);

buttonComand[1]=(Button) findViewById(R.id.sub);

buttonComand[2]=(Button) findViewById(R.id.ride);

buttonComand[3]=(Button) findViewById(R.id.divide);

buttonComand[4]=(Button) findViewById(R.id.equal);

input=(TextView) findViewById(R.id.input);

rl   =(TextView) findViewById(R.id.rl);

buttonClear=(Button) findViewById(R.id.clean);

NumberAction na= new NumberAction();

CommandAction ca=new CommandAction();

for(Button bc:buttonComand)

{

bc.setOnClickListener(ca);

}

for(Button bc:buttonNum)

{

bc.setOnClickListener(na);

}

buttonClear.setOnClickListener(new Button.OnClickListener()

{

@Override

public void onClick(View v) {

MyCalculator();

rl.setText("0.0");

}

});

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

private class NumberAction implements Button.OnClickListener

{

@Override

public void onClick(View view)

{

Button btn = (Button)view;

String inputTemp =btn.getText().toString();//6

input.setText(input.getText().toString()+inputTemp);

double numtemp = 0;

switch(btn.getId())

{

case R.id.num0:

{

if(firstFlag)

{

result=result*10+0;

firstFlag=false;

}

else

numtemp=numtemp*10+0;

break;

}

case R.id.num1:

{

if(firstFlag)

{

result=result*10+1;

firstFlag=false;

}

else

numtemp=numtemp*10+1;

break;

}

case R.id.num2:

{

if(firstFlag)

{

result=result*10+2;

firstFlag=false;

}

else

numtemp=numtemp*10+2;

break;

}

case R.id.num3:

{

if(firstFlag)

{

result=result*10+3;

firstFlag=false;

}

else

numtemp=numtemp*10+3;

break;

}

case R.id.num4:

{

if(firstFlag)

{

result=result*10+4;

firstFlag=false;

}

else

numtemp=numtemp*10+4;

break;

}

case R.id.num5:

{

if(firstFlag)

{

result=result*10+5;

firstFlag=false;

}

else

numtemp=numtemp*10+5;

break;

}

case R.id.num6:

{

if(firstFlag)

{

result=result*10+6;

firstFlag=false;

}

else

{

numtemp=numtemp*10+6;

calculate(numtemp);

}

break;

}

case R.id.num7:

{

if(firstFlag)

{

result=result*10+7;

firstFlag=false;

}

else

{

numtemp=numtemp*10+7;

calculate(numtemp);

}

break;

}

case R.id.num8:

{

if(firstFlag)

{

result=result*10+8;

{

result=result*10+8;

firstFlag=false;

}

}

else

{

numtemp=numtemp*10+8;

calculate(numtemp);

}

break;

}

case R.id.num9:

{

if(firstFlag)

{

result=result*10+9;

firstFlag=false;

}

else

{

numtemp=numtemp*10+9;

calculate(numtemp);

}

break;

}

}

}

}

private class CommandAction implements Button.OnClickListener

{

@Override

public void onClick(View v)

{

Button btn=(Button)v;

String inputCommand=(String)btn.getText();

switch(btn.getId())

{

case R.id.add:

{

lastCommand="+";

break;

}

case R.id.sub:

{

lastCommand="-";

break;

}

case R.id.ride:

{

lastCommand="*";

break;

}

case R.id.divide:

{

lastCommand="/";

break;

}

case R.id.equal:

{

lastCommand="=";

input.setText("");

rl.setText(String.valueOf(result));

return ;

}

}

input.setText(input.getText()+inputCommand);

}

}

private void calculate(double x)

{

if(lastCommand.equals("+"))

{

result += x;

}

if(lastCommand.equals("-"))

{

result -= x;

}

if(lastCommand.equals("*"))

{

result *= x;

}

if(lastCommand.equals("/"))

{

result /= x;

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值