Android计算器简单逻辑实现实例分享
引言:
我的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;
}
}
}
上一篇: Android zip文件下载和解压实例
下一篇: Android获取应用程序大小的方法