android计算器设计.docx

上传人:b****2 文档编号:1952079 上传时间:2023-05-02 格式:DOCX 页数:55 大小:233.42KB
下载 相关 举报
android计算器设计.docx_第1页
第1页 / 共55页
android计算器设计.docx_第2页
第2页 / 共55页
android计算器设计.docx_第3页
第3页 / 共55页
android计算器设计.docx_第4页
第4页 / 共55页
android计算器设计.docx_第5页
第5页 / 共55页
android计算器设计.docx_第6页
第6页 / 共55页
android计算器设计.docx_第7页
第7页 / 共55页
android计算器设计.docx_第8页
第8页 / 共55页
android计算器设计.docx_第9页
第9页 / 共55页
android计算器设计.docx_第10页
第10页 / 共55页
android计算器设计.docx_第11页
第11页 / 共55页
android计算器设计.docx_第12页
第12页 / 共55页
android计算器设计.docx_第13页
第13页 / 共55页
android计算器设计.docx_第14页
第14页 / 共55页
android计算器设计.docx_第15页
第15页 / 共55页
android计算器设计.docx_第16页
第16页 / 共55页
android计算器设计.docx_第17页
第17页 / 共55页
android计算器设计.docx_第18页
第18页 / 共55页
android计算器设计.docx_第19页
第19页 / 共55页
android计算器设计.docx_第20页
第20页 / 共55页
亲,该文档总共55页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

android计算器设计.docx

《android计算器设计.docx》由会员分享,可在线阅读,更多相关《android计算器设计.docx(55页珍藏版)》请在冰点文库上搜索。

android计算器设计.docx

android计算器设计

 

android计算器设计

 

本人学习android有一个多月了,在学习期间为了对所学的知识进行应用,因而设计了一个简单的计算器,现在通过本文与大家一起分享。

由于本人水平有限,因此设计的计算器可能有着一部分的BUG,如发现BUG请联系我,谢谢!

QQ:

474027915

 

1.界面的设计

1.1应用程序图标的设计

每一个应用程序,我们都要设计一个图标,可以说图标是应用程序的名片,好的图标能够很快地吸引用户。

关于如何设计图标,在网上可以查询相关文件进行学习,在这不多做讲述,本人在网上寻找了一个图标作为计算器应用的图标,图标如下所示。

由于不同的android手机有着不同的分辨率,将不同分辨率的图标图片放置在相应位置,如下图所示。

drawable-hdpi中放置分辨率最高的图标,drawable-ldpi中放置分辨率中等的图标,drawable-hdpi中放置分辨率最低的图标。

图标图片大小的要求如下:

在AndroidManifest.xml文件中进行修改,修改情况如下:

icon="@drawable/calculator"android:

label="@string/app_name">

其他设置保持不变。

在value中的String.xml中加入app_name,代码为:

计算器

结果如下图所示:

1.2按钮放置以及界面编排

界面设计应该尽量简洁而美观,应该具有良好的交互性。

因本人水平有限,所以设计的界面不是很完美,望请见谅。

最终设计的计算器界面情况如下所示:

整个界面的布局为LinearLayout,按钮的布局为TableLayout。

另外需要对EditText和Button的一些属性进行相关设置,以使得它们满足我们的需要。

在这直接给出main.xml中的代码,代码如下所示:

xmlversion="1.0"encoding="utf-8"?

>

android="

android:

orientation="vertical"

android:

layout_width="fill_parent"

android:

layout_height="fill_parent"

android:

background="#880000"

android:

paddingTop="50px"

>

android:

id="@+id/editText"

android:

gravity="left"

android:

text=""

android:

textSize="37px"

android:

layout_height="150px"

android:

layout_width="fill_parent"

android:

layout_marginBottom="20px"

android:

layout_marginLeft="10px"

android:

layout_marginRight="10px"

android:

editable="false"

android:

cursorVisible="false"

>

android:

id="@+id/editText1"

android:

gravity="left"

android:

text=""

android:

textSize="40px"

android:

layout_height="80px"

android:

layout_width="fill_parent"

android:

layout_marginBottom="30px"

android:

layout_marginLeft="10px"

android:

layout_marginRight="10px"

android:

editable="false"

android:

cursorVisible="false"

android:

focusable="false"

>

android:

layout_width="fill_parent"

android:

layout_height="fill_parent"

android:

stretchColumns="0,1,2,3,4"

>

android:

id="@+id/buttom7"

android:

layout_margin="3px"

android:

text="7"

android:

textSize="40px"

android:

focusable="false"

/>

android:

id="@+id/buttom8"

