安卓(Android studio)编写一个计算器,可以连续加减乘除,可加小数点,不包含括号以及加减乘除优先算法。

安卓(Android studio)编写一个计算器,可以累加,可加小数点,不包含括号以及加减乘除优先算法。

首先需要在res目录下的layout文件下创建一个xml文件进行界面的布局设计,接着在Java目录下创建一个Java的class类文件,然后在AndroidManifest.xml设置启动Java的class类文件。具体实现如下:(部分代码可以封装处理,时间不够处理的有点繁琐)

1. 布局设计(各个控件的ID,大小等设计)


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtResult"
        android:text="显示结果"
        android:gravity="right"
        android:textSize="24dp"

        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btnCls"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="myClick"
            android:text="C"/>
        <Button
            android:id="@+id/btnDel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:onClick="myClick"
            android:text="DEL"/>
        <Button
            android:id="@+id/btnchu"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="myClick"
            android:text="÷"/>
    LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1"/>
        <Button
            android:id="@+id/btn2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="2"/>
        <Button
            android:id="@+id/btn3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="3"/>
        <Button
            android:id="@+id/btnAdd"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="+"/>
    LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="myClick"
            android:text="4"/>
        <Button
            android:id="@+id/btn5"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="myClick"
            android:text="5"/>
        <Button
            android:id="@+id/btn6"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="myClick"
            android:text="6"/>
        <Button
            android:id="@+id/btnobs"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="myClick"
            android:text="-"/>
    LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn7"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="myClick"
            android:text="7"/>
        <Button
            android:id="@+id/btn8"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="myClick"
            android:text="8"/>
        <Button
            android:id="@+id/btn9"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="myClick"
            android:text="9"/>
        <Button
            android:id="@+id/btncheng"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="myClick"
            android:text="×"/>
    LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn0"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="myClick"
            android:text="0"/>
        <Button
            android:id="@+id/btndot"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="myClick"
            android:text="."/>
        <Button
            android:id="@+id/btneql"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:onClick="myClick"
            android:text="="/>

    LinearLayout>
LinearLayout>

2. 激活设置


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.demo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".Lsn0">//Lsn0是Java类 
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            intent-filter>
        activity>
        <activity android:name=".MainActivity">activity>


    application>

manifest>

3.Java实现具体功能

package com.example.demo;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class Lsn0 extends AppCompatActivity {
    //Button btn1,btn2,btn3;

    private TextView txtResult;
    //Button[] btn=new Button[10];
    private Button btnAdd, btncheng, btnchu, btnDel, btnCls, btnobs, btneql, btndot;
    private StringBuffer digitalA = new StringBuffer(), digitalB = new StringBuffer();
    private boolean isChar = false, isDot = false;
    private int operate = 0;
    Button[] btn = new Button[10];
    int[] btnId = {R.id.btn0, R.id.btn1, R.id.btn2, R.id.btn3, R.id.btn4, R.id.btn5, R.id.btn6, R.id.btn7, R.id.btn8, R.id.btn9};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.main_layout);
        txtResult = (TextView) this.findViewById(R.id.txtResult);
        //btn1 = (Button) this.findViewById(R.id.btn1);
        //btn2 = (Button) this.findViewById(R.id.btn2);
        //btn3 = (Button) this.findViewById(R.id.btn3);
        //txtResult = (TextView) this.findViewById(R.id.txtResult);
        initView();
        btnAdd = (Button) this.findViewById(R.id.btnAdd);
        btnobs = (Button) this.findViewById(R.id.btnobs);
        btncheng = (Button) this.findViewById(R.id.btncheng);
        btnchu = (Button) this.findViewById(R.id.btnchu);
        btnDel = (Button) this.findViewById(R.id.btnDel);
        btnCls = (Button) this.findViewById(R.id.btnCls);
        btneql = (Button) this.findViewById(R.id.btneql);
        btndot = (Button) this.findViewById(R.id.btndot);
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                operate = 1;
                isChar = true;



                if (digitalA.length()!=0&&digitalB.length()!=0) {

                    //txtResult.setText("0");
                    float a = Float.parseFloat(digitalA.toString());
                    float b = Float.parseFloat(digitalB.toString());
                    float c=0;
                    c=a+b;

                    digitalA=new StringBuffer();
                    digitalA.append(c);

                    digitalB=new StringBuffer();

                }
                txtResult.setText(digitalA);
                //float a = Float.parseFloat(digitalA.toString());
                //float b = Float.parseFloat(digitalB.toString());
                //if (digitalB.length()!=0&&digitalA.length()!=0){

               // }
            }
        });
        btnobs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                operate = 2;
                isChar = true;
                txtResult.setText("0");
                if (digitalA.length()!=0&&digitalB.length()!=0) {

                    //txtResult.setText("0");
                    float a = Float.parseFloat(digitalA.toString());
                    float b = Float.parseFloat(digitalB.toString());
                    float c=0;
                    c=a-b;

                    digitalA=new StringBuffer();
                    digitalA.append(c);

                    digitalB=new StringBuffer();

                }
            }
        });
        btncheng.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                operate = 3;
                isChar = true;
                txtResult.setText("0");
                if (digitalA.length()!=0&&digitalB.length()!=0) {

                    //txtResult.setText("0");
                    float a = Float.parseFloat(digitalA.toString());
                    float b = Float.parseFloat(digitalB.toString());
                    float c=0;
                    c=a*b;

                    digitalA=new StringBuffer();
                    digitalA.append(c);

                    digitalB=new StringBuffer();

                }
            }
        });
        btnchu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                operate = 4;
                isChar = true;
                txtResult.setText("0");
                if (digitalA.length()!=0&&digitalB.length()!=0) {

                    //txtResult.setText("0");
                    float a = Float.parseFloat(digitalA.toString());
                    float b = Float.parseFloat(digitalB.toString());
                    float c=0;
                    c=a/b;

                    digitalA=new StringBuffer();
                    digitalA.append(c);

                    digitalB=new StringBuffer();

                }
            }
        });
        /*btndot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //operate=5;
                isChar=true;
                float a=Float.parseFloat(digitalA.toString());
                float b=Float.parseFloat(digitalB.toString());
                if (a!=null){

                }
                //txtResult.setText(c+"");
                //digitalA=new StringBuffer();
                //digitalB=new StringBuffer();
                //txtResult.setText(null);

            }
        });*/
        btnCls.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                digitalB = new StringBuffer();
                digitalA = new StringBuffer();
                txtResult.setText("请输入");
            }
        });
        btnDel.setOnClickListener(new MyClick());
        btndot.setOnClickListener(new MyClick());
        btneql.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                float a = Float.parseFloat(digitalA.toString());
                float b = Float.parseFloat(digitalB.toString());
                float c = 0;
                switch (operate) {
                    case 1:


                        c = a + b;

                        break;
                    case 2:
                        c = a - b;
                        break;
                    case 3:
                        c = a * b;
                        break;
                    case 4:
                        c = a / b;
                        break;


                }
                txtResult.setText(c + "");
                digitalA = new StringBuffer();
                digitalB = new StringBuffer();
            }
        });

        MyClick myClick = new MyClick();
        for (int i = 0; i < btn.length; i++) {
            btn[i].setOnClickListener(myClick);
        }
        //btndot.setOnClickListener(myClick);
        // btnCls.setOnClickListener(myClick);
    }

    class MyClick implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btnDel:
                    if (isChar) {
                        String temp = digitalB.substring(0, digitalB.length() - 1);
                        digitalB = new StringBuffer();
                        digitalB.append(temp);
                        txtResult.setText(digitalB.toString());
                        break;
                    } else {
                        String temp = digitalA.substring(0, digitalA.length() - 1);
                        digitalA = new StringBuffer();
                        digitalA.append(temp);
                        txtResult.setText(digitalA.toString());
                        break;
                    }
                case R.id.btndot:
                    if (isChar) {
                        if (!isDot) {
                            if (digitalB.length() > 0) {
                                digitalB.append(".");
                                isDot = true;
                                break;
                            } else {
                                digitalB.append("0");
                                digitalB.append(".");
                                isDot = true;
                                break;
                            }
                        }
                        break;
                    } else {
                        if (!isDot) {
                            if (digitalA.length() > 0) {
                                digitalA.append(".");
                                isDot = true;
                                break;
                            } else {
                                digitalA.append("0");
                                digitalA.append(".");
                                isDot = true;
                                break;
                            }
                        }
                        break;
                    }


