当前位置: 代码迷 >> Android >> Android学习之练笔-计算器的实现
  详细解决方案

Android学习之练笔-计算器的实现

热度:71   发布时间:2016-04-27 23:24:35.0
Android学习之练笔---计算器的实现
刚接触android开发三天,看了一些视频,然后就突发奇想的看看可有啥简单的练笔程序,网上找了个关于计算器实现的demo,因此花了几个小时时间实现了该计算器程序,现将整个过程记载下来,也算是学习android途中的一点体会吧!
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];	@Override	public 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() {			@Override			public 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暂未查出,如果您发现了,也欢迎指正;
  相关解决方案