目的:采用网格布局,实现计算器的简单功能
开发工具:Android Studio3.3.2
开发环境:
实现过程:
- 第一步,创建一个Android项目,在这我把项目命名为MyApplication。
- 第二步,打开MyApplication/app/src/main/res/layout/activity_main.xml,使用text模式编写代码。
-
使用网格布局(GridLayout)把区域划分为6行4列。
-
第一行设置为显示数据的文本标签<TextView/>,第二行为清除数据的按钮,第三到六行均划分为4列,共16个按钮,分别代表0到9以及加、减、乘、除、等号,此外给每一个按钮添加id ,后续用来绑定按钮事件。
-
xml文件代码如下,写完之后,可以先run一下,看一下布局效果。
-
<?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:rowCount="6" android:columnCount="4" > <!--设置网格为6行4列--> <TextView android:id="@+id/a_edit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_columnSpan = "4" android:layout_marginLeft="4px" android:gravity="left" android:text="0" android:textSize="50dip" /> <!--组件占据4列,且左对齐,设置左边距为4px--> <Button android:id="@+id/btn_clear" android:layout_width="354dp" android:layout_height="wrap_content" android:layout_columnSpan="4" android:text="清除" android:textSize="26sp" /><!--组件占据4列--> <Button android:text ="1" android:id="@+id/btn_one" android:textSize="26sp"/> <Button android:text ="2" android:id="@+id/btn_two" android:textSize="26sp"/> <Button android:text ="3" android:id="@+id/btn_three" android:textSize="26sp"/> <Button android:text ="+" android:id="@+id/btn_add" android:textSize="26sp"/> <Button android:text ="4" android:id="@+id/btn_four" android:textSize="26sp"/> <Button android:text ="5" android:id="@+id/btn_five" android:textSize="26sp"/> <Button android:text ="6" android:id="@+id/btn_six" android:textSize="26sp"/> <Button android:text ="-" android:id="@+id/btn_sub" android:textSize="26sp"/> <Button android:text ="7" android:id="@+id/btn_seven" android:textSize="26sp"/> <Button android:text ="8" android:id="@+id/btn_eight" android:textSize="26sp"/> <Button android:text ="9" android:id="@+id/btn_nine" android:textSize="26sp"/> <Button android:text ="*" android:id="@+id/btn_multiply" android:textSize="26sp"/> <Button android:text ="." android:id="@+id/btn_point" android:textSize="26sp"/> <Button android:text ="0" android:id="@+id/btn_zero" android:textSize="26sp"/> <Button android:text ="=" android:id="@+id/btn_equal" android:textSize="26sp"/> <Button android:text ="/" android:id="@+id/btn_divide" android:textSize="26sp"/> </GridLayout>
- 第三步,这部分主要是处理计算器的点击事件,也是计算器的核心部分
- 打开MainActiviity.java,在MainActivity中开始编写Java
- 处理数字类按钮的点击事件:首先是要定义布局中所有的按钮和输入框,然后以手机中的计算器为例,可知用户点击数字按钮时,显示框会显示用户输入的数字,而且需要做计算就需要保存用户输入的数字。也就是是说,数字按钮的事件就是获取到用户点击按钮所对应的数字,并存储显示,小数点的按钮同理。
- 明确数字按钮的功能之后,就分析一下加减乘除。运算符就比较有意思一点,因为用户可能会做连续运算。情况一是:输入一个数字之后按下运算符按钮,此时就直接存储一下用户所点击的符号即可,案例1+2=3。情况二:用户已经计算过一次,即已经输入"=",也就是已经有一个运算结果了,此时再输入符号就表示想要在原来结果的基础上做运算,这个时候我们就要将上一次的运算结果(str_result)作为该次运算的第一个数字,案例4+6=10-2=8。情况三:和情况二类似,用户想计算两个或两个以上的数字,此时就要设置一个标记来判断用户是否已经输过符号,如果已经输过,就先计算结果并显示,然后再记录这次输入的运算符(1+3)*3=12【注:这里没有做运算符优先级处理,默认按顺序计算结果。
- 具体实现代码如下
package com.example.myapplication; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity implements View.OnClickListener { //声明对象 private Button bt_zero; private Button bt_one; private Button bt_two; private Button bt_three; private Button bt_four; private Button bt_five; private Button bt_six; private Button bt_seven; private Button bt_eight; private Button bt_nine; private Button bt_multiply; private Button bt_divide; private Button bt_sub; private Button bt_add; private Button bt_point; private Button bt_equal; private Button bt_clear; private TextView et_edit; private StringBuffer str_display = new StringBuffer(); //用来存放用户输入 private boolean flag = true; //是否输入小数点 private String str_oper = "+";//存放输入的符号 private String str_result = "0"; //存放计算结果 private double num1 = 0; //存放符号前的数字 private double num2 = 0; //存放符号后的数字; private boolean e_flag = false; //是否已经按等号 private boolean s_flag = false; //是否已经按符号 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //与用户界面程序建立组件关联【实例化对象】 bt_zero = (Button) findViewById(R.id.btn_zero); bt_one = (Button) findViewById(R.id.btn_one); bt_two = (Button) findViewById(R.id.btn_two); bt_three = (Button) findViewById(R.id.btn_three); bt_four = (Button) findViewById(R.id.btn_four); bt_five = (Button) findViewById(R.id.btn_five); bt_six = (Button) findViewById(R.id.btn_six); bt_seven = (Button) findViewById(R.id.btn_seven); bt_eight = (Button) findViewById(R.id.btn_eight); bt_nine = (Button) findViewById(R.id.btn_nine); bt_multiply = (Button) findViewById(R.id.btn_multiply); bt_divide = (Button) findViewById(R.id.btn_divide); bt_sub = (Button) findViewById(R.id.btn_sub); bt_add = (Button) findViewById(R.id.btn_add); bt_point = (Button) findViewById(R.id.btn_point); bt_equal = (Button) findViewById(R.id.btn_equal); bt_clear = (Button) findViewById(R.id.btn_clear); et_edit = (TextView) findViewById(R.id.a_edit); et_edit.setText("0.0"); //注册按钮监听器 bt_zero.setOnClickListener(this); bt_one.setOnClickListener(this); bt_two.setOnClickListener(this); bt_three.setOnClickListener(this); bt_four.setOnClickListener(this); bt_five.setOnClickListener(this); bt_six.setOnClickListener(this); bt_seven.setOnClickListener(this); bt_eight.setOnClickListener(this); bt_nine.setOnClickListener(this); bt_point.setOnClickListener(this); bt_add.setOnClickListener(this); bt_sub.setOnClickListener(this); bt_divide.setOnClickListener(this); bt_multiply.setOnClickListener(this); bt_clear.setOnClickListener(this); bt_equal.setOnClickListener(this); } public void onClick(View v) { switch (v.getId()) { //按钮0-9 case R.id.btn_zero: case R.id.btn_one: case R.id.btn_two: case R.id.btn_three: case R.id.btn_four: case R.id.btn_five: case R.id.btn_six: case R.id.btn_seven: case R.id.btn_eight: case R.id.btn_nine: if(e_flag){ str_result = "0"; e_flag = false; }//判断是否已经计算过,即是否输入“=”,如果是,此时已经按下数字键,上一次计算结果则无效,进行初始化,下面同理 str_display.append(((Button) v).getText().toString()); //获取到按钮的显示文字,转化为String类型,插入到str_display变量中 et_edit.setText(str_display.toString()); //将获取到的按钮文字显示在EditText里 break; case R.id.btn_point: if(e_flag){ str_result = "0"; e_flag = false; } if (flag) { str_display.append("."); et_edit.setText(str_display.toString()); flag = false; } break; case R.id.btn_add: case R.id.btn_sub: case R.id.btn_divide: case R.id.btn_multiply: //用户点击了运算按钮 if(s_flag){ //用户第二次点击运算按钮,则将上一次运算得到的结果赋值给num1 getResult(); s_flag = false; //初始化 } str_oper = ((Button) v).getText().toString(); //将获取到的按钮文字显示在EditText里 if (e_flag) { //用户点击了“=”号之后直接输入运算符,表示想连续运算,则先将上一次运算得到的结果赋值给num1 num1 = Double.parseDouble(str_result); str_result = "0"; str_display = new StringBuffer(""); }else{ if (str_display.toString() != "") { num1 = Double.parseDouble(str_display.toString()); //将用户的第一个输入赋值给num1 str_display = new StringBuffer(""); }else{ num1 = 0; } } et_edit.setText(String.valueOf(num1)); //将num1显示在EditText里 flag = true; s_flag = true; break; case R.id.btn_clear: //用户点击了清除按钮,对所有变量进行初始化 str_oper = "+"; str_result = "0"; num1 = 0; num2 = 0; flag = true; e_flag = false; s_flag = false; str_display = new StringBuffer(""); et_edit.setText("0.0"); break; case R.id.btn_equal: //用户点击了“=” getResult(); //调用函数,计算结果 break; } } private void getResult(){ if (str_oper.equals("+")) { //当用户输入的符号为“+”时 num2 = Double.parseDouble(str_display.toString()); str_result = String.valueOf(num1 + num2); //计算两数相加 et_edit.setText(str_result); //将计算结果显示在EditText里 str_display = new StringBuffer(""); } if (str_oper.equals("-")) { //当用户输入的符号为“+”时 num2 = Double.parseDouble(str_display.toString()); str_result = String.valueOf(num1 - num2); //计算两数相减 et_edit.setText(str_result); str_display = new StringBuffer(""); } if (str_oper.equals("*")) { //当用户输入的符号为“+”时 num2 = Double.parseDouble(str_display.toString()); str_result = String.valueOf(num1 * num2);//计算两数相乘 et_edit.setText(str_result); str_display = new StringBuffer(""); } if (str_oper.equals("/")) { //当用户输入的符号为“+”时 num2 = Double.parseDouble(str_display.toString()); if (num2 != 0) { str_result = String.valueOf(num1 / num2);//计算两数相除 et_edit.setText(str_result); } else { Toast.makeText(MainActivity.this, "除数不能为零!", 0).show(); } str_display = new StringBuffer(""); } num1 = 0; num2 =0; e_flag = true; s_flag = false; str_oper = ""; } }
——运行解果如下图所示【左边是android studio自带虚拟机效果,右边是真机效果】:
以上就是简单计算器的实现过程,本小组成员功力尚浅,若有不足之处,请多多指教鸭~