//                case R.id.btnCls:{
//
//
//                    break;
//                    //digitalA.delete(0,9);
//                    //digitalB.delete(0,9);
//                }
                case R.id.btn1:
                    if (isChar) {
                        digitalB.append('1');
                        break;
                    } else {
                        digitalA.append("1");
                        break;
                    }
                case R.id.btn2:
                    if (isChar) {
                        digitalB.append('2');
                        break;
                    } else {
                        digitalA.append("2");
                        break;
                    }
                case R.id.btn3:
                    if (isChar) {
                        digitalB.append('3');
                        break;
                    } else {
                        digitalA.append("3");
                        break;
                    }
                case R.id.btn4:
                    if (isChar) {
                        digitalB.append('4');
                        break;
                    } else {
                        digitalA.append("4");
                        break;
                    }
                case R.id.btn5:
                    if (isChar) {
                        digitalB.append('5');
                        break;
                    } else {
                        digitalA.append("5");
                        break;
                    }
                case R.id.btn6:
                    if (isChar) {
                        digitalB.append('6');
                        break;
                    } else {
                        digitalA.append("6");
                        break;
                    }
                case R.id.btn7:
                    if (isChar) {
                        digitalB.append('7');
                        break;
                    } else {
                        digitalA.append("7");
                        break;
                    }
                case R.id.btn8:
                    if (isChar) {
                        digitalB.append('8');
                        break;
                    } else {
                        digitalA.append("8");
                        break;
                    }
                case R.id.btn9:
                    if (isChar) {
                        digitalB.append('9');
                        break;
                    } else {
                        digitalA.append("9");
                        break;
                    }
                case R.id.btn0:
                    if (isChar) {
                        digitalB.append('0');
                        break;
                    } else {
                        digitalA.append("0");
                        break;
                    }
               /* case R.id.btndot:
                    if (isChar) {
                        digitalB.append(".");
                        break;
                    }else {
                        digitalA.append(".");
                        break;
                    }*/


            }
            if (isChar) {
                txtResult.setText(digitalB.toString());
            } else {
                txtResult.setText(digitalA.toString());
            }
        }
    }

    void initView() {
        for (int i = 0; i < btn.length; i++) {
            btn[i] = (Button) this.findViewById(btnId[i]);
        }
        //btn1.setOnClickListener(new MyClick());
    }
}

4.截图展示效果
安卓(Android studio)编写一个计算器,可以连续加减乘除,可加小数点,不包含括号以及加减乘除优先算法。_第1张图片

你可能感兴趣的:(笔记,安卓,android,移动开发,android,studio,java)