android:

layout_margin="3px"

android:

text="8"

android:

textSize="40px"

android:

focusable="false"

/>

android:

id="@+id/buttom9"

android:

layout_margin="3px"

android:

text="9"

android:

textSize="40px"

android:

focusable="false"

/>

android:

id="@+id/buttom10"

android:

layout_margin="3px"

android:

text="ON"

android:

textSize="40px"

android:

focusable="false"

/>

android:

id="@+id/buttom11"

android:

layout_margin="3px"

android:

text="OFF"

android:

textSize="40px"

android:

focusable="false"

/>

android:

id="@+id/buttom4"

android:

layout_margin="3px"

android:

text="4"

android:

textSize="40px"

android:

focusable="false"

/>

android:

id="@+id/buttom5"

android:

layout_margin="3px"

android:

text="5"

android:

textSize="40px"

android:

focusable="false"

/>

android:

id="@+id/buttom6"

android:

layout_margin="3px"

android:

text="6"

android:

textSize="40px"

android:

focusable="false"

/>

android:

id="@+id/buttom12"

android:

layout_margin="3px"

android:

text="+"

android:

textSize="40px"

android:

focusable="false"

/>

android:

id="@+id/buttom13"

android:

layout_margin="3px"

android:

text="-"

android:

textSize="40px"

android:

focusable="false"

/>

android:

id="@+id/buttom1"

android:

layout_margin="3px"

android:

text="1"

android:

textSize="40px"

android:

focusable="false"

/>

android:

id="@+id/buttom2"

android:

layout_margin="3px"

android:

text="2"

android:

textSize="40px"

android:

focusable="false"

/>

android:

id="@+id/buttom3"

android:

layout_margin="3px"

android:

text="3"

android:

textSize="40px"

android:

focusable="false"

/>

android:

id="@+id/buttom14"

android:

layout_margin="3px"

android:

text="×"

android:

textSize="40px"

android:

focusable="false"

/>

android:

id="@+id/buttom15"

android:

layout_margin="3px"

android:

text="÷"

android:

textSize="40px"

android:

focusable="false"

/>

android:

id="@+id/buttom0"

android:

layout_margin="3px"

android:

text="0"

android:

textSize="40px"

android:

focusable="false"

/>

android:

id="@+id/buttom16"

android:

layout_margin="3px"

android:

text="."

android:

textSize="40px"

android:

focusable="false"

/>

android:

id="@+id/buttom17"

android:

layout_margin="3px"

android:

text="="

android:

textSize="40px"

android:

focusable="false"

/>

android:

id="@+id/buttomback"

android:

layout_margin="3px"

android:

text="←"

android:

textSize="40px"

android:

focusable="false"

/>

android:

id="@+id/buttom18"

android:

layout_margin="3px"

android:

text="C"

android:

textSize="40px"

android:

focusable="false"

/>

若对相关控件的属性不够熟悉的话,可自行查阅相关文献。

2.设计思想

2.1整体思想

在整个过程中,首先应该对输入进行容错处理,这样有利于提高用户体验。

对于用户的按键输入,需要及时地显示在第一个EditView中。

在EditView中显示的是一个字符串,用户每输入一个操作,字符串发生相应改变,然后在EditView中反映出来。

接着便是对第一个EditView中的字符串进行处理。

通过程序设计,可以将字符串中的数据和操作符分别存在两个栈中。

然后需要设计算法处理两个栈,通过算法得出字符串表达式的运算结果,再将运算结果反映在第二个EditView中的字符串中。

