Android开发之计算器
这几天,根据自己学习的进程,制作了一个简单的计算器。也算是,学习这么久,第一个处女作品吧!呵呵,下面跟大家分享下,先来看下,运行效果:
下面,就来跟大家分享下,开发过程及需要注意的事项。
1、 创建Android项目,命名为Calculator;
2、 修改main.xml布局文件,下面我简单的说先文件的布局。在这里我采用了TableLayout布局方式。最上面,我弄了个TextView用于显示计算的结果,接下来就是按钮了!这个写都好简单,到时可以去看下代码。呵呵。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/silver"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="10px"
android:paddingTop="5px"
android:paddingRight="10px">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="*">
<TableRow
android:id="@+id/tableRow01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="1px" >
<TextView
android:id="@+id/result"
android:layout_width="fill_parent"
android:layout_height="40px"
android:text="0.0"
android:textColor="@drawable/black"
android:gravity="right"
android:textSize="18px"/>
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:paddingTop="10px">
<TableRow
android:id="@+id/tableRow02"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""/>
<Button
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff0000ff"
android:text="Back"/>
<Button
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff0000ff"
android:text="Delete"/>
<Button
android:id="@+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff0000ff"
android:text="Clear"/>
</TableRow>
<TableRow
android:id="@+id/tableRow03"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/mc"
android:layout_width="70px"
android:layout_height="wrap_content"
android:textColor="#ffff0000"
android:text="MC"/>
<Button
android:id="@+id/mr"
android:layout_width="70px"
android:layout_height="wrap_content"
android:textColor="#ffff0000"
android:text="MR"/>
<Button
android:id="@+id/mj"
android:layout_width="70px"
android:layout_height="wrap_content"
android:textColor="#ffff0000"
android:text="M-"/>
<Button
android:id="@+id/mz"
android:layout_width="70px"
android:layout_height="wrap_content"
android:textColor="#ffff0000"
android:text="M+"/>
</TableRow>
<TableRow
android:id="@+id/tableRow04"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/qi"
android:layout_width="70px"
android:layout_height="wrap_content"
android:textColor="#ff0000ff"
android:text="7"/>
<Button
android:id="@+id/ba"
android:layout_width="70px"
android:layout_height="wrap_content"
android:textColor="#ff0000ff"
android:text="8"/>
<Button
android:id="@+id/jiu"
android:layout_width="70px"
android:layout_height="wrap_content"
android:textColor="#ff0000ff"
android:text="9"/>
<Button
android:id="@+id/chu"
android:layout_width="70px"
android:layout_height="wrap_content"
android:textColor="#ffff0000"
android:text="/"/>
</TableRow>
<TableRow
android:id="@+id/tableRow05"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/si"
android:layout_width="70px"
android:layout_height="wrap_content"
android:textColor="#ff0000ff"
android:text="4"/>
<Button
android:id="@+id/wu"
android:layout_width="70px"
android:layout_height="wrap_content"
android:textColor="#ff0000ff"
android:text="5"/>
<Button
android:id="@+id/liu"
android:layout_width="70px"
android:layout_height="wrap_content"
android:textColor="#ff0000ff"
android:text="6"/>
<Button
android:id="@+id/cheng"
android:layout_width="70px"
android:layout_height="wrap_content"
android:textColor="#ffff0000"
android:text="*"/>
</TableRow>
<TableRow
android:id="@+id/tableRow06"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/yi"
android:layout_width="70px"
android:layout_height="wrap_content"
android:textColor="#ff0000ff"
android:text="1"/>
<Button
android:id="@+id/er"
android:layout_width="70px"
android:layout_height="wrap_content"
android:textColor="#ff0000ff"
android:text="2"/>
<Button
android:id="@+id/san"
android:layout_width="70px"
android:layout_height="wrap_content"
android:textColor="#ff0000ff"
android:text="3"/>
<Button
android:id="@+id/jian"
android:layout_width="70px"
android:layout_height="wrap_content"
android:textColor="#ffff0000"
android:text="-"/>
</TableRow>
<TableRow
android:id="@+id/tableRow07"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/ling"
android:layout_width="70px"
android:layout_height="wrap_content"
android:textColor="#ff0000ff"
android:text="0"/>
<Button
android:id="@+id/dian"
android:layout_width="70px"
android:layout_height="wrap_content"
android:textColor="#ffff0000"
android:text="."/>
<Button
android:id="@+id/deng"
android:layout_width="70px"
android:layout_height="wrap_content"
android:textColor="#ffff0000"
android:text="="/>
<Button
android:id="@+id/jia"
android:layout_width="70px"
android:layout_height="wrap_content"
android:textColor="#ffff0000"
android:text="+"/>
</TableRow>
</TableLayout>
</LinearLayout>
3、 大家仔细看下,我们最上面用于显示计算结果的TextView有什么不同?对,就是外面多了边框。呵呵,为什么要弄这个边框呢?我感觉,做一个应用,美观方面是一个让人容易接受的重要因素。所以大家不要只求简单!美观是不容忽视的!呵呵,下面我们来讲下,这个边框的实现。
(1) 首先在res/drawable-mdpi目录下新建一个shape.xml文件
(2) 编写shape.xml文件的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#FFFFFF"
android:endColor="#FFFFFF"
android:angle="0"/>
<stroke
android:width="5px"
android:color="#DEDCD2"/>
<padding
android:top="8dp"
android:bottom="8dp"
android:right="8dp"/>
</shape>
在这里我们使用了shap自定义图形,不懂的话,可以看前面我的Android开发之shape自定义图形一文,里面讲的很清楚,在这里,就不啰嗦了。
4、 修改主程序代码,具体代码如下:
public class Calculator extends Activity implements OnClickListener{
public float result; // 计算结果
TextView showResult;//显示计算的结果
Button[] number;//数字数字包含点
Button[] command;//符号数组
Button BackButton,DeleteButton,ClearButton;//操作按钮
Button MCButton,MRButton,MPButton,MMButton;
public String lastCommand; // 用于保存运算符
public boolean clearFlag; // 用于判断是否清空显示区域的值,true需要,false不需要
public boolean firstFlag; // 用于判断是否是首次输入,true首次,false不是首次
public Calculator(){//初始化变量
result=0;
clearFlag=false;
firstFlag=true;
lastCommand="=";}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
showResult=(TextView)findViewById(R.id.result);//实例化显示计算结果TextView
showResult.setBackgroundResource(R.drawable.shape);//设置背景,让它有边框
//数字数组
number=new Button[11];
number[0]=(Button)findViewById(R.id.ling);
number[1]=(Button)findViewById(R.id.yi);
number[2]=(Button)findViewById(R.id.er);
number[3]=(Button)findViewById(R.id.san);
number[4]=(Button)findViewById(R.id.si);
number[5]=(Button)findViewById(R.id.wu);
number[6]=(Button)findViewById(R.id.liu);
number[7]=(Button)findViewById(R.id.qi);
number[8]=(Button)findViewById(R.id.ba);
number[9]=(Button)findViewById(R.id.jiu);
number[10]=(Button)findViewById(R.id.dian);
//运算符数组
command=new Button[5];
command[0]=(Button)findViewById(R.id.jia);
command[1]=(Button)findViewById(R.id.jian);
command[2]=(Button)findViewById(R.id.cheng);
command[3]=(Button)findViewById(R.id.chu);
command[4]=(Button)findViewById(R.id.deng);
//为每个数字按钮注册按钮单击事件
NumberAction na=new NumberAction();
for(Button bc:number){
bc.setOnClickListener(na);}
//为每个操作符号按钮注册按钮单击事件
CommandAction ca=new CommandAction();
for(Button bc:command){
bc.setOnClickListener(ca);}
//实例化按钮
ClearButton=(Button)findViewById(R.id.clear);
BackButton=(Button)findViewById(R.id.back);
DeleteButton=(Button)findViewById(R.id.delete);
//为按钮注册单击事件
ClearButton.setOnClickListener(this);
BackButton.setOnClickListener(this);
DeleteButton.setOnClickListener(this);
}
public void calculate(double x){ //计算函数
if(lastCommand.equals("+"))
{
result+=x;
}
else if(lastCommand.equals("-"))
{
result-=x;
}
else if(lastCommand.equals("*"))
{
result*=x;
}
else if(lastCommand.equals("/"))
{
result/=x;
}
showResult.setText(""+result);
}
public void onClick(View v) {//按钮单击处理
switch(v.getId()){
case R.id.clear://当单击了Clear按钮后
showResult.setText("0.0");
result=0;
clearFlag=false;
firstFlag=true;
lastCommand="=";
break;
}
}
private class NumberAction implements OnClickListener{//数字事件注册类
public void onClick(View v) {
Button btn=(Button)v;
String input=btn.getText().toString();//取得单击该按钮的值
if(firstFlag){//如果是第一次输入
if(input.equals(".")){//判断如果第一次输入是".",则不做任何事
return;
}
if(showResult.getText().toString().equals("0.0")){
showResult.setText("");
}
result=Float.parseFloat(input);
firstFlag=false;
}
else{//如果不是第一次输入
String text=showResult.getText().toString();//取得先前的内容
//判断内容中是否有“.”,如果有,下一步输入的又是".",则不做什么
if(text.indexOf(".")!=-1 && input.equals(".")){
return;
}
//判断内容是不是等于"=",如果是,并且即将输入的是".",则不允许
if(text.equals("-") && input.equals(".")){
return;
}
//判断内容是不是等于"=",如果是,并且即将输入的不是".",则把输入的值赋给result,比如result=-6;
if(text.equals("-") && !input.equals(".")){
result=Float.parseFloat("-"+input);
}
//判断内容是不是等于"0",如果是,并且即将输入的不是".",则不允许
if(text.equals("0") && !input.equals(".")){
return;
}
}
if(clearFlag){
showResult.setText("");
clearFlag=false;
}
showResult.setText(showResult.getText().toString()+input);//显示内容
}
}
private class CommandAction implements OnClickListener{//符号操作等按钮的注册事件
public void onClick(View v) {
Button btn=(Button)v;
String inputCommand=(String)btn.getText();//取得按钮上的值
if(firstFlag){//判断是否是第一次输入
if(inputCommand.equals("-")){//如果是,如果输入的是"-"
showResult.setText("-");//则把showResult内容设置为"-"
firstFlag=false;
}
}
else{//如果不是第一次输入
if(!clearFlag){
calculate(Double.parseDouble(showResult.getText().toString()));
}
lastCommand=inputCommand;
clearFlag=true;
}
}
}
}
5、 到此,整个项目已开发完毕。单击运行便会得到以上效果。
程序的操作,在这里说下,单击数字按钮,可以输入数字,单击运算符,可以进行相关运算,然后就是单击Clear按钮,可以清除先前的数据,从头开始计算。在这里我有必要说一下。这个项目,作为计算器,基本内容都已实现。只是还有一些单击按钮事件,还没处理!还有就是程序本身还存在一些不足之处,比如在处理小数运算时,呵呵,在这里就卖个官司。就当给大家一个课后作业。呵呵!(其实我也还没开发)嘿嘿。好了,今天,就到这里。下次见!