使用Android 实现计算器功能

使用android实现简易的计算器的功能
1、给计算机布局:activity_main_xml:

<?xml version="1.0" encoding="utf-8"?>
<GridLayout 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"
    tools:context=".MainActivity"
    android:orientation="vertical"
    >

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        >

        <!--输入的文本框-->
        <EditText
            android:id="@+id/main_et_result"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:hint="请输入数字"
            />
    </GridLayout>

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:rowCount="5"
        android:columnCount="4"
        android:layout_gravity="center|top"
        >

        <Button
            android:id="@+id/main_btn1"
            android:layout_width="90dp"
            android:layout_height="60dp"
            android:text="1"
        />

        <Button
            android:id="@+id/main_btn2"
            android:layout_width="90dp"
            android:layout_height="60dp"
            android:text="2"
        />

        <Button
            android:id="@+id/main_btn3"
            android:layout_width="90dp"
            android:layout_height="60dp"
            android:text="3"
        />

        <Button
            android:id="@+id/main_btnc"
            android:layout_width="90dp"
            android:layout_height="60dp"
            android:text="/"
        />

        <Button
            android:id="@+id/main_btn4"
            android:layout_width="90dp"
            android:layout_height="60dp"
            android:text="4"
        />

        <Button
            android:id="@+id/main_btn5"
            android:layout_width="90dp"
            android:layout_height="60dp"
            android:text="5"
        />
        <Button
            android:id="@+id/main_btn6"
            android:layout_width="90dp"
            android:layout_height="60dp"
            android:text="6"
        />
        <Button
            android:id="@+id/main_btnx"
            android:layout_width="90dp"
            android:layout_height="60dp"
            android:text="*"
        />
        <Button
            android:id="@+id/main_btn7"
            android:layout_width="90dp"
            android:layout_height="60dp"
            android:text="7"
        />
        <Button
            android:id="@+id/main_btn8"
            android:layout_width="90dp"
            android:layout_height="60dp"
            android:text="8"
        />

        <Button
            android:id="@+id/main_btn9"
            android:layout_width="90dp"
            android:layout_height="60dp"
            android:text="9"
        />

        <Button
            android:id="@+id/main_btndel"
            android:layout_width="90dp"
            android:layout_height="60dp"
            android:text="del"
            />

        <Button
            android:id="@+id/main_btnj"
            android:layout_width="90dp"
            android:layout_height="63dp"
            android:text="-" />

        <Button
            android:id="@+id/main_btn0"
            android:layout_width="90dp"
            android:layout_height="60dp"
            android:layout_columnSpan="1"
            android:layout_gravity="fill_horizontal"
            android:text="0" />

        <Button
            android:id="@+id/main_btnd"
            android:layout_width="90dp"
            android:layout_height="60dp"
            android:text="."
        />

        <Button
            android:id="@+id/main_btn1a"
            android:layout_width="90dp"
            android:layout_height="71dp"
            android:layout_rowSpan="2"
            android:layout_gravity="fill_vertical"
            android:text="+" />

        <Button
            android:id="@+id/main_btn1d"
            android:layout_width="180dp"
            android:layout_height="60dp"
            android:layout_columnSpan="3"
            android:layout_gravity="fill_horizontal"
            android:text="="

        />

    <Space />
    </GridLayout>

</GridLayout>

说明:在这里我给编辑框(EditText) 设了一个id,用来通过id处理结果集;给每个按钮(Button)也设了一个id ,目的是通过id去获取它自己的值。

*合并行:layout_columnSpan*, 合并列 :layout_rowSpan

效果图:
这里写图片描述

说明:我这里运用的是一个大的GridLayout布局,里面嵌套两个小的GridLayout布局,分别存放输入框【编辑框(EditText)】和按钮(Button),
EditText和TextView非常相似,它与TextView共用了绝大总分XML属性和文法,
二者最大区别在于:EditText可以接受用户输入。

2、实现的方法:MainActivtiy.java:

package com.example.t212_a05;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    //    结果集
    private EditText editText;

    //数字1-9
    private Button main_btn1;
    private Button main_btn2;
    private Button main_btn3;
    private Button main_btn4;
    private Button main_btn5;
    private Button main_btn6;
    private Button main_btn7;
    private Button main_btn8;
    private Button main_btn9;
    private  Button main_btn0;

    //运算符
    private  Button main_btn1a  ;// +
    private  Button main_btnj;  // -
    private  Button main_btnx;  // *
    private  Button main_btnc;  // /
    private  Button main_btnd;  //小数点
    private  Button main_btn1d;  //=

    //清除
    private  Button main_btndel;

    boolean clear_flag;//清空标识

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //数字1-9
        View main_btn1 = findViewById(R.id.main_btn1);
        View main_btn2 = findViewById(R.id.main_btn2);
        View main_btn3= findViewById(R.id.main_btn3);
        View main_btn4 = findViewById(R.id.main_btn4);
        View main_btn5 = findViewById(R.id.main_btn5);
        View main_btn6 = findViewById(R.id.main_btn6);
        View main_btn7 = findViewById(R.id.main_btn7);
        View main_btn8 = findViewById(R.id.main_btn8);
        View main_btn9 = findViewById(R.id.main_btn9);
        View main_btn0 = findViewById(R.id.main_btn0);
        //运算符
        View main_btn1a = findViewById(R.id.main_btn1a);// +
        View main_btnj = findViewById(R.id.main_btnj);// -
        View main_btnx = findViewById(R.id.main_btnx);// *
        View main_btnc = findViewById(R.id.main_btnc); // /
        View main_btnd = findViewById(R.id.main_btnd);//小数点
        View main_btn1d = findViewById(R.id.main_btn1d);//=
        View main_btndel = findViewById(R.id.main_btndel);//清空


        editText = (EditText) findViewById(R.id.main_et_result);//结果集


        //添加点击事件
        main_btn0.setOnClickListener(this);
        main_btn1.setOnClickListener(this);
        main_btn2.setOnClickListener(this);
        main_btn3.setOnClickListener(this);
        main_btn4.setOnClickListener(this);
        main_btn5.setOnClickListener(this);
        main_btn6.setOnClickListener(this);
        main_btn7.setOnClickListener(this);
        main_btn8.setOnClickListener(this);
        main_btn9.setOnClickListener(this);

        main_btnd.setOnClickListener(this);
        main_btndel.setOnClickListener(this);

        main_btn1a.setOnClickListener(this);
        main_btnj.setOnClickListener(this);
        main_btnx.setOnClickListener(this);
        main_btnc.setOnClickListener(this);
        main_btn1d.setOnClickListener(this);
    }

    //读取每个按钮的点击的内容
    @Override
    public void onClick(View view) {
        //获取文本内容
        String input = editText.getText().toString();
        switch (view.getId()){
            case R.id.main_btn0:
            case R.id.main_btn1:
            case R.id.main_btn2:
            case R.id.main_btn3:
            case R.id.main_btn4:
            case R.id.main_btn5:
            case R.id.main_btn6:
            case R.id.main_btn7:
            case R.id.main_btn8:
            case R.id.main_btn9:
            case R.id.main_btnd:
                if(clear_flag){
                    clear_flag = false;
                    editText.setText("");//赋值为空
                }
                editText.setText(input + ((Button)view).getText());//结果集就为本身
                break;
            case R.id.main_btn1a:
            case R.id.main_btnj:
            case R.id.main_btnx:
            case R.id.main_btnc:
                if(clear_flag){
                    clear_flag = false;
                    input = "";
                    editText.setText("");
                }
                editText.setText(input + " " + ((Button)view).getText() + " ");
                break;
            case R.id.main_btndel:
                if(clear_flag){
                    clear_flag = false;
                    input = "";
                    editText.setText("");
                }else if(input != null || !input.equals("")) {//如果获取到的内容为空
                    editText.setText(input.substring(0, input.length() - 1));//结果集为空
                }
                break;
            case R.id.main_btn1d://运算结果  =
                getResult();//调用处理结果集的方法
                break;
        }
    }

    //运算结果的方法
    private void getResult(){
        String exp = editText.getText().toString();//获取文本框的内容
        if(exp==null||exp.equals("")){
            return;
        }
        if(!exp.contains(" ")){
            return;
        }
        if(clear_flag){
            clear_flag = false;
            return;
        }
        clear_flag = true;
        double result = 0;

        //进行截取
        //运算符前的数字
        String s1 = exp.substring(0,exp.indexOf(" "));
        //运算符
        String op = exp.substring(exp.indexOf(" ")+1,exp.indexOf(" ")+2);
        //运算符后的数字
        String s2 = exp.substring(exp.indexOf(" ")+3);

        if(!s1.equals("")&&!s2.equals("")) {//如果包含小数点的运算
            double d1 = Double.parseDouble(s1);//则数字都是double类型
            double d2 = Double.parseDouble(s2);
            if (op.equals("+")) {//如果是 +
                result = d1 + d2;
            } else if (op.equals("-")) {
                result = d1 - d2;
            } else if (op.equals("*")) {
                result = d1 * d2;
            } else if (op.equals("/")) {
                if (d2 == 0) { //如果被除数是0
                    result = 0; //则结果是0
                }
                else {//否则执行正常是除法运算
                    result = d1 / d2;
                }
            }

            if (!s1.contains(".") && !s2.contains(".") && !op.equals("/")) {//如果是整数类型
                int r = (int) result; //都是整形
                editText.setText(r + "");
            } else{
                editText.setText(result + "");
            }
        }else if(!s1.equals("") && s2.equals("")){//如果是只输入运算符前的数
            editText.setText(exp);//直接返回当前文本框的内容
        }else if(s1.equals("") && !s2.equals("")){//如果是只输入运算符后面的数
            double d2 = Double.parseDouble(s2);

            //运算符前没有输入数字
            if (op.equals("+")) {
                result = 0 + d2;
            } else if (op.equals("-")) {
                result = 0 - d2;
            } else if (op.equals("*")) {
                result = 0;
            } else if (op.equals("/")) {
                result = 0;
            }

            if (!s1.contains(".") && !s2.contains(".")) {
                int r = (int) result;
                editText.setText(r + "");
            } else{
                editText.setText(result + "");
            }
        }else {
            editText.setText("");
        }

    }
}

