广告
返回顶部
首页 > 资讯 > 精选 >Android studio怎么制作简易计算器功能
  • 820
分享到

Android studio怎么制作简易计算器功能

2023-06-30 15:06:04 820人浏览 泡泡鱼
摘要

这篇文章主要介绍“Android studio怎么制作简易计算器功能”,在日常操作中,相信很多人在Android studio怎么制作简易计算器功能问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家

这篇文章主要介绍“Android studio怎么制作简易计算器功能”,在日常操作中,相信很多人在Android studio怎么制作简易计算器功能问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Android studio怎么制作简易计算器功能”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

布局如下:

Android studio怎么制作简易计算器功能

布局文件代码:

<?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:id="@+id/mc"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".homework_3">    <EditText        android:id="@+id/editText"        android:layout_width="match_parent"        android:layout_height="110dp"        android:ems="10"        android:inputType="textPersonName"        android:hint="请输入" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1"        android:orientation="horizontal">        <Button            android:id="@+id/button_mc"            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_margin="2dp"            android:layout_weight="1"            android:insetTop="0dp"            android:insetBottom="0dp"            android:text="mc" />        <Button            android:id="@+id/button_mjia"            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_margin="2dp"            android:layout_weight="1"            android:insetTop="0dp"            android:insetBottom="0dp"            android:text="m+" />        <Button            android:id="@+id/button_mjian"            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_margin="2dp"            android:layout_weight="1"            android:insetTop="0dp"            android:insetBottom="0dp"            android:text="m-" />        <Button            android:id="@+id/button_mr"            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_margin="2dp"            android:layout_weight="1"            android:insetTop="0dp"            android:insetBottom="0dp"            android:text="mr" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1"        android:orientation="horizontal">        <Button            android:id="@+id/button_c"            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_margin="2dp"            android:layout_weight="1"            android:insetTop="0dp"            android:insetBottom="0dp"            android:text="c" />        <Button            android:id="@+id/button_xing"            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_margin="2dp"            android:layout_weight="1"            android:insetTop="0dp"            android:insetBottom="0dp"            android:text="*" />        <Button            android:id="@+id/button_chu"            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_margin="2dp"            android:layout_weight="1"            android:insetTop="0dp"            android:insetBottom="0dp"            android:text="/" />        <Button            android:id="@+id/button_xiaoyu"            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_margin="2dp"            android:layout_weight="1"            android:insetTop="0dp"            android:insetBottom="0dp"            android:text="&lt;" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1"        android:orientation="horizontal">        <Button            android:id="@+id/button_7"            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_margin="2dp"            android:layout_weight="1"            android:insetTop="0dp"            android:insetBottom="0dp"            android:text="7" />        <Button            android:id="@+id/button_8"            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_margin="2dp"            android:layout_weight="1"            android:insetTop="0dp"            android:insetBottom="0dp"            android:text="8" />        <Button            android:id="@+id/button_9"            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_margin="2dp"            android:layout_weight="1"            android:insetTop="0dp"            android:insetBottom="0dp"            android:text="9" />        <Button            android:id="@+id/button_jian"            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_margin="2dp"            android:layout_weight="1"            android:insetTop="0dp"            android:insetBottom="0dp"            android:text="-" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1"        android:orientation="horizontal">        <Button            android:id="@+id/button_4"            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_margin="2dp"            android:layout_weight="1"            android:insetTop="0dp"            android:insetBottom="0dp"            android:text="4" />        <Button            android:id="@+id/button_5"            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_margin="2dp"            android:layout_weight="1"            android:insetTop="0dp"            android:insetBottom="0dp"            android:text="5" />        <Button            android:id="@+id/button_6"            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_margin="2dp"            android:layout_weight="1"            android:insetTop="0dp"            android:insetBottom="0dp"            android:text="6" />        <Button            android:id="@+id/button_jia"            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_margin="2dp"            android:layout_weight="1"            android:insetTop="0dp"            android:insetBottom="0dp"            android:text="+" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="2"        android:orientation="horizontal">        <LinearLayout            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="3"            android:orientation="horizontal">            <LinearLayout                android:layout_width="match_parent"                android:layout_height="match_parent"                android:layout_weight="1"                android:orientation="vertical">                <LinearLayout                    android:layout_width="match_parent"                    android:layout_height="match_parent"                    android:layout_weight="1"                    android:orientation="horizontal">                    <Button                        android:id="@+id/button_1"                        android:layout_width="0dp"                        android:layout_height="match_parent"                        android:layout_margin="2dp"                        android:layout_weight="1"                        android:insetTop="0dp"                        android:insetBottom="0dp"                        android:text="1" />                    <Button                        android:id="@+id/button_2"                        android:layout_width="0dp"                        android:layout_height="match_parent"                        android:layout_margin="2dp"                        android:layout_weight="1"                        android:insetTop="0dp"                        android:insetBottom="0dp"                        android:text="2" />                    <Button                        android:id="@+id/button_3"                        android:layout_width="0dp"                        android:layout_height="match_parent"                        android:layout_margin="2dp"                        android:layout_weight="1"                        android:insetTop="0dp"                        android:insetBottom="0dp"                        android:text="3" />                </LinearLayout>                <LinearLayout                    android:layout_width="match_parent"                    android:layout_height="match_parent"                    android:layout_weight="1"                    android:orientation="horizontal">                    <Button                        android:id="@+id/button_0"                        android:layout_width="0dp"                        android:layout_height="match_parent"                        android:layout_margin="2dp"                        android:layout_weight="2"                        android:insetTop="0dp"                        android:insetBottom="0dp"                        android:text="0" />                    <Button                        android:id="@+id/button_dian"                        android:layout_width="0dp"                        android:layout_height="match_parent"                        android:layout_margin="2dp"                        android:layout_weight="1"                        android:insetTop="0dp"                        android:insetBottom="0dp"                        android:text="." />                </LinearLayout>            </LinearLayout>        </LinearLayout>        <LinearLayout            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="1"            android:orientation="horizontal">            <Button                android:id="@+id/button_dengyu"                android:layout_width="wrap_content"                android:layout_height="match_parent"                android:layout_margin="2dp"                android:layout_weight="1"                android:insetTop="0dp"                android:insetBottom="0dp"                android:text="=" />        </LinearLayout>    </LinearLayout></LinearLayout>

