Android智能计算器项目案例

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

Android智能计算器的开发

项目部分代码示例


提示:案例不完美,仅供参考

一、案例运行图片展示

在这里插入图片描述在这里插入图片描述
准备图片放在drawable中:在这里插入图片描述
在这里插入图片描述

二、布局文件

1.Activity_mian.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"
    android:background="@drawable/background"
    android:fitsSystemWindows="true"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    tools:context=".MainActivity"


    >
    <EditText
          android:id="@+id/txt_result"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:hint="0"
          android:gravity="right"
          android:textSize="40sp"
          android:textColor="#000000"
          android:padding="10dp"
          android:layout_margin="10dp"
          android:background="@drawable/editstylel"/>
    <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation=" horizontal"
          android:weightSum="4">
        <Button
          android:id="@+id/btn_7"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="7"
          android:textSize="60sp"
          android:textColor="#ffffff"
          android:textStyle="bold"
          android:background="@drawable/bth34"
          android:padding="5dp"
          android:layout_margin="10dp"
          android:layout_weight="1"
        />
        <Button
          android:id="@+id/btn_8"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="8"
          android:textSize="60sp"
          android:textColor="#ffffff"
          android:textStyle="bold"
          android:background="@drawable/bth34"
          android:padding="5dp"
          android:layout_margin="10dp"
          android:layout_weight="1"
        />
        <Button
          android:id="@+id/btn_9"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="9"
          android:textSize="60sp"
          android:textColor="#ffffff"
          android:textStyle="bold"
          android:background="@drawable/bth34"
          android:padding="5dp"
          android:layout_margin="10dp"
          android:layout_weight="1"
        />
        <Button
          android:id="@+id/btn_jia"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="+"
          android:textSize="60sp"
          android:textColor="#ffffff"
          android:textStyle="bold"
          android:background="@drawable/bth34"
          android:padding="5dp"
          android:layout_margin="10dp"
          android:layout_weight="1"
        />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation=" horizontal"
        android:weightSum="4">
        <Button
            android:id="@+id/btn_4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="4"
            android:textSize="60sp"
            android:textColor="#ffffff"
            android:textStyle="bold"
            android:background="@drawable/bth34"
            android:padding="5dp"
            android:layout_margin="10dp"
            android:layout_weight="1"
            />
        <Button
            android:id="@+id/btn_5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="5"
            android:textSize="60sp"
            android:textColor="#ffffff"
            android:textStyle="bold"
            android:background="@drawable/bth34"
            android:padding="5dp"
            android:layout_margin="10dp"
            android:layout_weight="1"
            />
        <Button
            android:id="@+id/btn_6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="6"
            android:textSize="60sp"
            android:textColor="#ffffff"
            android:textStyle="bold"
            android:background="@drawable/bth34"
            android:padding="5dp"
            android:layout_margin="10dp"
            android:layout_weight="1"
            />
        <Button
            android:id="@+id/btn_jian"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="-"
            android:textSize="60sp"
            android:textColor="#ffffff"
            android:textStyle="bold"
            android:background="@drawable/bth34"
            android:padding="5dp"
            android:layout_margin="10dp"
            android:layout_weight="1"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation=" horizontal"
        android:weightSum="4">
        <Button
            android:id="@+id/btn_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1"
            android:textSize="60sp"
            android:textColor="#ffffff"
            android:textStyle="bold"
            android:background="@drawable/bth34"
            android:padding="5dp"
            android:layout_margin="10dp"
            android:layout_weight="1"
            />
        <Button
            android:id="@+id/btn_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2"
            android:textSize="60sp"
            android:textColor="#ffffff"
            android:textStyle="bold"
            android:background="@drawable/bth34"
            android:padding="5dp"
            android:layout_margin="10dp"
            android:layout_weight="1"
            />
        <Button
            android:id="@+id/btn_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="3"
            android:textSize="60sp"
            android:textColor="#ffffff"
            android:textStyle="bold"
            android:background="@drawable/bth34"
            android:padding="5dp"
            android:layout_margin="10dp"
            android:layout_weight="1"
            />
        <Button
            android:id="@+id/btn_cheng"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="*"
            android:textSize="60sp"
            android:textColor="#ffffff"
            android:textStyle="bold"
            android:background="@drawable/bth34"
            android:padding="5dp"
            android:layout_margin="10dp"
            android:layout_weight="1"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation=" horizontal"
        android:weightSum="4">
        <Button
            android:id="@+id/btn_0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0"
            android:textSize="60sp"
            android:textColor="#ffffff"
            android:textStyle="bold"
            android:background="@drawable/bth34"
            android:padding="5dp"
            android:layout_margin="10dp"
            android:layout_weight="1"
            />
        <Button
            android:id="@+id/btn_qing"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="C"
            android:textSize="60sp"
            android:textColor="#ffffff"
            android:textStyle="bold"
            android:background="@drawable/bth34"
            android:padding="5dp"
            android:layout_margin="10dp"
            android:layout_weight="1"
            />
        <Button
            android:id="@+id/btn_deng"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="="
            android:textSize="60sp"
            android:textColor="#ffffff"
            android:textStyle="bold"
            android:background="@drawable/bth34"
            android:padding="5dp"
            android:layout_margin="10dp"
            android:layout_weight="1"
            />
        <Button
            android:id="@+id/btn_chu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="/"
            android:textSize="60sp"
            android:textColor="#ffffff"
            android:textStyle="bold"
            android:background="@drawable/bth34"
            android:padding="5dp"
            android:layout_margin="10dp"
            android:layout_weight="1"
            />
    </LinearLayout>
    <TextView
        android:id="@+id/tv_1"
        android:layout_width="360dp"
        android:layout_height="wrap_content"
        android:textColor="#00BFFF"
        android:padding="42dp"
        android:textSize="40sp"
        android:layout_gravity="center"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"


        />
