id=”+id/txt_input”android:
layout_width=H240dpn
android:
layout_height="wrap_content"androidgravity="right"android:
text="string/zero"
/>
LinearLayout是线性布局控件,它包含的子控件将以横向或竖向的方式排列,按照相对位置来排列所有的widgets或者其他的containers,超过边界时,某些控件将缺失或消失。
android:
id=n+id/txt_inpuf,〃定义了布局的变量名称为txtjnput,会自动写进R・java;在R.Java文件中会生成内部类id,可在主程序里面调用R.id.txtJnput来获取这个布局变量实体。
android:
orientation="verticar'//这个布局是垂直布局;(水平horizontal)
android:
layout_width=”fill_parent”
〃设置一个顶部布局或控件强制性让它布满整个屏幕;
(nwrap_contentH设置一个顶部布局或控件强制性让它布满整个屏幕)android:
gravity=”right”〃限定里面的内容向右对齐;
(android:
layout_gravity=,'nghtn表示在界面上的位置向右对齐)
用XML实现用户界面具有以下优点:
a)XML文件结构清晰,易于扩展,系统可扩展性好。
b)用户界面与业务逻辑完全分离,降低了系统界面与业务逻辑的耦合性。
c)XML文件简单易懂,可维护性好。
2计算器控制逻辑设计与实现代码:
CountMain.java
用户在输入过程中,肯定会存在一定的错误,程序设计过程中需要对应用程序进行
操作符不能连续
容错处理。
容错设计比如:
在输入过程中小数的话只能有一个小数点,
输入,整数数据的开头不能出现0等等。
所以设计了如下代码:
//数字监听
publicvoidxianshi(finalButtonbut,finalEditTextt1){but.setOnClickListener(newOnClickListener(){publicvoidonClick(Viewv){//判断开始是否可以按操作符
if(but.getText().toString()・equals(T)){
if(strBuff.indexOf(”+”)!
=-1
||strBuff.indexOf(”・”)!
=-1
||strBuff.indexOf伫‘)!
=-1
||strBuff.indexOf(T)!
=-1)
{strBuff.append(but.getText().toString());
t1.setText(strBuff);
//判断一个操作数中是否可以按多个点
}else{
if(strBuff.indexOf(".n)==-1&&strBuff.length()!
=
strBuff.append(but.getText().toString());
t1.setText(strBuff);
//判断在什么情况下可以按多个零
}elseif(but・getText().toString().equals(”0”)){
if(strBuff.indexOf("On)==0&&strBuff.indexOf(n.H)==〔){
strBuff.append(but.getText().toString());t1.setText(strBuff);
}elseif(strBuff.length()==0){strBuff.append(but.getText().toString());t1.setText(strBuff);
〃判断第一个按零,接着再按整数的情况时,屏幕的显示
}elseif(strBuff.indexOf(HOn)==0
&&strBuff.indexOf(n.n)==-1){strBuff.delete(O,strBuff.length());strBuff.append(but.getText().toString());t1.setText(strBuff);
}else{strBuff.append(but.getText().toString());t1.setText(strBuff);
});
}
运算部分代码:
//符号监听
publicvoidjisuan(finalButtonbut1,finalEditTextt1){but1・setOnClickListener(newOnClickListener(){
publicvoidonClick(Viewv){
//存放输入的字符
Stringnewcharl=but1.getText()・toString();
〃判断开始是否为等号,输入一个数字后在输入等号没有反映
Stringnewchar=
if(!
(newchar1.equals(,'=,))){
newchar=newcharl;
}
floatperl=0;//用来存放截取字符串的第一个操作数
floatper2=0;//用来存放截取字符串的第二个操作数
//判断按的字符是否为清除键
if(newchar.equalslgnoreCase("cH)){strBuff.delete(O,strBuff.length());t1.setText(n0n);return;
}
〃判断按的操作符是什么计算字符
〃判断是否为加法
if(strBuff.indexOf(”+”)!
=-1
&&strBuff.indexOf(”+”)&&strBuff.indexOf(”+”)!
=0){
inti=strBuff.indexOf(H4-n);
perl=Float.parseFloat(strBuff.substring(0,i));
per2=Float.parseFloat(strBuff.substring(i+1,strBuff.length()));floatsum=perl+per2;
strBuff.delete(O,strBuff」ength());strBuff.append(sum);
strBuff.append(newchar);
t1.setText(strBuff);
〃判断是否为减法
}elseif(strBuff.indexOf(”■”)!
=-1
&&strBuff.indexOf(”■”)&&strBuff.indexOf(”■”)!
=0){
inti=strBuff.indexOf(H-n);
perl=Float.parseFloat(strBuff.substring(0,i));
per2=Float.parseFloat(strBuff.substring(i+1,strBuff.length()));
floatsum=perl・per2;strBuff.delete(O,strBuff.length());
strBuff.append(sum);strBuff.append(newchar);
t1.setText(strBuff);
〃判断是否为乘法
}elseif(strBuff.indexOf(“”)!
=-1
&&strBuff.indexOf(',*n)&&strBuff.indexOf(”★”)!
=0){
inti=strBuff.indexOf(n*n);
perl=Float.parseFloat(strBuff.substring(0,i));
per2=Float.parseFloat(strBuff.substring(i+1,strBuff.length()));floatsum=perl*per2;
strBuff.delete(0,strBuff.length());strBuff.append(sum);
strBuff.append(newchar);
t1.setText(strBuff);
〃判断是否为除法
}elseif(strBuff.indexOf(Vn)!
=-1
&&strBuff.indexOf('7H)&&strBuff.indexOf('7n)!
=0){
inti=strBuff.indexOf(V");
perl=Float.parseFloat(strBuff.substring(0,i));
per2=Float.parseFloat(strBuff.substring(i+1,strBuff.length()));
〃判断第二个操作数是否为零,为零则把显示框清零if(per2!
=0){floatsum=perl/per2;
strBuff.delete(O,strBuff」ength());strBuff.append(sum);strBuff.append(newchar);
t1.setText(strBuff);
}else{
strBuff.delete(0,strBuff.length());
strBuff.append(n0n);
t1.setText(strBuff);
}
}else{
//判断操作符是否能接连不断的点击
if(strBuff.length()>0
&&('0*<=strBuff.charAt(strBuff.length()・1))
&&(9>=strBuff.charAt(strBuff.length()・1))){strBuff.append(newchar);
t1.setText(strBuff);
}
)
}
});
}
Override
publicvoidonCreate(BundlesavedInstanceState){//这是Activity建立的时候一定会调用的onCreate()方法,第一行是声明
super.onCreate(savedlnstanceState);//然后是继承原来的onCreate()方法setContentView(R.layout.main);//然后是载入main这个视图,一般来说是在XML里定义的//下面为变量赋值
finalEditTextt1=(EditText)findViewByld(R.id.txt_input);
finalButtonbut1=(Button)findViewByld(R.id.but1);
xianshi(but1,t1);
finalButtonbut2=(Button)findViewByld(R.id.but2);
〃符号
finalButtonbut4=(Button)findViewByld(R.id.but4);
jisuan(but4,t1);
6、设计总结
到现在为止,系统的设计和开发已基本完成。
本系统的功能基本符合了普通用户对于计算器的要求,整个系统界面良好,简单明了,使用起来十分方便,具有普通计算器的基本功能。
整个过程中,从总体架构的构思,到最后的完成都投入了大量的时间和精力,对系统中的每个功能都进行了反复的调试,修改。
由于是第一次独立的完成软件的开发,不论是在知识层面还是经验方面都有着很大的欠缺和不足,因此本系统难免会有一些不尽如人意的地方,但是由于时间的关系,只能在日后再进行完善了。