界面设计
activity_calcuator.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:background="#EEEEEE"android:orientation ="vertical"android:padding="5dp"><ScrollViewandroid:layout_width="match_parent"android:layout_height="wrap_content"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:text="@string/simple_calculator"android:textColor="@color/black"android:textSize="20sp" /><TextViewandroid:id="@+id/tv_result"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@color/white"android:gravity="right|bottom"android:lines="3"android:text="0"android:textColor="@color/black"android:textSize="25sp" /><GridLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:columnCount="4"android:rowCount="5"><Buttonandroid:id="@+id/btn_cancel"android:layout_width="0dp"android:layout_height="75dp"android:layout_columnWeight="1"android:gravity="center"android:text="@string/cancel"android:textColor="@color/black"android:textSize="@dimen/button_font_size" /><Buttonandroid:id="@+id/btn_divide"android:layout_width="0dp"android:layout_height="75dp"android:layout_columnWeight="1"android:gravity="center"android:text="➗"android:textColor="@color/black"android:textSize="30sp" /><Buttonandroid:id="@+id/btn_multiply"android:layout_width="0dp"android:layout_height="75dp"android:layout_columnWeight="1"android:gravity="center"android:text="x"android:textColor="@color/black"android:textSize="30sp" /><Buttonandroid:id="@+id/btn_clear"android:layout_width="0dp"android:layout_height="75dp"android:layout_columnWeight="1"android:gravity="center"android:text="C"android:textColor="@color/black"android:textSize="30sp" /><Buttonandroid:id="@+id/btn_seven"android:layout_width="0dp"android:layout_height="75dp"android:layout_columnWeight="1"android:gravity="center"android:text="7"android:textColor="@color/black"android:textSize="30sp" /><Buttonandroid:id="@+id/btn_eight"android:layout_width="0dp"android:layout_height="75dp"android:layout_columnWeight="1"android:gravity="center"android:text="8"android:textColor="@color/black"android:textSize="30sp" /><Buttonandroid:id="@+id/btn_nine"android:layout_width="0dp"android:layout_height="75dp"android:layout_columnWeight="1"android:gravity="center"android:text="9"android:textColor="@color/black"android:textSize="30sp" /><Buttonandroid:id="@+id/btn_plus"android:layout_width="0dp"android:layout_height="75dp"android:layout_columnWeight="1"android:gravity="center"android:text="+"android:textColor="@color/black"android:textSize="30sp" /><Buttonandroid:id="@+id/btn_four"android:layout_width="0dp"android:layout_height="75dp"android:layout_columnWeight="1"android:gravity="center"android:text="4"android:textColor="@color/black"android:textSize="30sp" /><Buttonandroid:id="@+id/btn_five"android:layout_width="0dp"android:layout_height="75dp"android:layout_columnWeight="1"android:gravity="center"android:text="5"android:textColor="@color/black"android:textSize="30sp" /><Buttonandroid:id="@+id/btn_six"android:layout_width="0dp"android:layout_height="75dp"android:layout_columnWeight="1"android:gravity="center"android:text="6"android:textColor="@color/black"android:textSize="30sp" /><Buttonandroid:id="@+id/btn_minus"android:layout_width="0dp"android:layout_height="75dp"android:layout_columnWeight="1"android:gravity="center"android:text="-"android:textColor="@color/black"android:textSize="30sp" /><Buttonandroid:id="@+id/btn_one"android:layout_width="0dp"android:layout_height="75dp"android:layout_columnWeight="1"android:gravity="center"android:text="1"android:textColor="@color/black"android:textSize="30sp" /><Buttonandroid:id="@+id/btn_two"android:layout_width="0dp"android:layout_height="75dp"android:layout_columnWeight="1"android:gravity="center"android:text="2"android:textColor="@color/black"android:textSize="30sp" /><Buttonandroid:id="@+id/btn_three"android:layout_width="0dp"android:layout_height="75dp"android:layout_columnWeight="1"android:gravity="center"android:text="3"android:textColor="@color/black"android:textSize="30sp" /><ImageButtonandroid:id="@+id/ib_sqrt"android:layout_width="0dp"android:layout_height="@dimen/button_height"android:layout_columnWeight="1"android:scaleType="centerInside"android:src="@drawable/sqtr" /><Buttonandroid:id="@+id/btn_reciprocal"android:layout_width="0dp"android:layout_height="@dimen/button_height"android:layout_columnWeight="1"android:gravity="center"android:text="@string/reciprocal"android:textColor="@color/black"android:textSize="@dimen/button_font_size" /><Buttonandroid:id="@+id/btn_zero"android:layout_width="0dp"android:layout_height="@dimen/button_height"android:layout_columnWeight="1"android:gravity="center"android:text="@string/zero"android:textColor="@color/black"android:textSize="@dimen/button_font_size" /><Buttonandroid:id="@+id/btn_dot"android:layout_width="0dp"android:layout_height="@dimen/button_height"android:layout_columnWeight="1"android:gravity="center"android:text="@string/dot"android:textColor="@color/black"android:textSize="@dimen/button_font_size" /><Buttonandroid: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>
dimens.xml
<resources><dimen name="fab_margin">16dp</dimen><dimen name="button_font_size">30sp</dimen><dimen name="button_height">75dp</dimen>
</resources>
strings.xml
<resources><string name="app_name">chapter03</string><string name="action_settings">Settings</string><!-- Strings used for fragments for navigation --><string name="first_fragment_label">First Fragment</string><string name="second_fragment_label">Second Fragment</string><string name="next">Next</string><string name="previous">Previous</string><string name="hello">你好,世界</string><string name="simple_calculator">牛逼算器</string><string name="cancel">CE</string><string name="divide">➗</string><string name="multiply">x</string><string name="clear">C</string><string name="seven">7</string><string name="eight">8</string><string name="nine">9</string><string name="plus">+</string><string name="four">4</string><string name="five">5</string><string name="six">6</string><string name="minus">-</string><string name="one">1</string><string name="two">2</string><string name="three">3</string><string name="reciprocal">1/x</string><string name="zero">0</string><string name="dot">.</string><string name="equal">=</string>
</resources>
CalcuatorActivity.java逻辑代码:
package com.example.myapplication;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;public class CalcuatorActivity extends AppCompatActivity {private TextView tv_result;//第一个操作数private String firstNum = "";//运算符private String operator = "";//第二个操作数private String secondNum = "";//当前的计算结果private String result = "";//显示的文本内容private String showText = "";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);Log.d("CalcuatorActiviy","onCreat() method called");setContentView(R.layout.activity_calcuator);//从布局中获取名交tv_result的文本视图tv_result = findViewById(R.id.tv_result);//下面每个按钮控件都组册了点击监听findViewById(R.id.btn_cancel).setOnClickListener((View.OnClickListener) this);findViewById(R.id.btn_divide).setOnClickListener((View.OnClickListener) this);findViewById(R.id.btn_multiply).setOnClickListener((View.OnClickListener) this);findViewById(R.id.btn_clear).setOnClickListener((View.OnClickListener) this);findViewById(R.id.btn_seven).setOnClickListener((View.OnClickListener) this);findViewById(R.id.btn_eight).setOnClickListener((View.OnClickListener) this);findViewById(R.id.btn_nine).setOnClickListener((View.OnClickListener) this);findViewById(R.id.btn_plus).setOnClickListener((View.OnClickListener) this);findViewById(R.id.btn_four).setOnClickListener((View.OnClickListener) this);findViewById(R.id.btn_five).setOnClickListener((View.OnClickListener) this);findViewById(R.id.btn_six).setOnClickListener((View.OnClickListener) this);findViewById(R.id.btn_minus).setOnClickListener((View.OnClickListener) this);findViewById(R.id.btn_one).setOnClickListener((View.OnClickListener) this);findViewById(R.id.btn_two).setOnClickListener((View.OnClickListener) this);findViewById(R.id.btn_three).setOnClickListener((View.OnClickListener) this);findViewById(R.id.btn_reciprocal).setOnClickListener((View.OnClickListener) this);findViewById(R.id.btn_zero).setOnClickListener((View.OnClickListener) this);findViewById(R.id.btn_dot).setOnClickListener((View.OnClickListener) this);findViewById(R.id.btn_equal).setOnClickListener((View.OnClickListener) this);findViewById(R.id.ib_sqrt).setOnClickListener((View.OnClickListener) this);}//@Overridepublic void onClick(View v){String inputText;//如果是开根号按钮if(v.getId() == R.id.ib_sqrt){inputText = "√";}else{//除了开根号之外的其他按钮inputText = ((TextView) v).getText().toString();}switch(v.getId()){//点击了清除按钮case R.id.btn_clear:clear();break;//点击了取消按钮case R.id.btn_cancel:break;//点击了加减乘除case R.id.btn_plus: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));refreshText(showText + "=" + result);// 开根号按钮case R.id.ib_sqrt:double sqrt_result = Math.sqrt(Double.parseDouble(firstNum));refreshOperate(String.valueOf(sqrt_result));refreshText(showText + "√=" + result);break;//点击了求倒数按钮case R.id.btn_reciprocal:double reciprocal_result = 1.0/Double.parseDouble(firstNum);refreshOperate(String.valueOf(reciprocal_result));refreshText(showText + "/=" + result);break;default://上次结果已经出来了if(result.length()>0&&operator.equals("")){clear();}//无运算符,则继续拼接第一个操作数if(operator.equals("")){firstNum = firstNum + inputText;}else{//有运算符,则继续拼接第二个操作符secondNum = secondNum + inputText;}if(showText.equals("0")&&!inputText.equals(".")){refreshText(inputText);}else {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 "8":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);}}
这完全是前端的领域我觉得