(注:

数据和操作符也可以储存在队列中,实际上储存在队列中更为合理。

2.2容错处理

用户在输入过程中,肯定会存在一定的错误,程序设计过程中需要对应用程序进行容错处理。

容错设计比如:

在输入过程中小数的话只能有一个小数点,操作符不能连续输入,整数数据的开头不能出现0等等。

在设计的应用程序中,设计了几个标记flag,每按下一个按键,若是按键是合理的话,都会引起标记flag的改变。

标记flag将会限制接下来的输入是否有效,无效则不做任何处理。

flag0=0:

处理0的输出,为1则表示数字的开头有一个0。

当flag0=1时,不能再输入0,当再输入数字或者操作符时,flag0重新置0。

flag1=0:

处理0的输出,每个数字开头的标志数值为0表示为数字开头。

当flag1=0时,如果先输入0再输入1的话,前面的0自动消除。

还有当flag1=0时,不能输入小数点等等。

flag2=0:

处理小数点,一个数字最多只能有一个小数点。

当数字中有小数点时,flag2变为1,此后不能再输入小数点,输入操作符等操作后,flag2又变为0。

flag3=0:

处理运算符的输出。

很明显不能连续输入操作符,用此标志,来限制连续操作符的输入。

flag4=0:

处理ON/OFF问题。

当OFF之后,flag变为1,只有当flag等于1时,ON按钮才能进行操作。

flag5=0:

处理等号问题。

很明显操作符之后不能按下等号按钮,等号按钮按下之后要在第二个EditView中显示结果、将显示输出的EditView清空。

2.3数据结构的设计

在程序设计过程中,需要设计数据结构来存储数字、操作符和容错标记,在本文中使用了栈,对这些数据进行了存储。

容错标记flag的存储结构如下所示:

classFlag

{

privateinta0;

privateinta1;

privateinta2;

privateinta3;

publicinta0()

{returna0;}

publicinta1()

{returna1;}

publicinta2()

{returna2;}

publicinta3()

{returna3;}

publicFlag(inta,intb,intc,intd)

{

a0=a;

a1=b;

a2=c;

a3=d;

}

publicFlagnext;

}

classFlagList

{

privateFlagfirst;

publicFlagList(inta,intb,intc,intd)

{first=null;}

publicbooleanisEmpty()

{return(first==null);}

publicvoidinsertFirst(inta,intb,intc,intd)

{

FlagnewFlag=newFlag(a,b,c,d);

newFlag.next=first;

first=newFlag;

}

publicFlagdeleteFirst()

{

Flagtemp=first;

first=first.next;

returntemp;

}

}

classFlagStack

{

privateFlagListtheFlagList;

privateintcount;

publicFlagStack()

{

theFlagList=newFlagList(0,0,0,0);

count=0;

}

publicvoidpush(inta,intb,intc,intd)

{

theFlagList.insertFirst(a,b,c,d);

count++;

}

publicFlagpop()

{

count=count-1;

returntheFlagList.deleteFirst();

}

publicbooleanisEmpty()

{

return(theFlagList.isEmpty());

}

publicintcount()

{

returncount;

}

}

数字存储的形式如下所示:

classNum

{

privateStrings=newString();

publicNum(Strings1)

{

s=s1;

}

publicStringgetNum()

{

returns;

}

publicNumnext;

}

classNumList

{

privateNumfirstNum;

publicNumList()

{firstNum=null;}

publicbooleanisEmpty()

{return(firstNum==null);}

publicvoidinsertFirst(Strings1)

{

NumnewNum=newNum(s1);

newNum.next=firstNum;

firstNum=newNum;

}

publicNumdeleteFirst()

{

Numtemp=firstNum;

firstNum=firstNum.next;

returntemp;

}

}

classNumStack

{

privateNumListtheNumList;

privateintcountNum;

publicNumStack()

{

theNumList=newNumList();

countNum=0;

}

publicvoidpush(Strings1)

{

theNumList.insertFirst(s1);

countNum++;

}

publicNumpop()

{

countNum=countNum-1;

returntheNumList.deleteFirst();

}

publicbooleanisEmpty()

{

return(theNumList.isEmpty());

}

publicintcountNum()

{

returncountNum;

}

}

操作符存储的形式如下所示:

classSymbol

{

privatecharc;

privateintdegree1;

publicSymbol(charc1)

{

c=c1;

if(c=='×'||c=='÷')

degree1=2;

else

degree1=1;

}

publicintgetDegree()

{

returndegree1;

}

publicchargetSymbol()

{

returnc;

}

publicSymbolnext;

}

classSymbolList

{

privateSymbolfirstSymbol;

publicSymbolList()

{firstSymbol=null;}

publicbooleanisEmpty()

{return(firstSymbol==null);}

publicvoidinsertFirst(charc1)

{

SymbolnewSymbol=newSymbol(c1);

newSymbol.next=firstSymbol;

firstSymbol=newSymbol;

}

publicSymboldeleteFirst()

{

Symboltemp=firstSymbol;

firstSymbol=firstSymbol.next;

returntemp;

}

}

classSymbolStack

{

privateSymbolListtheSymbolList;

privateintcountSymbol;

publicSymbolStack()

{

theSymbolList=newSymbolList();

countSymbol=0;

}

publicvoidpush(charc1)

{

theSymbolList.insertFirst(c1);

countSymbol++;

}

publicSymbolpop()

{

countSymbol=countSymbol-1;

returntheSymbolList.deleteFirst();

}

publicbooleanisEmpty()

{

return(theSymbolList.isEmpty());

}

publicintcountSymbol()

{

return

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 解决方案 > 学习计划

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2