</LinearLayout>

2.res中drawbale中btnstyle3.xml

代码如下(示例):

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
       <!--未单击时的样式-->
       <solid android:color="#D024FF"/>
       <stroke android:color="#000000" android:width = "5dp"/>
       <corners android:radius="1000dp"/>
</shape>

3.res中drawbale中btnstyle4.xml

代码如下(示例):

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color = "#FF24AF"/>
    <stroke android:color ="#28B474" android:width="5dp"/>
    <corners android:radius = "1000dp"/>
</shape>

4.res中drawbale中editstylel.xml

代码如下(示例):

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
   <solid android:color="#FFFFFF"/>
    <stroke android:color="#D6c9c9" android:width="3dp"/>
</shape>

5.res中drawbale中bth34.xml

代码如下(示例):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/btnstyle3" android:state_pressed ="false"/>
        <item android:drawable="@drawable/btnstyle4" android:state_pressed ="true"/>
</selector>

三、实现功能文件

1.在MainActivity.java中写入

代码如下(示例):

package com.example.interlligentcomputingmachine;

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

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private TextView txt_result;
    private Button btn_7;
    private Button btn_8;
    private Button btn_9;
    private Button btn_jia;
    private Button btn_4;
    private Button btn_5;
    private Button btn_6;
    private Button btn_jian;
    private Button btn_1;
    private Button btn_2;
    private Button btn_3;
    private Button btn_cheng;
    private Button btn_0;
    private Button btn_qing;
    private Button btn_deng;
    private Button btn_chu;
    private double num1 =0,num2 =0;   //声明参数
    private double result = 0;        //运算
    private Boolean isClickdeng =false;//是否单击
    private  String op ="%";
    private TextView tv1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    //绑定控件
        txt_result = findViewById(R.id.txt_result);
        btn_7 = findViewById(R.id.btn_7);
        btn_8 = findViewById(R.id.btn_8);
        btn_9 = (Button)findViewById(R.id.btn_9);
        btn_jia = (Button)findViewById(R.id.btn_jia);
        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_jian = (Button)findViewById(R.id.btn_jian);
        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_cheng = (Button)findViewById(R.id.btn_cheng);
        btn_0 = (Button)findViewById(R.id.btn_0);
        btn_qing = (Button)findViewById(R.id.btn_qing);
        btn_deng = (Button)findViewById(R.id.btn_deng);
        btn_chu = (Button)findViewById(R.id.btn_chu);
        //按钮的单击事件
        //数字0-9按钮代码
            btn_7.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //按钮单击逻辑
                    if (isClickdeng){
                        txt_result.setText("");
                        isClickdeng=false;
                    }
                    txt_result.setText(txt_result.getText().toString()+"7");
                }
            });
            btn_8.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //按钮单击逻辑
                    if(isClickdeng){
                        txt_result.setText("");
                        isClickdeng=false;
                    }
                    txt_result.setText(txt_result.getText().toString()+"8");
                }
            });
        btn_9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //按钮单击逻辑
                if(isClickdeng){
                    txt_result.setText("");
                    isClickdeng=false;
                }
                txt_result.setText(txt_result.getText().toString()+"9");
            }
        });
        btn_4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //按钮单击逻辑
                if(isClickdeng){
                    txt_result.setText("");
                    isClickdeng=false;
                }
                txt_result.setText(txt_result.getText().toString()+"4");
            }
        });
        btn_5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //按钮单击逻辑
                if(isClickdeng){
                    txt_result.setText("");
                    isClickdeng=false;
                }
                txt_result.setText(txt_result.getText().toString()+"5");
            }
        });
        btn_6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //按钮单击逻辑
                if(isClickdeng){
                    txt_result.setText("");
                    isClickdeng=false;
                }
                txt_result.setText(txt_result.getText().toString()+"6");
            }
        });
        btn_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //按钮单击逻辑
                if(isClickdeng){
                    txt_result.setText("");
                    isClickdeng=false;
                }
                txt_result.setText(txt_result.getText().toString()+"1");
            }
        });
        btn_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //按钮单击逻辑
                if(isClickdeng){
                    txt_result.setText("");
                    isClickdeng=false;
                }
                txt_result.setText(txt_result.getText().toString()+"2");
            }
        });
        btn_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //按钮单击逻辑
                if(isClickdeng){
                    txt_result.setText("");
                    isClickdeng=false;
                }
                txt_result.setText(txt_result.getText().toString()+"3");
            }
        });
        btn_0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //按钮单击逻辑
                if(isClickdeng){
                    txt_result.setText("");
                    isClickdeng=false;
                }
                txt_result.setText(txt_result.getText().toString()+"0");
            }
        });
        //运算符的按钮代码
        btn_jia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String st1 =txt_result.getText().toString();//获取加号之前的字符串类型的数据
                if(st1.equals("")){
                    return;
                }
                num1 = Double.parseDouble(st1);
                txt_result.setText("");
                op ="+";
                isClickdeng=false;

            }
        });
        btn_jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String st1 =txt_result.getText().toString();//获取减号之前的字符串类型的数据
                if(st1.equals("")){
                    return;
                }
                num1 = Double.parseDouble(st1);
                txt_result.setText("");
                op ="-";
                isClickdeng=false;

            }
        });
        btn_cheng.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String st1 =txt_result.getText().toString();//获取乘号之前的字符串类型的数据
                if(st1.equals("")){
                    return;
                }
                num1 = Double.parseDouble(st1);
                txt_result.setText("");
                op ="*";
                isClickdeng=false;

            }
        });
        btn_chu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String st1 =txt_result.getText().toString();//获取除号之前的字符串类型的数据
                if(st1.equals("")){
                    return;
                }
                num1 = Double.parseDouble(st1);
                txt_result.setText("");
                op ="/";
                isClickdeng=false;

            }
        });
        //清除按钮代码
        btn_qing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txt_result.setText("");//清除文本框的内容
            }
        });
        //等于按钮的代码
        btn_deng.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str2 = txt_result.getText().toString();//获取文本框的内容
                if(str2.equals("")){
                    return;
                }
         num2 = Double.parseDouble(str2);
                txt_result.setText("");
                switch (op){
                    case "+":result = num1 + num2;break;
                    case "-":result = num1 - num2;break;
                    case "*":result = num1 * num2;break;
                    case "/":result = num1 / num2;break;
                    case "%":result = num2;break;
                    default:result =0.0;break;
                }
                txt_result.setText(result+"");
                op = "%";
                isClickdeng = true;
            }
        });
        tv1 =findViewById(R.id.tv_1);
        tv1.setText("小case的计算器");
        tv1.setSelected(true);
    }
}


细节

更改app文件名找到values文件夹下strings.xml

代码如下(示例):

<resources>
    <string name="app_name">case</string>
</resources>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值