简易Android计算器(有bug)

14 篇文章 0 订阅

简易Android计算器(有bug)


页面概括:

1.整体采用线性布局
2.该计算器没有小数点计算
3.存在的BUG比较多
4.仅供参考
5.布局没有代码注释

布局代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >
    <EditText 
        android:id="@+id/ed"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#995545"        
        />
    <LinearLayout 
        android:id="@+id/ll_1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"        
        >
        <Button 
           android:id="@+id/bt_7"
           android:layout_width="0dp"
           android:layout_weight="1"
           android:layout_height="match_parent"
           android:text="7"
            />
        <Button 
           android:id="@+id/bt_8"
           android:layout_width="0dp"
           android:layout_weight="1"
           android:layout_height="match_parent"
           android:text="8"
            />
        <Button 
           android:id="@+id/bt_9"
           android:layout_width="0dp"
           android:layout_weight="1"
           android:layout_height="match_parent"
           android:text="9"
            />
        <Button 
           android:id="@+id/bt_chu"
           android:layout_width="0dp"
           android:layout_weight="1"
           android:layout_height="match_parent"
           android:text="÷"
            />
    </LinearLayout>
    <LinearLayout 
        android:id="@+id/ll_2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"        
        >
        <Button 
           android:id="@+id/bt_4"
           android:layout_width="0dp"
           android:layout_weight="1"
           android:layout_height="match_parent"
           android:text="4"
            />
        <Button 
           android:id="@+id/bt_5"
           android:layout_width="0dp"
           android:layout_weight="1"
           android:layout_height="match_parent"
           android:text="5"
            />
        <Button 
           android:id="@+id/bt_6"
           android:layout_width="0dp"
           android:layout_weight="1"
           android:layout_height="match_parent"
           android:text="6"
            />
        <Button 
           android:id="@+id/bt_cheng"
           android:layout_width="0dp"
           android:layout_weight="1"
           android:layout_height="match_parent"
           android:text="x"
            />
    </LinearLayout>
    <LinearLayout 
        android:id="@+id/ll_3"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"        
        >
        <Button 
           android:id="@+id/bt_1"
           android:layout_width="0dp"
           android:layout_weight="1"
           android:layout_height="match_parent"
           android:text="1"
            />
        <Button 
           android:id="@+id/bt_2"
           android:layout_width="0dp"
           android:layout_weight="1"
           android:layout_height="match_parent"
           android:text="2"
            />
        <Button 
           android:id="@+id/bt_3"
           android:layout_width="0dp"
           android:layout_weight="1"
           android:layout_height="match_parent"
           android:text="3"
            />
        <Button 
           android:id="@+id/bt_jian"
           android:layout_width="0dp"
           android:layout_weight="1"
           android:layout_height="match_parent"
           android:text="-"
            />
    </LinearLayout>
    <LinearLayout 
        android:id="@+id/ll_1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"        
        >
        <Button 
           android:id="@+id/bt_0"
           android:layout_width="0dp"
           android:layout_weight="1"
           android:layout_height="match_parent"
           android:text="0"
            />
        <Button 
           android:id="@+id/bt_qingchu"
           android:layout_width="0dp"
           android:layout_weight="1"
           android:layout_height="match_parent"
           android:text="C"
            />
        <Button 
           android:id="@+id/bt_deng"
           android:layout_width="0dp"
           android:layout_weight="1"
           android:layout_height="match_parent"
           android:text="="
            />
        <Button 
           android:id="@+id/bt_jia"
           android:layout_width="0dp"
           android:layout_weight="1"
           android:layout_height="match_parent"
           android:text="+"
            />
    </LinearLayout>

</LinearLayout>

布局效果图
这里写图片描述

java代码:
package com.example.demo_jisuanqi;

