使用Android Studio实现简单的计算器(已亲自实验通过)_使用多个activity实现分别实现加、减、乘、除的计算器,具体包括:通过一个启动ac-程序员宅基地

技术标签: AndroidStudio  android studio  

使用Android Studio实现简单的计算器(已亲自实验通过)

实验内容

用多个Activity实现分别实现加、减、乘、除的计算器,具体包括:通过一个启动Activity布置4个按钮,每个按钮分别触发加、减、乘、除的Activity;然后在打开的Activity中实现与实验一类似的页面,以一个按钮触发计算输出。

实验分析

用多个Activity实现分别实现加、减、乘、除的计算器,具体包括:通过一个启动Activity布置4个按钮,每个按钮分别触发加、减、乘、除的Activity;然后在打开的Activity中实现与实验一类似的页面,以一个按钮触发计算输出。

实验效果界面

实验步骤

  1. 创建一个新项目

  2. 编辑layout文件activity_main.xml
<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    android:background="#000000" >


    <EditText
        android:id="@+id/et_input"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:background="@drawable/white"
        android:editable="false"
        android:paddingBottom="5dp"
        android:textSize="30sp" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:orientation="horizontal"
        android:gravity="center_horizontal">

        <Button
            android:id="@+id/btn_clr"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:text="C"
            android:textSize="30sp"
            android:paddingRight="15sp"
            android:paddingBottom="15sp"
            android:background="@drawable/selector"/>

        <Button
            android:id="@+id/btn_del"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:text=""
            android:textSize="30sp"
            android:layout_marginLeft="10dp"
            android:paddingRight="15sp"
            android:paddingBottom="15sp"
            android:background="@drawable/selector"/>

        <Button
            android:id="@+id/btn_div"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:text="÷"
            android:textSize="30sp"
            android:layout_marginLeft="10dp"
            android:paddingRight="15sp"
            android:paddingBottom="15sp"
            android:background="@drawable/selector"/>

        <Button
            android:id="@+id/btn_mul"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:text="×"
            android:textSize="30sp"
            android:layout_marginLeft="10dp"
            android:paddingRight="15sp"
            android:paddingBottom="15sp"
            android:background="@drawable/selector"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal"
        android:gravity="center_horizontal"
        >
        <Button
            android:id="@+id/btn_7"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:text="7"
            android:textSize="30sp"
            android:paddingRight="15sp"
            android:paddingBottom="15sp"
            android:background="@drawable/selector"/>
        <Button
            android:id="@+id/btn_8"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:text="8"
            android:textSize="30sp"
            android:layout_marginLeft="10dp"
            android:paddingRight="15sp"
            android:paddingBottom="15sp"
            android:background="@drawable/selector"/>
        <Button
            android:id="@+id/btn_9"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:text="9"
            android:textSize="30sp"
            android:layout_marginLeft="10dp"
            android:paddingRight="15sp"
            android:paddingBottom="15sp"
            android:background="@drawable/selector"/>
        <Button
            android:id="@+id/btn_sub"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:text="-"
            android:textSize="30sp"
            android:layout_marginLeft="10dp"
            android:paddingRight="15sp"
            android:paddingBottom="15sp"
            android:background="@drawable/selector" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal"
        android:gravity="center_horizontal"
        >
        <Button
            android:id="@+id/btn_4"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:text="4"
            android:textSize="30sp"
            android:paddingRight="15sp"
            android:paddingBottom="15sp"
            android:background="@drawable/selector"/>
        <Button
            android:id="@+id/btn_5"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:text="5"
            android:textSize="30sp"
            android:layout_marginLeft="10dp"
            android:paddingRight="15sp"
            android:paddingBottom="15sp"
            android:background="@drawable/selector"/>
        <Button
            android:id="@+id/btn_6"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:text="6"
            android:textSize="30sp"
            android:layout_marginLeft="10dp"
            android:paddingRight="15sp"
            android:paddingBottom="15sp"
            android:background="@drawable/selector"/>
        <Button
            android:id="@+id/btn_add"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:text="+"
            android:textSize="30sp"
            android:layout_marginLeft="10dp"
            android:paddingRight="15sp"
            android:paddingBottom="15sp"
            android:background="@drawable/selector"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="10dp"
        android:gravity="center_horizontal">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                >
                <Button
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:id="@+id/btn_1"
                    android:text="1"
                    android:textSize="30sp"
                    android:paddingRight="15sp"
                    android:paddingBottom="15sp"
                    android:background="@drawable/selector" />
                <Button
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:id="@+id/btn_2"
                    android:text="2"
                    android:textSize="30sp"
                    android:layout_marginLeft="10dp"
                    android:paddingRight="15sp"
                    android:paddingBottom="15sp"
                    android:background="@drawable/selector" />
                <Button
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:id="@+id/btn_3"
                    android:text="3"
                    android:textSize="30sp"
                    android:layout_marginLeft="10dp"
                    android:paddingRight="15sp"
                    android:paddingBottom="15sp"
                    android:background="@drawable/selector" />
            </LinearLayout>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_marginTop="10dp">
                <Button
                    android:layout_width="170dp"
                    android:layout_height="80dp"
                    android:id="@+id/btn_0"
                    android:text="0"
                    android:textSize="30sp"
                    android:paddingRight="15sp"
                    android:paddingBottom="15sp"
                    android:background="@drawable/selector"/>
                <Button
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:id="@+id/btn_pt"
                    android:text="."
                    android:textSize="30sp"
                    android:layout_marginLeft="10dp"
                    android:paddingRight="15sp"
                    android:paddingBottom="15sp"
                    android:background="@drawable/selector"/>
            </LinearLayout>

        </LinearLayout>

        <Button
            android:id="@+id/btn_eq"
            android:layout_width="80dp"
            android:layout_height="170dp"
            android:layout_marginLeft="10dp"
            android:text="="
            android:textSize="30sp"
            android:paddingRight="15sp"
            android:paddingBottom="15sp"
            android:background="@drawable/button_bg"/>

    </LinearLayout>