说明: 在写方法的时候,让MainActivtiy实现View.OnClickListener 这个接口,在实现点击事件的时候,就可以直接用 this ,例如: main_btn0.setOnClickListener(this);

运行结果:
这里写图片描述

这里写图片描述

样式比较丑,凑合着吧,功能实现出来就好!
Android 实现计算器功能,只是简单的实现。

  • 69
    点赞
  • 441
    收藏
    觉得还不错? 一键收藏
  • 32
    评论
实现一个 Android 计算器需要以下步骤: 1. 创建一个新的 Android 项目,并在 layout 文件夹中创建一个布局文件,用于显示计算器的界面。界面可以包括数字按钮、运算符按钮、清除按钮、等于号按钮和文本框等。 2. 在 MainActivity.java 中编写代码,实现计算器的逻辑功能。可以使用 Java 中的数学运算符和方法来实现计算器的加减乘除等功能。 3. 在 MainActivity.java 中编写代码,实现按钮的点击事件。可以使用 OnClickListener 接口来处理按钮的点击事件。 4. 在 AndroidManifest.xml 文件中添加权限,以允许应用程序访问互联网。 以下是一个简单的 Android 计算器示例代码: 1. 布局文件 activity_main.xml: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/result_textview" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="30sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/button_1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="1" /> <Button android:id="@+id/button_2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="2" /> <Button android:id="@+id/button_3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="3" /> <Button android:id="@+id/button_add" 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/button_4" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="4" /> <Button android:id="@+id/button_5" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="5" /> <Button android:id="@+id/button_6" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="6" /> <Button android:id="@+id/button_subtract" 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/button_7" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="7" /> <Button android:id="@+id/button_8" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="8" /> <Button android:id="@+id/button_9" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="9" /> <Button android:id="@+id/button_multiply" 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/button_clear" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="C" /> <Button android:id="@+id/button_0" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="0" /> <Button android:id="@+id/button_equal" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="=" /> <Button android:id="@+id/button_divide" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="÷" /> </LinearLayout> </LinearLayout> ``` 2. MainActivity.java 文件: ```java public class MainActivity extends AppCompatActivity implements View.OnClickListener { private TextView resultTextView; private Button button1, button2, button3, button4, button5, button6, button7, button8, button9, button0; private Button buttonAdd, buttonSubtract, buttonMultiply, buttonDivide, buttonEqual, buttonClear; private String currentNumber = ""; private String lastNumber = ""; private String lastOperator = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); resultTextView = findViewById(R.id.result_textview); button1 = findViewById(R.id.button_1); button2 = findViewById(R.id.button_2); button3 = findViewById(R.id.button_3); button4 = findViewById(R.id.button_4); button5 = findViewById(R.id.button_5); button6 = findViewById(R.id.button_6); button7 = findViewById(R.id.button_7); button8 = findViewById(R.id.button_8); button9 = findViewById(R.id.button_9); button0 = findViewById(R.id.button_0); buttonAdd = findViewById(R.id.button_add); buttonSubtract = findViewById(R.id.button_subtract); buttonMultiply = findViewById(R.id.button_multiply); buttonDivide = findViewById(R.id.button_divide); buttonEqual = findViewById(R.id.button_equal); buttonClear = findViewById(R.id.button_clear); button1.setOnClickListener(this); button2.setOnClickListener(this); button3.setOnClickListener(this); button4.setOnClickListener(this); button5.setOnClickListener(this); button6.setOnClickListener(this); button7.setOnClickListener(this); button8.setOnClickListener(this); button9.setOnClickListener(this); button0.setOnClickListener(this); buttonAdd.setOnClickListener(this); buttonSubtract.setOnClickListener(this); buttonMultiply.setOnClickListener(this); buttonDivide.setOnClickListener(this); buttonEqual.setOnClickListener(this); buttonClear.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button_0: currentNumber += "0"; break; case R.id.button_1: currentNumber += "1"; break; case R.id.button_2: currentNumber += "2"; break; case R.id.button_3: currentNumber += "3"; break; case R.id.button_4: currentNumber += "4"; break; case R.id.button_5: currentNumber += "5"; break; case R.id.button_6: currentNumber += "6"; break; case R.id.button_7: currentNumber += "7"; break; case R.id.button_8: currentNumber += "8"; break; case R.id.button_9: currentNumber += "9"; break; case R.id.button_add: lastOperator = "+"; lastNumber = currentNumber; currentNumber = ""; break; case R.id.button_subtract: lastOperator = "-"; lastNumber = currentNumber; currentNumber = ""; break; case R.id.button_multiply: lastOperator = "*"; lastNumber = currentNumber; currentNumber = ""; break; case R.id.button_divide: lastOperator = "/"; lastNumber = currentNumber; currentNumber = ""; break; case R.id.button_equal: double result = calculateResult(); resultTextView.setText(String.valueOf(result)); currentNumber = String.valueOf(result); lastOperator = ""; lastNumber = ""; break; case R.id.button_clear: currentNumber = ""; lastNumber = ""; lastOperator = ""; resultTextView.setText(""); break; } resultTextView.setText(currentNumber); } private double calculateResult() { double result = 0.0; double current = Double.parseDouble(currentNumber); double last = Double.parseDouble(lastNumber); switch (lastOperator) { case "+": result = last + current; break; case "-": result = last - current; break; case "*": result = last * current; break; case "/": result = last / current; break; } return result; } } ``` 在 AndroidManifest.xml 文件中添加以下权限: ```xml <uses-permission android:name="android.permission.INTERNET" /> ``` 这样,就可以实现一个简单的 Android 计算器了。
评论 32
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值