(含完整代码)简易Android计算器的实现

文章结构

      • 1、项目要求
      • 2、实现思路
      • 3、代码实现
          • 主文件
          • 子文件
      • 4、效果预览

1、项目要求

其实老师的要求就是做一个简易的计算器,有两个文本框,一个按钮,然后实现加法即可。
但是我还是想做一个比较好康的计算器(如下),也可以借此写个博客和大家分享一下
(含完整代码)简易Android计算器的实现_第1张图片

2、实现思路

思路就是做一个和平时手机用的计算器差不多的即可。
六行:第一行是显示计算过程和结果的
   第二行道第六行是放各种按钮的,直接看效果预览就知道了
By the way,这里我的计算器只是个很粗糙的版本,我已经把UI都搞好了,并且把一些简单的计算都做好了,大家只需要完善一下就可以达到自己的需求了!各位都是学过Java的,相信对大家来说应该不难吧!
代码太多,所以分为
   主文件:MainActivity.java、activity_main.xml
   子文件:
        (含完整代码)简易Android计算器的实现_第2张图片

3、代码实现

主文件

废话不多说,直接上代码
MainActivity.java(这是java文件的代码内容):

package cn.hillain.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity2 extends AppCompatActivity {
    private TextView input;
    private TextView result;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        input = this.findViewById(R.id.input);
        result = this.findViewById(R.id.result);
    }
    String input1 = "";
    String oper = "";
    String input2 = "";
    boolean flag = false;
    boolean input1_flag = false;
    public void oneOnClick(View view){
        if(view instanceof TextView){
            String oneText = ((TextView)view).getText().toString();
            if(flag){
                switch (oneText){
                    case "C":
                        input.setText("");
                        result.setText("");
                        input1 = "";
                        input2 = "";
                        oper ="";
                        input1_flag=false;
                        flag=false;
                        break;
                }
            }else{
                switch (oneText){
                    case "+/-":
                        break;
                    case "%":
                        break;
                    case "1":
                        if(!input1_flag){
                            input1 += "1";
                            input.setText(input1);
                        }else{
                            input2 += "1";
                            input.setText(input1+oper+input2);
                        }
                        break;
                    case "2":
                        if(!input1_flag){
                            input1 += "2";
                            input.setText(input1);
                        }else{
                            input2 += "2";
                            input.setText(input1+oper+input2);
                        }
                        break;
                    case "3":
                        if(!input1_flag){
                            input1 += "3";
                            input.setText(input1);
                        }else{
                            input2 += "3";
                            input.setText(input1+oper+input2);
                        }
                        break;
                    case "4":
                        if(!input1_flag){
                            input1 += "4";
                            input.setText(input1);
                        }else{
                            input2 += "4";
                            input.setText(input1+oper+input2);
                        }
                        break;
                    case "5":
                        if(!input1_flag){
                            input1 += "5";
                            input.setText(input1);
                        }else{
                            input2 += "5";
                            input.setText(input1+oper+input2);
                        }
                        break;
                    case "6":
                        if(!input1_flag){
                            input1 += "6";
                            input.setText(input1);
                        }else{
                            input2 += "6";
                            input.setText(input1+oper+input2);
                        }
                        break;
                    case "7":
                        if(!input1_flag){
                            input1 += "7";
                            input.setText(input1);
                        }else{
                            input2 += "7";
                            input.setText(input1+oper+input2);
                        }
                        break;
                    case "8":
                        if(!input1_flag){
                            input1 += "8";
                            input.setText(input1);
                        }else{
                            input2 += "8";
                            input.setText(input1+oper+input2);
                        }
                        break;
                    case "9":
                        if(!input1_flag){
                            input1 += "9";
                            input.setText(input1);
                        }else{
                            input2 += "9";
                            input.setText(input1+oper+input2);
                        }
                        break;
                    case "0":
                        if(!input1_flag){
                            input1 += "0";
                            input.setText(input1);
                        }else{
                            input2 += "0";
                            input.setText(input1+oper+input2);
                        }
                        break;
                    case ".":
                        if(!input1_flag){
                            input1 += ".";
                            input.setText(input1);
                        }else{
                            input2 += ".";
                            input.setText(input1+oper+input2);
                        }
                        break;
                    case "÷":
                        oper = "÷";
                        input1_flag = true;
                        input.setText(input1+oper+input2);
                        break;
                    case "*":
                        oper = "*";
                        input1_flag = true;
                        input.setText(input1+oper+input2);
                        break;
                    case "-":
                        oper = "-";
                        input1_flag = true;
                        input.setText(input1+oper+input2);
                        break;
                    case "+":
                        oper = "+";
                        input1_flag = true;
                        input.setText(input1+oper+input2);
                        break;
                    case "=":
                        float num1 = Float.valueOf(input1).floatValue();
                        float num2 = Float.valueOf(input2).floatValue();
                        if(oper.equals("+")){
                            flag = true;
                            result.setText(String.valueOf(num1+num2));
                        }
                        else if(oper.equals("-")){
                            flag = true;
                            result.setText(String.valueOf(num1-num2));
                        }
                        else if(oper.equals("*")){
                            flag = true;
                            result.setText(String.valueOf(num1*num2));
                        }
                        if(oper.equals("÷")){
                            if (num2 == 0) {
                                Toast.makeText(MainActivity2.this, "除数不能为0", Toast.LENGTH_LONG).show();
                            }else{
                                flag = true;
                                result.setText(String.valueOf(num1/num2));
                            }
                        }
                        break;
                }
            }

        }
    }
}