import android.R.integer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
    //常见 全局变量
    private Button bt0;
    private Button bt1;
    private Button bt2;
    private Button bt3;
    private Button bt4;
    private Button bt5;
    private Button bt6;
    private Button bt7;
    private Button bt8;
    private Button bt9;
    private Button jia;
    private Button jian;
    private Button cheng;
    private Button chu;
    private Button dengyu;
    private Button c;
    private EditText ed;
    //定义 符号 选择判断是否有符号 初识0
    int fuhao = 0;
    //定义输入的两个数 num1 num2
    String num1 = "";
    String num2 = "";
    //定义符号的位置 
    String wz = "";
    //定义点击符号
    boolean jjcc = true;
    //定义结果 
    String deshu = "";
    //定义 得数 装换为 整型
    int zxds;   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //1.获取布局文件
        setContentView(R.layout.activity_main);
        //2.获取控件,创建对象
        bt0 = findViewById(R.id.bt_0);
        bt1 = findViewById(R.id.bt_1);
        bt2 = findViewById(R.id.bt_2);
        bt3 = findViewById(R.id.bt_3);
        bt4 = findViewById(R.id.bt_4);
        bt5 = findViewById(R.id.bt_5);
        bt6 = findViewById(R.id.bt_6);
        bt7 = findViewById(R.id.bt_7);
        bt8 = findViewById(R.id.bt_8);
        bt9 = findViewById(R.id.bt_9);
        cheng = findViewById(R.id.bt_cheng);
        chu = findViewById(R.id.bt_chu);
        jia = findViewById(R.id.bt_jia);
        jian = findViewById(R.id.bt_jian);
        c = findViewById(R.id.bt_qingchu);
        ed = findViewById(R.id.ed);
        dengyu = findViewById(R.id.bt_deng);
        //3.指挥对象做事情
        bt0.setOnClickListener(new OnClickListener() {
            //注意:需要导包 import android.view.View.OnClickListener;
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //3.1利用 IF 判断是否有符号
                if(fuhao == 0){
                    num1 = num1 + "0";
                    ed.setText(num1);
                }
                //3.2利用 IF 判断是否有符号
                if(fuhao == 1){
                    num2=num2+"0";
                    ed.setText(num1 + wz + num2);
                }               
            }
        });
        //3.指挥对象做事情
        bt1.setOnClickListener(new OnClickListener() {
            //注意:需要导包 import android.view.View.OnClickListener;
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //3.1利用 IF 判断是否有符号
                if(fuhao == 0){
                    num1 = num1 + "1";
                    ed.setText(num1);
                }
                //3.2利用 IF 判断是否有符号
                if(fuhao == 1){
                    num2=num2+"1";
                    ed.setText(num1 + wz + num2);
                }               
            }
        });
        //3.指挥对象做事情
        bt2.setOnClickListener(new OnClickListener() {
            //注意:需要导包 import android.view.View.OnClickListener;
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //3.1利用 IF 判断是否有符号
                if(fuhao == 0){
                    num1 = num1 + "2";
                    ed.setText(num1);
                }
                //3.2利用 IF 判断是否有符号
                if(fuhao == 1){
                    num2=num2+"2";
                    ed.setText(num1 + wz + num2);
                }               
            }
        });
        //3.指挥对象做事情
        bt3.setOnClickListener(new OnClickListener() {
            //注意:需要导包 import android.view.View.OnClickListener;
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //3.1利用 IF 判断是否有符号
                if(fuhao == 0){
                    num1 = num1 + "3";
                    ed.setText(num1);
                }
                //3.2利用 IF 判断是否有符号
                if(fuhao == 1){
                    num2=num2+"3";
                    ed.setText(num1 + wz + num2);
                }               
            }
        });
        //3.指挥对象做事情
        bt4.setOnClickListener(new OnClickListener() {
            //注意:需要导包 import android.view.View.OnClickListener;
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //3.1利用 IF 判断是否有符号
                if(fuhao == 0){
                    num1 = num1 + "4";
                    ed.setText(num1);
                }
                //3.2利用 IF 判断是否有符号
                if(fuhao == 1){
                    num2=num2+"4";
                    ed.setText(num1 + wz + num2);
                }               
            }
        });
        //3.指挥对象做事情
        bt5.setOnClickListener(new OnClickListener() {
            //注意:需要导包 import android.view.View.OnClickListener;
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //3.1利用 IF 判断是否有符号
                if(fuhao == 0){
                    num1 = num1 + "5";
                    ed.setText(num1);
                }
                //3.2利用 IF 判断是否有符号
                if(fuhao == 1){
                    num2=num2+"5";
                    ed.setText(num1 + wz + num2);
                }               
            }
        });
        //3.指挥对象做事情
        bt6.setOnClickListener(new OnClickListener() {
            //注意:需要导包 import android.view.View.OnClickListener;
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //3.1利用 IF 判断是否有符号
                if(fuhao == 0){
                    num1 = num1 + "6";
                    ed.setText(num1);
                }
                //3.2利用 IF 判断是否有符号
                if(fuhao == 1){
                    num2=num2+"6";
                    ed.setText(num1 + wz + num2);
                }               
            }
        });
        //3.指挥对象做事情
        bt7.setOnClickListener(new OnClickListener() {
            //注意:需要导包 import android.view.View.OnClickListener;
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //3.1利用 IF 判断是否有符号
                if(fuhao == 0){
                    num1 = num1 + "7";
                    ed.setText(num1);
                }
                //3.2利用 IF 判断是否有符号
                if(fuhao == 1){
                    num2=num2+"7";
                    ed.setText(num1 + wz + num2);
                }               
            }
        });
        //3.指挥对象做事情
        bt8.setOnClickListener(new OnClickListener() {
            //注意:需要导包 import android.view.View.OnClickListener;
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //3.1利用 IF 判断是否有符号
                if(fuhao == 0){
                    num1 = num1 + "8";
                    ed.setText(num1);
                }
                //3.2利用 IF 判断是否有符号
                if(fuhao == 1){
                    num2=num2+"8";
                    ed.setText(num1 + wz + num2);
                }               
            }
        });
        //3.指挥对象做事情
        bt9.setOnClickListener(new OnClickListener() {
            //注意:需要导包 import android.view.View.OnClickListener;
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //3.1利用 IF 判断是否有符号
                if(fuhao == 0){
                    num1 = num1 + "9";
                    ed.setText(num1);
                }
                //3.2利用 IF 判断是否有符号
                if(fuhao == 1){
                    num2=num2+"9";
                    ed.setText(num1 + wz + num2);
                }               
            }
        });
        //4  符号
        jia.setOnClickListener(new OnClickListener() {          
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //4.1 判断是否点击了符号 jjcc
                if (jjcc) {
                    //4.1.1 位置 加上符号
                    wz = "+";
                    //4.1.2 符号 ++
                    fuhao = 1;
                    //4.1.3 符号为假
                    jjcc = false;
                }
            }
        });
      //4  符号
        jian.setOnClickListener(new OnClickListener() {         
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //4.1 判断是否点击了符号 jjcc
                if (jjcc) {
                    //4.1.1 位置 加上符号
                    wz = "-";
                    //4.1.2 符号 ++
                    fuhao = 1;
                    //4.1.3 符号为假
                    jjcc = false;
                }
            }
        });
      //4  符号
        cheng.setOnClickListener(new OnClickListener() {            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //4.1 判断是否点击了符号 jjcc
                if (jjcc) {
                    //4.1.1 位置 加上符号
                    wz = "x";
                    //4.1.2 符号 ++
                    fuhao = 1;
                    //4.1.3 符号为假
                    jjcc = false;
                }
            }
        });
      //4  符号
        chu.setOnClickListener(new OnClickListener() {          
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //4.1 判断是否点击了符号 jjcc
                if (jjcc) {
                    //4.1.1 位置 加上符号
                    wz = "÷";
                    //4.1.2 符号 ++
                    fuhao = 1;
                    //4.1.3 符号为假
                    jjcc = false;
                }
            }
        });
        //5 等于
        dengyu.setOnClickListener(new OnClickListener() {           
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //5.1 修改 符号为真
                jjcc = true;
                //5.2 利用IF 判断选择的是哪个符号
                if (wz.equals("+")) {
                    //5.2.1 定义两个整型的 变量 来运算
                    //Integer.parseInt(String)就是将String字符类型数据转换为Integer整型数据
                     int x = Integer.parseInt(num1);
                     int y = Integer.parseInt(num2);
                    //5.2.2 运算
                     zxds = x + y;
                    //5.2.3 转换字符串
                     deshu = String.valueOf(zxds);
                    //5.2.3 输出到编辑框里面
                     ed.setText(num1 + wz + num2 + "=" + deshu);
                }
                if (wz.equals("-")) {
                    //5.2.1 定义两个整型的 变量 来运算
                    //Integer.parseInt(String)就是将String字符类型数据转换为Integer整型数据
                     int x = Integer.parseInt(num1);
                     int y = Integer.parseInt(num2);
                    //5.2.2 运算
                     zxds = x - y;
                    //5.2.3 转换字符串
                     deshu = String.valueOf(zxds);
                    //5.2.3 输出到编辑框里面
                     ed.setText(num1 + wz + num2 + "=" + deshu);
                }
                if (wz.equals("x")) {
                    //5.2.1 定义两个整型的 变量 来运算
                    //Integer.parseInt(String)就是将String字符类型数据转换为Integer整型数据
                     int x = Integer.parseInt(num1);
                     int y = Integer.parseInt(num2);
                    //5.2.2 运算
                     zxds = x * y;
                    //5.2.3 转换字符串
                     deshu = String.valueOf(zxds);
                    //5.2.3 输出到编辑框里面
                     ed.setText(num1 + wz + num2 + "=" + deshu);
                }
                if (wz.equals("÷")) {
                    //5.2.1 定义两个整型的 变量 来运算
                    //Integer.parseInt(String)就是将String字符类型数据转换为Integer整型数据
                     int x = Integer.parseInt(num1);
                     int y = Integer.parseInt(num2);
                    //5.2.2 运算
                     zxds = x / y;
                    //5.2.3 转换字符串
                     //判断 y是否为0
                     if(y != 0){
                     deshu = String.valueOf(zxds);
                    //5.2.3 输出到编辑框里面
                     ed.setText(num1 + wz + num2 + "=" + deshu);
                     }else{
                         ed.setText("∞");
                         Toast.makeText(MainActivity.this, "除数不能为0", 0).show();
                     }
                }
            }
        });
        //6 重置复位
        c.setOnClickListener(new OnClickListener() {            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //定义 符号 选择判断是否有符号 初识0
                 fuhao = 0;
                //定义输入的两个数 num1 num2
                 num1 = "";
                 num2 = "";
                //定义符号的位置 
                 wz = "";
                //定义点击符号
                jjcc = true;
                //定义结果 
                String deshu = "";
                //定义 得数 装换为 整型
                 zxds = 0;
                //输入框 为0
                ed.setText("0");
            }
        });
    }   
}

效果图这里写图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值