Android学习之练笔---计算器的实现
16lz
2021-01-24
刚接触android开发三天,看了一些视频,然后就突发奇想的看看可有啥简单的练笔程序,网上找了个关于计算器实现的demo,因此花了几个小时时间实现了该计算器程序,现将整个过程记载下来,也算是学习android途中的一点体会吧!
1、关于页面设计部分是看着网上的图片然后根据所学知识及网络上解决思路(当然也参考了下别人的设计内容啦)设计出了如附件中的界面【具体见附件的 计算器的实现.png】
起主要代码设计如下【main.xml】:
2、实现了上面的那么多,计算器的实现.png图片基本搞定了,那么接下来就是要编写mainActivity.java的内容了,直接贴上源码
3、至此,一个简单的计算器程序就完成了,当然这个系统还是存在个小bug的,比如:如果数值过大,在显示框内内容无法很好的显示;当你输入的值或者计算后的值恰好等于-0.010101020202030303的时候得先clear一下才能继续使用,因为思想局限性,用这类思想暂未想到好的解决思路,后面随着学习深入的话会回过头来做些优化的(估计得更换实现思路)
4、或许其中还会有隐藏着其他bug暂未查出,如果您发现了,也欢迎指正;
1、关于页面设计部分是看着网上的图片然后根据所学知识及网络上解决思路(当然也参考了下别人的设计内容啦)设计出了如附件中的界面【具体见附件的 计算器的实现.png】
起主要代码设计如下【main.xml】:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="32sp" android:stretchColumns="1" tools:context="${relativePackage}.${activityClass}" ><TableRow> <EditText android:id="@+id/result" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@android:drawable/editbox_background" android:layout_span="4" android:textSize="48sp" android:gravity="right|center_vertical" android:cursorVisible="false" android:inputType="none" android:lines="1" /> </TableRow> <TableRow> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" > <!--android:textSize表示的是该button内数字的的大小--> <Button android:id="@+id/num7" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="32sp" android:text="@string/num_7" android:layout_weight="1" /> <Button android:id="@+id/num8" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="32sp" android:text="@string/num_8" android:layout_weight="1" /> <Button android:id="@+id/num9" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="32sp" android:text="@string/num_9" android:layout_weight="1" /> <Button android:id="@+id/divide" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="32sp" android:text="@string/divide" android:layout_weight="1" /> </LinearLayout> </TableRow> <TableRow> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"> <Button android:id="@+id/num4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="32sp" android:text="@string/num_4" android:layout_weight="1" /> <Button android:id="@+id/num5" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="32sp" android:text="@string/num_5" android:layout_weight="1" /> <Button android:id="@+id/num6" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="32sp" android:text="@string/num_6" android:layout_weight="1" /> <Button android:id="@+id/multiply" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="32sp" android:text="@string/multiply" android:layout_weight="1" /> </LinearLayout> </TableRow> <TableRow> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"> <Button android:id="@+id/num1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="32sp" android:text="@string/num_1" android:layout_weight="1" /> <Button android:id="@+id/num2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="32sp" android:text="@string/num_2" android:layout_weight="1" /> <Button android:id="@+id/num3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="32sp" android:text="@string/num_3" android:layout_weight="1" /> <Button android:id="@+id/subtract" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="32sp" android:text="@string/subtract" android:layout_weight="1" /> </LinearLayout> </TableRow> <TableRow> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"> <Button android:id="@+id/num0" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="32sp" android:text="@string/num_0" android:layout_weight="1" /> <Button android:id="@+id/point" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="32sp" android:text="@string/point" android:layout_weight="1" /> <Button android:id="@+id/add" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="32sp" android:text="@string/add" android:layout_weight="1" /> <Button android:id="@+id/equal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="32sp" android:text="@string/equal" android:layout_weight="1" /> </LinearLayout> </TableRow> <TableRow> <Button android:id="@+id/clear" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="30sp" android:text="@string/clear" android:layout_span="4" android:gravity="center_vertical|center_horizontal"/> </TableRow> </TableLayout>
2、实现了上面的那么多,计算器的实现.png图片基本搞定了,那么接下来就是要编写mainActivity.java的内容了,直接贴上源码
public class MainActivity extends Activity {private StringBuffer btnsBuffer = new StringBuffer("");private double num1 = -0.010101020202030303;private double num2 = -0.010101020202030303;private String operateString;Button[] btnArray = new Button[17];@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);final EditText resultText = (EditText) findViewById(R.id.result);btnArray[0] = (Button) findViewById(R.id.num0);btnArray[1] = (Button) findViewById(R.id.num1);btnArray[2] = (Button) findViewById(R.id.num2);btnArray[3] = (Button) findViewById(R.id.num3);btnArray[4] = (Button) findViewById(R.id.num4);btnArray[5] = (Button) findViewById(R.id.num5);btnArray[6] = (Button) findViewById(R.id.num6);btnArray[7] = (Button) findViewById(R.id.num7);btnArray[8] = (Button) findViewById(R.id.num8);btnArray[9] = (Button) findViewById(R.id.num9);btnArray[10] = (Button) findViewById(R.id.point);btnArray[11] = (Button) findViewById(R.id.add);btnArray[12] = (Button) findViewById(R.id.subtract);btnArray[13] = (Button) findViewById(R.id.multiply);btnArray[14] = (Button) findViewById(R.id.divide);btnArray[15] = (Button) findViewById(R.id.equal);btnArray[16] = (Button) findViewById(R.id.clear);OnClickListener listener = new OnClickListener() {@Overridepublic void onClick(View v) {Button button = (Button) v;String btnString = button.getText().toString();// 如果输入的是 +-*/ 那么就存到operateString中if (("+").equals(btnString) || ("-").equals(btnString)|| ("*").equals(btnString) || ("/").equals(btnString)) {// 如果buffer中内容不为空,那么就将值存入num1中,作为计算的第一个数if (null != btnsBuffer && btnsBuffer.length() > 0)num1 = Double.parseDouble(btnsBuffer.toString());// 如果num1为默认值的话,说明这是非正常输入【不输入数字,直接输入符号情况】if (num1 != -0.010101020202030303)operateString = btnString;btnsBuffer.delete(0, btnsBuffer.length());} else if (("=").equals(btnString)) {if (null != btnsBuffer && !("").equals(btnString))num2 = Double.parseDouble(btnsBuffer.toString());} else if (("clear").equals(btnString)) {num1 = -0.010101020202030303;num2 = -0.010101020202030303;operateString = null;btnsBuffer.delete(0, btnsBuffer.length());resultText.setText(null);} else {btnsBuffer.append(btnString);resultText.setText(btnsBuffer);}if (-0.010101020202030303 != num1&& -0.010101020202030303 != num2&& ((null != operateString) && (!"".equals(operateString)))) {Double result = 0.0d;if ("+".equals(operateString)) {result = num1 + num2;} else if ("-".equals(operateString)) {result = num1 - num2;} else if ("*".equals(operateString)) {result = num1 * num2;} else if ("/".equals(operateString)) {result = num1 / num2;}num1 = result;num2 = -0.010101020202030303;operateString = null;btnsBuffer.delete(0, btnsBuffer.length());resultText.setText(result.toString());}}};for (Button button : btnArray) {button.setOnClickListener(listener);}}}
3、至此,一个简单的计算器程序就完成了,当然这个系统还是存在个小bug的,比如:如果数值过大,在显示框内内容无法很好的显示;当你输入的值或者计算后的值恰好等于-0.010101020202030303的时候得先clear一下才能继续使用,因为思想局限性,用这类思想暂未想到好的解决思路,后面随着学习深入的话会回过头来做些优化的(估计得更换实现思路)
4、或许其中还会有隐藏着其他bug暂未查出,如果您发现了,也欢迎指正;
更多相关文章
- Android换肤的两种思路
- android 双向滑动 稍加改进,可做成QQHD效果
- Android歌词秀设计思路(6)运用Proxy设计模式简化歌词播放服务的使
- Android心得3.4--用Pull解析器解析xml文件内容.doc
- Android(安卓)图形架构
- 调用系统计算器 android(适用于不同品牌)
- strut2服务器与android交互数据
- android 按行读取txt文本内容
- robotium获取本地文档内容