activity_main.xml(layout中的xml文件):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="100dp">
        <TextView
        android:id="@+id/input"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:text=""
        android:textSize="30sp" />
        <TextView
            android:id="@+id/result"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="195dp"
            android:layout_marginTop="50dp"
            android:text=""
            android:textSize="30sp" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="80dp">
        <TextView
            android:id="@+id/C"
            android:onClick="oneOnClick"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center"
            android:textSize="30sp"
            android:background="@drawable/selector_gray1"
            android:text="C"/>
        <TextView
            android:id="@+id/zf"
            android:onClick="oneOnClick"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center"
            android:textSize="30sp"
            android:background="@drawable/selector_gray1"
            android:text="+/-"/>
        <TextView
            android:id="@+id/mo"
            android:onClick="oneOnClick"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center"
            android:textSize="30sp"
            android:background="@drawable/selector_gray1"
            android:text="%"/>
        <TextView
            android:id="@+id/chu"
            android:onClick="oneOnClick"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center"
            android:textSize="30sp"
            android:background="@drawable/selector_gray3"
            android:text="÷"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="80dp">
        <TextView
            android:id="@+id/one"
            android:onClick="oneOnClick"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center"
            android:textSize="30sp"
            android:background="@drawable/selector_gray2"
            android:text="1"/>
        <TextView
            android:id="@+id/two"
            android:onClick="oneOnClick"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center"
            android:textSize="30sp"
            android:background="@drawable/selector_gray2"
            android:text="2"/>
        <TextView
            android:id="@+id/three"
            android:onClick="oneOnClick"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center"
            android:textSize="30sp"
            android:background="@drawable/selector_gray2"
            android:text="3"/>
        <TextView
            android:id="@+id/cheng"
            android:onClick="oneOnClick"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center"
            android:textSize="30sp"
            android:background="@drawable/selector_gray3"
            android:text="*"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="80dp">
        <TextView
            android:id="@+id/four"
            android:onClick="oneOnClick"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center"
            android:textSize="30sp"
            android:background="@drawable/selector_gray2"
            android:text="4"/>
        <TextView
            android:id="@+id/five"
            android:onClick="oneOnClick"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center"
            android:textSize="30sp"
            android:background="@drawable/selector_gray2"
            android:text="5"/>
        <TextView
            android:id="@+id/six"
            android:onClick="oneOnClick"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center"
            android:textSize="30sp"
            android:background="@drawable/selector_gray2"
            android:text="6"/>
        <TextView
            android:id="@+id/jian"
            android:onClick="oneOnClick"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center"
            android:textSize="30sp"
            android:background="@drawable/selector_gray3"
            android:text="-"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="80dp">
        <TextView
            android:id="@+id/seven"
            android:onClick="oneOnClick"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center"
            android:textSize="30sp"
            android:background="@drawable/selector_gray2"
            android:text="7"/>
        <TextView
            android:id="@+id/eight"
            android:onClick="oneOnClick"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center"
            android:textSize="30sp"
            android:background="@drawable/selector_gray2"
            android:text="8"/>
        <TextView
            android:id="@+id/nine"
            android:onClick="oneOnClick"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center"
            android:textSize="30sp"
            android:background="@drawable/selector_gray2"
            android:text="9"/>
        <TextView
            android:id="@+id/jia"
            android:onClick="oneOnClick"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center"
            android:textSize="30sp"
            android:background="@drawable/selector_gray3"
            android:text="+"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="80dp">
        <TextView
            android:id="@+id/zero"
            android:onClick="oneOnClick"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:paddingLeft="44dp"
            android:textSize="30sp"
            android:background="@drawable/selector_gray2"
            android:text="0"/>
        <TextView
            android:id="@+id/dian"
            android:onClick="oneOnClick"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center"
            android:textSize="30sp"
            android:background="@drawable/selector_gray2"
            android:text="."/>
        <TextView
            android:id="@+id/dengyu"
            android:onClick="oneOnClick"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center"
            android:textSize="30sp"
            android:background="@drawable/selector_gray3"
            android:text="="/>
    </LinearLayout>
</LinearLayout>
子文件

selector_gray1.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
        android:drawable="@drawable/shape_rectangle_gray1"/>
    <item android:drawable="@drawable/shape_rectangle_darkgray"/>
</selector>

selector_gray2.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
        android:drawable="@drawable/shape_rectangle_gray1"/>
    <item android:drawable="@drawable/shape_rectangle_lightgray"/>
</selector>

selector_gray3.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
        android:drawable="@drawable/shape_rectangle_gray2"/>
    <item android:drawable="@drawable/shape_rectangle_orange"/>
</selector>

shape_rectangle_darkgray.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#D6D6D6"/>
    <stroke android:color="#A1A1A1" android:width="1dp"/>
</shape>

shape_rectangle_gray1.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#999999"/>
    <stroke android:color="#A1A1A1" android:width="1dp"/>
</shape>

shape_rectangle_gray2.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#b55f1c"/>
    <stroke android:color="#A1A1A1" android:width="1dp"/>
</shape>

shape_rectangle_lightgray.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#E0E0E0"/>
    <stroke android:color="#A1A1A1" android:width="1dp"/>
</shape>

shape_rectangle_orange.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#FF8D27"/>
    <stroke android:color="#A1A1A1" android:width="1dp"/>
</shape>

4、效果预览

这么多代码,没有预览,估计大家都不想复制
(含完整代码)简易Android计算器的实现_第3张图片

你可能感兴趣的:(Android,android,android,studio,java,移动开发,xml)