</LinearLayout>
  1. 新建drawable文件,名为white.xml(为了产生点击效果,点击前为白色)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <corners
        android:radius="5dp"/>
    <solid
        android:color="#ffffffff"/>
</shape>

  1. 新建drawable文件,名为gray.xml(为了产生点击效果,点击后为灰色)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <corners
        android:radius="5dp"/>
    <solid
        android:color="#ffDEDEDE"/>
</shape>
  1. 新建drawable文件,名为selector.xml(实现点击效果文件)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/gray"
        android:state_pressed="true"/>
    <item android:drawable="@drawable/white"/>
</selector>
  1. 新建drawable文件,名为button_bg.xml(添加=按钮背景颜色)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
        <!-- 背景色 -->
        <solid android:color="#FFB400"/>
         <corners
             android:radius="5dp"/>
</shape>
  1. 编写MainActivity.java(功能实现代码)
package com.example.test2_calculator;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import com.example.test2_calculator.R;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    

    //创建Button对象   也就是activity_main.xml里所设置的ID
    Button btn_0,btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9,btn_pt;
    Button btn_mul,btn_div,btn_add,btn_sub;
    Button btn_clr,btn_del,btn_eq;
    EditText et_input;
    boolean clr_flag;    //判断et编辑文本框中是否清空

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //实例化对象
        setContentView(R.layout.activity_main);
        btn_0= (Button) findViewById(R.id.btn_0);
        btn_1= (Button) findViewById(R.id.btn_1);
        btn_2= (Button) findViewById(R.id.btn_2);
        btn_3= (Button) findViewById(R.id.btn_3);
        btn_4= (Button) findViewById(R.id.btn_4);
        btn_5= (Button) findViewById(R.id.btn_5);
        btn_6= (Button) findViewById(R.id.btn_6);
        btn_7= (Button) findViewById(R.id.btn_7);
        btn_8= (Button) findViewById(R.id.btn_8);
        btn_9= (Button) findViewById(R.id.btn_9);
        btn_pt= (Button) findViewById(R.id.btn_pt);
        btn_add= (Button) findViewById(R.id.btn_add);
        btn_sub= (Button) findViewById(R.id.btn_sub);
        btn_mul= (Button) findViewById(R.id.btn_mul);
        btn_div= (Button) findViewById(R.id.btn_div);
        btn_clr= (Button) findViewById(R.id.btn_clr);
        btn_del= (Button) findViewById(R.id.btn_del);
        btn_eq= (Button) findViewById(R.id.btn_eq);
        et_input= (EditText) findViewById(R.id.et_input);

        //给按钮设置的点击事件
        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_pt.setOnClickListener(this);
        btn_add.setOnClickListener(this);
        btn_sub.setOnClickListener(this);
        btn_mul.setOnClickListener(this);
        btn_div.setOnClickListener(this);
        btn_clr.setOnClickListener(this);
        btn_del.setOnClickListener(this);
        btn_eq.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
    
        String str=et_input.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_pt:
                if(clr_flag){
    
                    clr_flag=false;
                    str="";
                    et_input.setText("");
                }
                et_input.setText(str+((Button)v).getText());
                break;
            case R.id.btn_add:
            case R.id.btn_sub:
            case R.id.btn_mul:
            case R.id.btn_div:
                if(clr_flag){
    
                    clr_flag=false;
                    str="";
                    et_input.setText("");
                }
                if(str.contains("+")||str.contains("-")||str.contains("×")||str.contains("÷")) {
    
                    str=str.substring(0,str.indexOf(" "));
                }
                et_input.setText(str+" "+((Button)v).getText()+" ");
                break;
            case R.id.btn_clr:
                if(clr_flag)
                    clr_flag=false;
                str="";
                et_input.setText("");
                break;
            case R.id.btn_del: //判断是否为空,然后在进行删除
                if(clr_flag){
    
                    clr_flag=false;
                    str="";
                    et_input.setText("");
                }
                else if(str!=null&&!str.equals("")){
    
                    et_input.setText(str.substring(0,str.length()-1));
                }
                break;
            case R.id.btn_eq: //单独运算最后结果
                getResult();//调用下面的方法
                break;
        }
    }

    private void getResult() {
    
        String exp=et_input.getText().toString();
        if(exp==null||exp.equals("")) return ;
        //因为没有运算符所以不用运算
        if(!exp.contains(" ")){
    
            return ;
        }
        if(clr_flag){
    
            clr_flag=false;
            return;
        }
        clr_flag=true;
        //截取运算符前面的字符串
        String s1=exp.substring(0,exp.indexOf(" "));
        //截取的运算符
        String op=exp.substring(exp.indexOf(" ")+1,exp.indexOf(" ")+2);
        //截取运算符后面的字符串
        String s2=exp.substring(exp.indexOf(" ")+3);
        double cnt=0;
        if(!s1.equals("")&&!s2.equals("")){
    
            double d1=Double.parseDouble(s1);
            double d2=Double.parseDouble(s2);
            if(op.equals("+")){
    
                cnt=d1+d2;
            }
            if(op.equals("-")){
    
                cnt=d1-d2;
            }
            if(op.equals("×")){
    
                cnt=d1*d2;
            }
            if(op.equals("÷")){
    
                if(d2==0) cnt=0;
                else cnt=d1/d2;
            }
            if(!s1.contains(".")&&!s2.contains(".")&&!op.equals("÷")) {
    
                int res = (int) cnt;
                et_input.setText(res+"");
            }else {
    
                et_input.setText(cnt+"");}
        }
        //如果s1是空    s2不是空  就执行下一步
        else if(!s1.equals("")&&s2.equals("")){
    
            double d1=Double.parseDouble(s1);
            if(op.equals("+")){
    
                cnt=d1;
            }
            if(op.equals("-")){
    
                cnt=d1;
            }
            if(op.equals("×")){
    
                cnt=0;
            }
            if(op.equals("÷")){
    
                cnt=0;
            }
            if(!s1.contains(".")) {
    
                int res = (int) cnt;
                et_input.setText(res+"");
            }else {
    
                et_input.setText(cnt+"");}
        }
        //如果s1是空    s2不是空  就执行下一步
        else if(s1.equals("")&&!s2.equals("")){
    
            double d2=Double.parseDouble(s2);
            if(op.equals("+")){
    
                cnt=d2;
            }
            if(op.equals("-")){
    
                cnt=0-d2;
            }
            if(op.equals("×")){
    
                cnt=0;
            }
            if(op.equals("÷")){
    
                cnt=0;
            }
            if(!s2.contains(".")) {
    
                int res = (int) cnt;
                et_input.setText(res+"");
            }else {
    
                et_input.setText(cnt+"");}
        }
        else {
    
            et_input.setText("");
        }
    }
}

  1. 完成所有部署,启动项目即可
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_44857413/article/details/106668199