JAVA代码:

package com.example.a01_helloworld;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.os.Debug;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.EditText;import com.Google.android.material.badge.BadgeUtils;public class homework_3 extends AppCompatActivity implements View.OnClickListener{    //创建button对象    Button button_mc; //mc    Button button_mjia; //m+    Button button_mjian; //m-    Button button_mr; //mr    Button button_c; //c    Button button_xing; //*    Button button_chu; // /    Button button_xiaoyu; // <    Button button_7; // 7    Button button_8; //8    Button button_9; //9    Button button_jian; // -    Button button_4; // 4    Button button_5; // 5    Button button_6; // 6    Button button_jia; // +    Button button_1; // 1    Button button_2; // 2    Button button_3; // 3    Button button_0; // 0    Button button_dian; // .    Button button_dengyu; // =    EditText result;    // 判断文本编辑框中是否清空    boolean is_clear;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_homework_3);        // 实例化对象        button_mc = (Button)findViewById(R.id.button_mc);        button_mjia = (Button)findViewById(R.id.button_mjia);        button_mjian = (Button)findViewById(R.id.button_mjian);        button_mr = (Button)findViewById(R.id.button_mr);        button_c = (Button)findViewById(R.id.button_c);        button_xing = (Button)findViewById(R.id.button_xing);        button_chu = (Button)findViewById(R.id.button_chu);        button_xiaoyu = (Button)findViewById(R.id.button_xiaoyu);        button_7 = (Button)findViewById(R.id.button_7);        button_8 = (Button)findViewById(R.id.button_8);        button_9 = (Button)findViewById(R.id.button_9);        button_jian = (Button)findViewById(R.id.button_jian);        button_4 = (Button)findViewById(R.id.button_4);        button_5 = (Button)findViewById(R.id.button_5);        button_6 = (Button)findViewById(R.id.button_6);        button_jia = (Button)findViewById(R.id.button_jia);        button_1 = (Button)findViewById(R.id.button_1);        button_2 = (Button)findViewById(R.id.button_2);        button_3 = (Button)findViewById(R.id.button_3);        button_0 = (Button)findViewById(R.id.button_0);        button_dian = (Button)findViewById(R.id.button_dian);        button_dengyu = (Button)findViewById(R.id.button_dengyu);        result = (EditText)findViewById(R.id.editText);        // 给按钮设置点击对象        button_mc.setOnClickListener(this);        button_mjia.setOnClickListener(this);        button_mjian.setOnClickListener(this);        button_mr.setOnClickListener(this);        button_c.setOnClickListener(this);        button_xing.setOnClickListener(this);        button_chu.setOnClickListener(this);        button_xiaoyu.setOnClickListener(this);        button_7.setOnClickListener(this);        button_8.setOnClickListener(this);        button_9.setOnClickListener(this);        button_jian.setOnClickListener(this);        button_4.setOnClickListener(this);        button_5.setOnClickListener(this);        button_6.setOnClickListener(this);        button_jia.setOnClickListener(this);        button_1.setOnClickListener(this);        button_2.setOnClickListener(this);        button_3.setOnClickListener(this);        button_0.setOnClickListener(this);        button_dian.setOnClickListener(this);        button_dengyu.setOnClickListener(this);    }    @Override    public void onClick(View v) {        String str = result.getText().toString();        switch (v.getId()){            case R.id.button_0:            case R.id.button_1:            case R.id.button_2:            case R.id.button_3:            case R.id.button_4:            case R.id.button_5:            case R.id.button_6:            case R.id.button_7:            case R.id.button_8:            case R.id.button_9:                if(result.length()==0){                    str = "";                    result.setText("");                }                result.setText(str+((Button)v).getText());                break;            case R.id.button_dian:                if(result.length()==0){                    return;                }                else {                    Log.d("homework_3","aaa");                    result.setText(str+((Button)v).getText());                    break;                }            case R.id.button_xiaoyu:                if (is_clear) {                    is_clear =false ;                    str ="" ;                    result.setText("");                }else if (str!=null&&!str.equals("")){                    result.setText(str.substring(0,str.length()-1));                }                break;            case R.id.button_jia:            case R.id.button_jian:            case R.id.button_xing:            case R.id.button_chu:                if (result.length()==0)                {                    str = "";                    result.setText("");                }                result.setText(str+" "+((Button)v).getText()+" ");                break;            case R.id.button_c:                is_clear = false;                result.setText("");            case R.id.button_dengyu:                Log.d("homework_3","=");                getResult();                break;        }    }    private void getResult(){        String exp = result.getText().toString();        if (exp==null||exp.equals(""))        {            return;        }        if(!exp.contains(" "))        {            return;        }        if (is_clear)        {            is_clear = false;            return;        }        is_clear = true;        double Result = 0;        // public int indexOf(int ch): 返回指定字符在字符串中第一次出现处的索引        String s1 = exp.substring(0,exp.indexOf(" "));//找到第一个数字        Log.d("homework_3",s1);        String op = exp.substring(exp.indexOf(" ")+1,exp.indexOf(" ")+2);//找到运算符        Log.d("homework_3",op);        String s2 = exp.substring(exp.indexOf(" ")+3);//找到第二个数字        Log.d("homework_3",s2);        if (!s1.equals(" ")&&!s2.equals(" ")){            double d1 = Double.parseDouble(s1);            double d2 = Double.parseDouble(s2);            if(op.equals("+")){                Result = d1+d2;                String o = String.valueOf(Result);                Log.d("homework_3",o);                result.setText(o);                return;            }else if (op.equals("-")){                Result = d1-d2;                String o = String.valueOf(Result);                Log.d("homework_3",o);                result.setText(o);                return;            }else if (op.equals("*")){                Result = d1*d2;                String o = String.valueOf(Result);                Log.d("homework_3",o);                result.setText(o);                return;            }else if (op.equals("/")){                Result = d1/d2;                String o = String.valueOf(Result);                Log.d("homework_3",o);                result.setText(o);                return;            }        }    }}

到此,关于“Android studio怎么制作简易计算器功能”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!

--结束END--

本文标题: Android studio怎么制作简易计算器功能

本文链接: https://www.lsjlt.com/news/329814.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Android studio怎么制作简易计算器功能
    这篇文章主要介绍“Android studio怎么制作简易计算器功能”,在日常操作中,相信很多人在Android studio怎么制作简易计算器功能问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家...
    99+
    2023-06-30
  • 用Android studio实现简易计算器功能
    用Android studio做一个简易计算器,供大家参考,具体内容如下 长话短说,先建立一个Android项目; 创建完成后打开activity_main.xml,构建我们的应...
    99+
    2022-11-13
  • Android Studio简易计算器
    目录 第一步,创建新项目 第二步,设计UI 第三步,实现计算逻辑 第四步,测试应用程序 随着移动互联网的普及,手机应用程序已经成为人们生活中不可或缺的一部分。计算器是一类被广泛使用的应用程序之一,因此学习如何开发一款简易的计算器应用程序是...
    99+
    2023-09-17
    android studio android
  • 【Android Studio】简易计算器
    简易计算器要求: 1,操作简单,易于掌握,界面简单。 2.方便进行加,减,乘,除等操作。数字保留小数点后两位。 3.包含小数点运算和输入回退功能。 4.能够进行多次叠加运算。 5.系统能够进行多次叠加...
    99+
    2023-09-21
    android studio python android
  • Androidstudio制作简易计算器功能
    本文实例为大家分享了Android studio制作简易计算器的具体代码,供大家参考,具体内容如下 布局如下: 布局文件代码: <xml version="1.0" enco...
    99+
    2022-11-13
  • Python制作简易计算器功能
    本文实例为大家分享了Python制作简易计算器的具体代码,供大家参考,具体内容如下 简易计算器 简易计算器功能: 实现输入,计算,输出功能;可以计算基本的加减乘除功能;处理除法中除数...
    99+
    2022-11-10
  • Android studio如何实现简易的计算器功能
    这篇文章主要讲解了“Android studio如何实现简易的计算器功能”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Android studio如何实现简易的计算器功能...
    99+
    2023-06-30
  • Android Studio怎么实现简易计算器设计
    今天小编给大家分享一下Android Studio怎么实现简易计算器设计的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下...
    99+
    2023-06-30
  • Android Studio怎么实现简易计算器App
    本篇内容主要讲解“Android Studio怎么实现简易计算器App”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Android Studio怎么实现简易计算器App”吧!...
    99+
    2023-06-30
  • Android Studio实现简易计算器设计
    本文实例为大家分享了Android Studio实现简易计算器的具体代码,供大家参考,具体内容如下 一、题目 1、如图所示(实际设计,类似此界面样式即可,全屏时,按钮将会纵向拉伸),...
    99+
    2022-11-13
  • Android实现简易计算器功能
    本项目为大家分享了Android实现计算器功能的具体代码,供大家参考,具体内容如下 项目介绍 练手项目。能实现加减乘除及括号运算。 开发思路 界面布局  1.界面布...
    99+
    2022-11-12
  • android studio实现简易的计算器
    本文实例为大家分享了android studio实现简易计算器的具体代码,供大家参考,具体内容如下 先看效果图 基本功能:加,减,乘,除 核心代码实现 public class M...
    99+
    2022-11-13
  • Android Studio实现简单计算器功能
    本文实例为大家分享了Android Studio实现简单计算器功能的具体代码,供大家参考,具体内容如下 程序步骤: (1)在布局文件定义一些计算器界面的文本框,按钮等组件。 (...
    99+
    2022-06-06
    Android Studio studio Android
  • Android实现简易计算功能
    本文实例为大家分享了Android实现简易计算功能的具体代码,供大家参考,具体内容如下 效果如图: activity_main.xml <?xml version...
    99+
    2022-11-12
  • Android Studio实现简易计算器源码
    本文实例为大家分享了Android Studio实现简易计算器的具体代码,供大家参考,具体内容如下 效果图: 源码: 布局样式: <xml version="1.0" en...
    99+
    2022-11-13
  • Android Studio如何实现简易进制转换计算器
    这篇文章主要介绍了Android Studio如何实现简易进制转换计算器的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Android Studio如何实现简易进制转换计算器文章都会有所收获,...
    99+
    2023-06-30
  • android studio怎么实现简单的计算器小功能
    这篇文章主要介绍了android studio怎么实现简单的计算器小功能的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇android studio怎么实现简单的计算器小功能文章都会有所收获,...
    99+
    2023-06-30
  • android studio如何实现简易的计算器
    本篇内容主要讲解“android studio如何实现简易的计算器”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“android studio如何实现简易的计算器”吧!先看效果...
    99+
    2023-06-30
  • js制作简易计算器
    本文实例为大家分享了js制作简易计算器的具体代码,供大家参考,具体内容如下 要制作一个如图所示的简易计算器,首先要建立一个表单,制作出如图所示的样子。 <table bo...
    99+
    2022-11-12
  • Android Studio开发实现简单计算器功能
    本文实例为大家分享了Android Studio开发实现简单计算器的具体代码,供大家参考,具体内容如下 代码: activity_3.xml <xml version="1.0...
    99+
    2022-11-13
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作