赞
踩
首先是一个线性布局所以整体布局采用<LinearLayout>
其次采用一个垂直方向滚动视图<ScrollView>
显示标题用一个<TextView>,显示输出结果用一个<TextView>
下方按钮行列均匀,采用网格布局<GridLayout>
- //前后端分离,存放路径app-->src-->main-->res-->layout-->activity_calculator.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:background="#EEEEEE"
- android:padding="5dp">
-
- <ScrollView
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:textColor="@color/black"
- android:textSize="20sp"
- android:text="@string/calculator"/>
- <TextView
- android:id="@+id/tv_result"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@color/white"
- android:lines="3"
- android:text="0"
- android:textColor="@color/black"
- android:textSize="25sp"
- android:gravity="right|bottom"/>
-
- <GridLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:columnCount="4"
- android:rowCount="5">
- <Button
- android:id="@+id/btn_cancel"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="@string/cancel"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"
- />
-
- <Button
- android:id="@+id/btn_divide"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="@string/divide"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"
- />
- <Button
- android:id="@+id/btn_multiply"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="@string/multiply"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"
- />
- <Button
- android:id="@+id/btn_clear"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="@string/clear"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"
- />
- <Button
- android:id="@+id/btn_seven"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="7"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"
- />
- <Button
- android:id="@+id/eight"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="8"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"
- />
- <Button
- android:id="@+id/nine"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="9"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"
- />
- <Button
- android:id="@+id/btn_add"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="@string/add"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"
- />
- <Button
- android:id="@+id/btn_four"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="4"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"
- />
- <Button
- android:id="@+id/btn_five"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="5"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"
- />
- <Button
- android:id="@+id/btn_six"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="6"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"
- />
- <Button
- android:id="@+id/btn_minus"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="@string/minus"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"
- />
- <Button
- android:id="@+id/btn_one"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="1"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"
- />
- <Button
- android:id="@+id/btn_two"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="2"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"
- />
- <Button
- android:id="@+id/btn_three"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="3"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"
- />
- <Button
- android:id="@+id/btn_square"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="@string/square"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"
- />
- <Button
- android:id="@+id/btn_inverse"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="@string/inverse"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"
- />
- <Button
- android:id="@+id/btn_zero"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="0"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"
- />
- <Button
- android:id="@+id/btn_point"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="@string/point"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"
- />
- <Button
- android:id="@+id/btn_equal"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="@string/equal"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"
- />
-
- </GridLayout>
-
- </LinearLayout>
-
- </ScrollView>
-
- </LinearLayout>
首先要定义几个变量存放运行数据。
private TextView tv_result; //返回 private String firstNum=""; //第一个操作数 private String secondNum=""; //第二个操作数 private String operator=""; //操作符号 private String result=""; //当前返回 private String showText=""; //显示文本
在Calculate开始时获取到点击事件数据并在onCreate()中保存。在对于数据进行处理。其中定义了calculateFour()函数处理加减乘四则运算, clear()用于清除字符串,refreshOperate()用于刷新运算结果.......具体详解代码如下。
- //app-->src-->main-->java-->CalculatorActivity
- package com.example.myapplication;
- import androidx.appcompat.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Switch;
- import android.widget.TextView;
-
-
- public class CalculatorActivity extends AppCompatActivity implements View.OnClickListener {
- private TextView tv_result; //返回
- private String firstNum=""; //第一个操作数
- private String secondNum=""; //第二个操作数
- private String operator=""; //操作符号
- private String result=""; //当前返回
- private String showText=""; //显示文本
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_calculator);
- tv_result = findViewById(R.id.tv_result);
- findViewById(R.id.btn_cancel).setOnClickListener(this); //CE 取消
- findViewById(R.id.btn_clear).setOnClickListener(this);// C 清除
- findViewById(R.id.btn_add).setOnClickListener(this); // +加法
- findViewById(R.id.btn_minus).setOnClickListener(this);// -减法
- findViewById(R.id.btn_multiply).setOnClickListener(this);//乘法
- findViewById(R.id.btn_divide).setOnClickListener(this); //除法
- findViewById(R.id.btn_equal).setOnClickListener(this); //等于
- findViewById(R.id.btn_square).setOnClickListener(this); //开平方
- findViewById(R.id.btn_point).setOnClickListener(this); //点
- findViewById(R.id.btn_inverse).setOnClickListener(this);//倒数
- findViewById(R.id.btn_zero).setOnClickListener(this); //0
- findViewById(R.id.btn_one).setOnClickListener(this); //1
- findViewById(R.id.btn_two).setOnClickListener(this); //2
- findViewById(R.id.btn_three).setOnClickListener(this); //3
- findViewById(R.id.btn_four).setOnClickListener(this); //4
- findViewById(R.id.btn_five).setOnClickListener(this); //5
- findViewById(R.id.btn_six).setOnClickListener(this); //6
- findViewById(R.id.btn_seven).setOnClickListener(this); //7
- findViewById(R.id.eight).setOnClickListener(this); //8
- findViewById(R.id.nine).setOnClickListener(this); //9
- }
-
- @Override
- public void onClick(View view) {
- String inputText; //变量用于输出信息
-
- if(view.getId()==R.id.btn_square)
- {inputText="√";}
- else
- {inputText = ((TextView)view).getText().toString();}
-
- switch (view.getId())
- {//点击了清除按钮
- case R.id.btn_clear: //清除按钮
- clear();
- break;
- case R.id.btn_clear: //回退按钮(逻辑未做)
- break;
- case R.id.btn_add: //加
- case R.id.btn_minus: //减
- case R.id.btn_multiply: //乘
- case R.id.btn_divide: //除法
- {operator=inputText; //运算符
- refreshText(showText+operator);}
- break;
- case R.id.btn_equal: //等号
- {double calculate_result= calculateFour();
- refreshOperate(String.valueOf(calculate_result));
- refreshOperate(showText+"="+result);}
- break;
- case R.id.btn_square: //开方
- {double btn_square=Math.sqrt(Double.parseDouble(firstNum)); //处理开发
- refreshOperate(String.valueOf(btn_square));
- refreshOperate(showText+"√="+result);}
- break;
- case R.id.btn_inverse: //求倒数
- {double inverse_result=1.0/Double.parseDouble(firstNum); //处理倒数
- refreshOperate(String.valueOf(inverse_result));
- refreshOperate(showText+"/="+result);}
- break;
- default:
- if(result.length()>0&&operator.equals("")) //判断运算结束时情况
- { clear();}
- //无运算符,则继续拼接第一个操作数
- if(operator.equals(""))
- {
- firstNum=firstNum+inputText;
- }else {
- secondNum=secondNum+inputText;
- }
- //整数不需要前面的0
- if(showText.equals(0)&&!inputText.equals("."))
- {
- refreshText(inputText);
- }
- refreshText(showText+inputText);
- break;
- }
- }
-
-
-
- private double calculateFour() { //四则运算
- switch (operator)
- { case "+":
- return Double.parseDouble(firstNum)+Double.parseDouble(secondNum);
- case "-":
- return Double.parseDouble(firstNum)-Double.parseDouble(secondNum);
- case "*":
- return Double.parseDouble(firstNum)*Double.parseDouble(secondNum);
- default:
- return Double.parseDouble(firstNum)/Double.parseDouble(secondNum);
- }
- }
-
-
- //清空字符串
- private void clear() {
- refreshOperate("");
- refreshText("");
- }
-
-
- //刷新运算结果
- private void refreshOperate(String new_result)
- {
- result=new_result;
- firstNum=result;
- secondNum="";
- operator="";
- }
-
- //刷新显示文本
- private void refreshText(String text){
- showText=text;
- tv_result.setText(showText);
- }
-
- }
Android开发,前后端分离,数据与代码分离所以定义的字符串都单读放在string.xml里
- //app-->src-->res-->values-->strings.xml
- <resources>
- <string name="app_name">My Application</string>
- <string name="app_val">value1</string>
- <string name="calculator">简易计算器</string>
- <string name="cancel">CE</string>
- <string name="add">+</string>
- <string name="minus">-</string>
- <string name="divide">/</string>
- <string name="multiply">*</string>
- <string name="clear">C</string>
- <string name="inverse">1/X</string>
- <string name="point">.</string>
- <string name="equal">=</string>
- <string name="square">√</string>
- </resources>
这个简易计算器还有很多bug例如,没有判断错误情况,三操作数情况,负数,科学计算器的功能等等,有待完善。(仅用于对前段时间知识的总结)。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。