智能推荐

Android分享一张图片_android 分享一张图片到basequickadapter中-程序员宅基地

文章浏览阅读1k次。public class Act_Share extends Activity { private ShareCustomAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(_android 分享一张图片到basequickadapter中

Linux所有服务开放对应端口大全_linux服务器端口全部开放-程序员宅基地

文章浏览阅读4.7k次。Linux所有服务开放对应端口大全_linux服务器端口全部开放

php连接mysql实现简单注册登陆页面_php连接数据库登录和注册-程序员宅基地

文章浏览阅读1.6w次,点赞33次,收藏226次。PHP7连接数据库的方式:使用mysqli及PDOhttps://blog.csdn.net/zwliang98/article/details/82997349php输出执行sql语句的错误信息:mysqli_query($conn,$sql) or die(mysqli_error( $conn ));问题一:Incorrect integer value: ‘’ for colu..._php连接数据库登录和注册

深度学习之——防止过拟合的方法_加噪声防止过拟合-程序员宅基地

文章浏览阅读3.2k次,点赞8次,收藏13次。1、过拟合定义:在training data上的error渐渐减小,但是在验证集上的error却反而渐渐增大——因为训练出来的网络过拟合了训练集,对训练集外的数据却不work。模型越复杂,越容易过拟合。因此,原先以最小化损失(经验风险最小化)为目标:现在以最小化损失和模型复杂度(结构风险最小化)为目标:通过降低复杂模型的复杂度来防止过拟合的规则称为正则化。2、..._加噪声防止过拟合

Winform实现在DataGridView控件的单元格中添加多个控件_c# datagridview 添加多控件-程序员宅基地

文章浏览阅读3.6k次,点赞2次,收藏34次。Winform实现在DataGridView控件的单元格中添加多个控件背景实现思路关键代码背景DataGridView控件的列是支持TextBoxColumn、ComboBoxColumn等类型的,就是DataGridView的单元格进入编辑模式的时候就会出现对应的控件,但是有些业务场景是需要在一个单元格进入编辑状态时需要多个控件组合完成业务需求,就无法直接只用特定类型的Column来实现了。..._c# datagridview 添加多控件

python获取字符串中的子字符串,部分字符串,比如有一个字符串,python,如何就获取前3位,或者后2位_python 从某一位置读到末尾前几位-程序员宅基地

文章浏览阅读6.1k次,点赞4次,收藏14次。1.python,如何就获取前3位,或者后2位 text = 'python'>>> text[0:2] #使用冒号 : 分割符,获取位置0到位置2,但是不包括位置2的字符,即 p y 0位置,1位置'py'text[3:4] #获取位置3,4,但是不包括位置4,那么只是去位置3上的字符'h'>>> text[2:5] #获取2,3,4位..._python 从某一位置读到末尾前几位

随便推点

重装系统详细指南_电脑重装系统怎么操作csdn-程序员宅基地

文章浏览阅读7.4k次,点赞89次,收藏67次。电脑使用时间久了,无论是正常的系统升级,还是出了故障无法正常使用,都免不了要重装系统,本文详细地给大家介绍下如何重装系统,轻松完成。_电脑重装系统怎么操作csdn

出了国才能知道的十个小秘密_国国际海员-程序员宅基地

文章浏览阅读1.2k次。在国内时,了解“外面的世界”并不难,然而,认识的误区只有在国外住久了,慢慢地体会才能逐渐消除。下面十个方面如果不出国,我是不会知道很清楚的……   一、英语不努力学,是掌握不了的。 出国以前我以为到了国外,有了英语环境,自己会不费力地通过语言关。然而,除了小孩子,成年人欲消除语言障碍谈何容易?难怪许多在国外生活了半辈子的老侨,其英语程度还属初级。   二、从中国出口海外的食品,是给华人吃_国国际海员

git push error: 403_git push 403-程序员宅基地

文章浏览阅读5.0k次。我今天在把本地仓库push到远程仓库的时候,出现了问题:一会儿是这个:fatal: unable to access 'https://github.com/***/***/': OpenSSL SSL_read: Connection was reset, errno 10054一会儿又变成了这个:remote: Permission to Dxuan-chen/huashan.git denied to xuanfchen.fatal: unable to access 'https:_git push 403

excel shell合成_1分钟拆解:「如何将10多个工作表sheet,合并成一张?」-程序员宅基地

文章浏览阅读1.1k次。大家好,我是有讲课堂的认证达人:解题宝宝。今天到了VBA教学时间!因为今天阿,解题宝宝无聊闲逛,惊奇发现了两份VBA代码,特意分享给大家。是解决如何合并大量不同的工作表哒。多少张都没问题!亲测有效!分为以下两种情况☟01 合并同一工作簿的不同工作表。效果长这样:本来,同一工作簿下,一个排班表是一张sheet;接下里,就变成:所有排班表汇总成一张sheet,格式还自动排好!◎ 效果演示代码立即备上..._execl通过shell脚本的方式合并

程序员,30岁,还没有做管理层?你应该看看这篇文章_33岁一直做it,还没到管理层-程序员宅基地

文章浏览阅读1k次。如果你30岁,你是一名程序员,那么你就要好好把握接下来的黄金5年。_33岁一直做it,还没到管理层

AMOS分析技术:二阶验证性因子分析-程序员宅基地

文章浏览阅读4.8w次,点赞5次,收藏90次。基础准备草堂君在前面介绍了验证性因子分析的内容,包括验证性因子分析与探索性因子分析的区别联系,斜交验证性因子分析和正交验证性因子分析,可以点击下方文章链接回顾:AMOS分析技术:验证性因子分析介绍;信度与效度指标详解AMOS分析技术:斜交验证性因子分析;介绍如何整理出能够放入论文的模型信效度结果AMOS分析技术:正交验证性因子分析;模型拟合质量好,模型就一定好吗?今天草堂君要介绍的是二阶验证性因子_二阶验证性因子分析

推荐文章

热门文章

相关标签