Android实现计算器功能

.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#D8ECF3">
  <TextView
        android:gravity="bottom|right"
        android:textSize="70dp"
        android:singleLine="true"
        android:layout_margin="15dp"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:background="@drawable/white"
        android:id="@+id/textView"/>


    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp">

        <Button
            android:id="@+id/btn_clean"
            android:layout_marginLeft="10dp"
            android:background="@drawable/orange"
            android:gravity="center"
            android:text="C"
            android:textSize="25sp" />

        <Button
            android:id="@+id/btn_del"
            android:layout_marginLeft="10dp"
            android:layout_span="2"
            android:background="@drawable/gray"
            android:gravity="center"
            android:text="Del"
            android:textSize="25sp" />

        <Button
            android:id="@+id/btn_divide"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:background="@drawable/gray"
            android:gravity="center"
            android:layout_span="1"
            android:text="/"
            android:textSize="25sp" />

    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp">

        <Button
            android:id="@+id/btn_7"
            android:layout_marginLeft="10dp"
            android:background="@drawable/white"
            android:gravity="center"
            android:text="7"
            android:textSize="25sp" />

        <Button
            android:id="@+id/btn_8"
            android:layout_marginLeft="10dp"
            android:background="@drawable/white"
            android:gravity="center"
            android:text="8"
            android:textSize="25sp" />

        <Button
            android:id="@+id/btn_9"
            android:layout_marginLeft="10dp"
            android:background="@drawable/white"
            android:gravity="center"
            android:text="9"
            android:textSize="25sp" />

        <Button
            android:id="@+id/btn_multiply"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:background="@drawable/gray"
            android:gravity="center"
            android:text="*"
            android:textSize="25sp" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp">

        <Button
            android:id="@+id/btn_4"
            android:layout_marginLeft="10dp"
            android:background="@drawable/white"
            android:gravity="center"
            android:text="4"
            android:textSize="25sp" />

        <Button
            android:id="@+id/btn_5"
            android:layout_marginLeft="10dp"
            android:background="@drawable/white"
            android:gravity="center"
            android:text="5"
            android:textSize="25sp" />

        <Button
            android:id="@+id/btn_6"
            android:layout_marginLeft="10dp"
            android:background="@drawable/white"
            android:gravity="center"
            android:text="6"
            android:textSize="25sp" />

        <Button
            android:id="@+id/btn_add"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:background="@drawable/gray"
            android:gravity="center"
            android:text="+"
            android:textSize="25sp" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp">

        <Button
            android:id="@+id/btn_1"
            android:layout_marginLeft="10dp"
            android:background="@drawable/white"
            android:gravity="center"
            android:text="1"
            android:textSize="25sp" />

        <Button
            android:id="@+id/btn_2"
            android:layout_marginLeft="10dp"
            android:background="@drawable/white"
            android:gravity="center"
            android:text="2"
            android:textSize="25sp" />

        <Button
            android:id="@+id/btn_3"
            android:layout_marginLeft="10dp"
            android:background="@drawable/white"
            android:gravity="center"
            android:text="3"
            android:textSize="25sp" />

        <Button
            android:id="@+id/btn_minus"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:background="@drawable/gray"
            android:gravity="center"
            android:text="-"
            android:textSize="25sp" />

    </TableRow>



    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp">

        <Button
            android:id="@+id/btn_0"
            android:layout_marginLeft="10dp"
            android:layout_span="2"
            android:background="@drawable/white"
            android:gravity="center"
            android:text="0"
            android:textSize="25sp" />

        <Button
            android:id="@+id/btn_point"
            android:layout_marginLeft="10dp"
            android:layout_span="1"
            android:background="@drawable/white"
            android:gravity="center"
            android:text="."
            android:textSize="25sp" />

        <Button
            android:id="@+id/btn_equal"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_span="1"
            android:background="@drawable/gray"
            android:gravity="center"
            android:text="="
            android:textSize="25sp" />


    </TableRow>


</TableLayout>

.java文件:

package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class dj_Activity extends AppCompatActivity {
 
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dj_);
        btn_0 = findViewById(R.id.btn_0);        //初始化
        btn_1 = findViewById(R.id.btn_1);
        btn_2 = findViewById(R.id.btn_2);
        btn_3 = findViewById(R.id.btn_3);
        btn_4 = findViewById(R.id.btn_4);
        btn_5 = findViewById(R.id.btn_5);
        btn_6 = findViewById(R.id.btn_6);
        btn_7 = findViewById(R.id.btn_7);
        btn_8 = findViewById(R.id.btn_8);
        btn_9 = findViewById(R.id.btn_9);
        btn_multiply = findViewById(R.id.btn_multiply);
        btn_divide = findViewById(R.id.btn_divide);
        btn_add = findViewById(R.id.btn_add);
        btn_minus = findViewById(R.id.btn_minus);
        btn_point = findViewById(R.id.btn_point);
        btn_del =findViewById(R.id.btn_del);
        btn_equal = findViewById(R.id.btn_equal);
        btn_clean = findViewById(R.id.btn_clean);

        textView = findViewById(R.id.textView);

        btn_0.setOnClickListener(this);             //设置按钮的点击事件
        btn_1.setOnClickListener(this);
        btn_2.setOnClickListener(this);
        btn_3.setOnClickListener(this);
        btn_4.setOnClickListener(this);
        btn_5.setOnClickListener(this);
        btn_6.setOnClickListener(this);
        btn_7.setOnClickListener(this);
        btn_8.setOnClickListener(this);
        btn_9.setOnClickListener(this);
        btn_minus.setOnClickListener(this);
        btn_multiply.setOnClickListener(this);
        btn_del.setOnClickListener(this);
        btn_divide.setOnClickListener(this);
        btn_point.setOnClickListener(this);
        btn_add.setOnClickListener(this);
        btn_equal.setOnClickListener(this);
        btn_clean.setOnClickListener(this);
    }

    public void onClick(View v) {
        String str = textView.getText().toString();
        switch(v.getId ()){
            case R.id.btn_0:
            case R.id.btn_1:
            case R.id.btn_2:
            case R.id.btn_3:
            case R.id.btn_4:
            case R.id.btn_5:
            case R.id.btn_6:
            case R.id.btn_7:
            case R.id.btn_8:
            case R.id.btn_9:
            case R.id.btn_point:
                if(clear_flag){
                    clear_flag=false;
                    str="";
                    textView.setText ("");
                }
                textView.setText(str+((Button)v).getText ());
                break;

            case R.id.btn_add:
            case R.id.btn_minus:
            case R.id.btn_multiply:
            case R.id.btn_divide:
                if(clear_flag){
                    clear_flag=false;
                    textView.setText("");
                }
                textView.setText(str+" "+((Button)v).getText()+" ");
                break;
            case R.id.btn_del:
                if(clear_flag){
                    clear_flag=false;
                    textView.setText ("");
                }else if (str != null && !str.equals ("")){
                    textView.setText(str.substring(0,str.length()-1));    //删除一个字符
                }
                break;
            case R.id.btn_clean:
                clear_flag=false;
                str = "";
                textView.setText("");        //清空文本内容
                break;
            case R.id.btn_equal:
                getResult();              //获取结果
                break;
        }
    }

    private void getResult() {                            //算法
        String s = textView.getText().toString();
        if(s == null  || s.equals ("")){
            return;
        }
        if (!s.contains ("")){
            return;
        }
        if (clear_flag){
            clear_flag=false;
            return;
        }
        clear_flag=true;

        String str1 = s.substring(0,s.indexOf(" "));                      // 获取到运算符前面的字符
        String str_y = s.substring(s.indexOf(" ")+1,s.indexOf(" ")+2);    //获取到运算符
        String str2 = s.substring(s.indexOf(" ")+ 3);                     //获取到运算符后面的字符

        double result = 0;
        if (!str1.equals ("")  && !str2.equals ("")){
            double num1 = Double.parseDouble(str1);   //将str1、str2强制转化为double类型
            double num2 = Double.parseDouble(str2);

            if (str_y.equals ("+")){
                result = num1 + num2;
            }else if (str_y.equals ("-")){
                result = num1 - num2;
            }else if (str_y.equals ("÷")){
                if (num2 == 0){
                    result = 0;
                }else {
                    result = num1/num2;
                }
            }else if (str_y.equals ("*")){
                result = num1*num2;
            }
            if (!str1.contains (".") && !str2.contains (".") && !s.equals ("÷")){
                int k = (int) result;               //强制转换
                textView.setText (k);
            }else{
                textView.setText (result+"");
            }
        }else if (!str1.equals ("") && str2.equals ("")){
            textView.setText (s);
        }else if (str1.equals ("") && !str2.equals ("")){
            double num2 = Double.parseDouble(str2);
            if (s.equals ("+")){
                result = 0 + num2;
            }else if (s.equals("-")){
                result = 0 - num2;
            }else if (s.equals("×")){
                result = 0;
            }else if (s.equals("÷")){
                result = 0;
            }
            if (!str2.contains (".")) {
                int r = (int) result;
                textView.setText (r + "");
            } else {
                textView.setText (result + "");
            }
        } else {
            textView.setText ("");
        }
    }
}

截图:

在这里插入图片描述

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现一个 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 计算器了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值