简易计算器--初学者实用

android studio 简易计算器

安卓中常用布局:线性表布局(LineraLayout),表格布局(TableLayout),相对布局(RelativeLayout)

界面编程

布局:相对布局(RelativeLayout)
布局文件名(relative_calculator)

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 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">

<TextView
    android:id="@+id/zll"
    android:layout_width="match_parent"
    android:layout_height="93dp"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:background="@drawable/resultshape"
    android:gravity="left"
    android:textAllCaps="false"
    android:textColor="@android:color/background_dark"
    android:textSize="50sp" />

<Button
    android:id="@+id/number7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/zll"
    android:layout_below="@+id/zll"
    android:layout_marginTop="45dp"
    android:text="7"
    android:textColor="@android:color/holo_blue_dark"
    android:textSize="30sp"
    android:background="@drawable/bouttonshape"/>

<Button
    android:id="@+id/number8"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/number7"
    android:layout_below="@+id/zll"
    android:layout_marginLeft="20dp"
    android:layout_toRightOf="@+id/number7"
    android:text="8"
    android:textColor="@android:color/holo_blue_dark"
    android:textSize="30sp"
    android:background="@drawable/bouttonshape"/>

<Button
    android:id="@+id/number9"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/number8"
    android:layout_below="@+id/zll"
    android:layout_marginLeft="20dp"
    android:layout_toRightOf="@+id/number8"
    android:text="9"
    android:textColor="@android:color/holo_blue_dark"
    android:textSize="30sp"
    android:background="@drawable/bouttonshape"/>

<Button
    android:id="@+id/jiafa"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/number9"
    android:layout_marginLeft="20dp"
    android:layout_toRightOf="@+id/number9"
    android:text="+"
    android:textSize="30sp" />

<Button
    android:id="@+id/number4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/number7"
    android:layout_below="@+id/number7"
    android:layout_marginTop="20dp"
    android:text="4"
    android:textColor="@android:color/holo_blue_dark"
    android:textSize="30sp"
    android:background="@drawable/bouttonshape"/>

<Button
    android:id="@+id/number5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/number4"
    android:layout_below="@+id/number8"
    android:layout_marginLeft="20dp"
    android:layout_toRightOf="@+id/number4"
    android:text="5"
    android:textColor="@android:color/holo_blue_dark"
    android:textSize="30sp"
    android:background="@drawable/bouttonshape"/>

<Button
    android:id="@+id/number6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/number5"
    android:layout_below="@+id/number9"
    android:layout_marginLeft="20dp"
    android:layout_toRightOf="@+id/number5"
    android:text="6"
    android:textColor="@android:color/holo_blue_dark"
    android:textSize="30sp"
    android:background="@drawable/bouttonshape"/>

<Button
    android:id="@+id/jianfa"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/number6"
    android:layout_below="@+id/jiafa"
    android:layout_marginLeft="20dp"
    android:layout_toRightOf="@+id/number6"
    android:text="-"
    android:textSize="30sp"/>

<Button
    android:id="@+id/number1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/number4"
    android:layout_below="@+id/number4"
    android:layout_marginTop="20dp"
    android:text="1"
    android:textColor="@android:color/holo_blue_dark"
    android:textSize="30sp"
    android:background="@drawable/bouttonshape"/>

<Button
    android:id="@+id/number2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/number1"
    android:layout_marginLeft="20dp"
    android:layout_toRightOf="@+id/number1"
    android:text="2"
    android:textColor="@android:color/holo_blue_dark"
    android:textSize="30sp"
    android:background="@drawable/bouttonshape"/>

<Button
    android:id="@+id/number3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/number1"
    android:layout_below="@+id/number6"
    android:layout_marginLeft="20dp"
    android:layout_toRightOf="@+id/number2"
    android:text="3"
    android:textColor="@android:color/holo_blue_dark"
    android:textSize="30sp"
    android:background="@drawable/bouttonshape"/>

<Button
    android:id="@+id/chengfa"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/number3"
    android:layout_marginLeft="20dp"
    android:layout_toRightOf="@+id/number3"
    android:text="*"
    android:textSize="30sp" />

<Button
    android:id="@+id/number0"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/number1"
    android:layout_marginTop="20dp"
    android:text="0"
    android:textColor="@android:color/holo_blue_dark"
    android:textSize="30sp"
    android:background="@drawable/bouttonshape"/>

<Button
    android:id="@+id/dian"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/number2"
    android:layout_alignTop="@+id/number0"
    android:layout_below="@+id/number2"
    android:layout_toRightOf="@+id/number0"
    android:text="."
    android:textSize="30sp" />

<Button
    android:id="@+id/dengyu"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/number3"
    android:layout_alignTop="@+id/dian"
    android:layout_toRightOf="@+id/dian"
    android:text="="
    android:textSize="30sp" />

<Button
    android:id="@+id/chuyu"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/chengfa"
    android:layout_alignTop="@+id/dengyu"
    android:text="/"
    android:textSize="30sp" />

<Button
    android:id="@+id/quxiao"
    android:layout_width="245dp"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="33dp"
    android:background="@android:drawable/dialog_holo_light_frame"
    android:text="cancel"
    android:textSize="55sp" />

逻辑编程

由于只定义了两个有关数字变量,所以只能进行两个数的加减乘除,适合简单计算。

//导入包
package com.cdp.myrelative;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

//
public class Relativectivity extends AppCompatActivity {
Button[] numbt = new Button[11];//数字数组
Button[] oprbt = new Button[5];//操作符数组
Button cancle = null;//删除
TextView result_et = null;//获取文本框
Boolean firstflag = true;//标志
Boolean cleaarflag = true;//清除标志
double result;//运算结果
String oprtcomm = “”;//运算符
String a="";//字符串a变量为空
String b="";//字符串b变量为空

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.relative_calculator);//获取布局
    numbt[0] = (Button) findViewById(R.id.number0);//找到数字按钮
    numbt[1] = findViewById(R.id.number1);
    numbt[2] = findViewById(R.id.number2);
    numbt[3] = findViewById(R.id.number3);
    numbt[4] = findViewById(R.id.number4);
    numbt[5] = findViewById(R.id.number5);
    numbt[6] = findViewById(R.id.number6);
    numbt[7] = findViewById(R.id.number7);
    numbt[8] = findViewById(R.id.number8);
    numbt[9] = findViewById(R.id.number9);
    numbt[10] = findViewById(R.id.dian);
    oprbt[0] = findViewById(R.id.jiafa);
    oprbt[1] = findViewById(R.id.jianfa);
    oprbt[2] = findViewById(R.id.chengfa);
    oprbt[3] = findViewById(R.id.chuyu);
    oprbt[4] = findViewById(R.id.dengyu);
    result_et = findViewById(R.id.zll);
    cancle = findViewById(R.id.quxiao);
    NumberListener numberListener = new NumberListener();
    for (Button tempbt : numbt) {//初始化数字按键
        tempbt.setOnClickListener(numberListener);

    }
    OpratorListener opratorListener = new OpratorListener();
    for (Button temp : oprbt) {//初始化操作符按键

        temp.setOnClickListener(opratorListener);
    }

    cancle.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {//清除键监听器
            result_et.setText("");//使得文本框清除为空
            firstflag =true;//让第一次标志位为真,使得重新计算
        }
    });
}

//数字监听器
class NumberListener implements View.OnClickListener {//implements表示继承(实现)一个接口

    @Override//数字监听器
    public void onClick(View z) {
        Button inputbt = (Button) z;//首先获取按钮
        String inputtext = inputbt.getText().toString();//获取按钮内容
        if (firstflag) {
            if (inputtext.equals(".")) {//判断点击事件为点时,不响应(第一次点击)
                return;
            } else {//如果第一次点击不为点

                a = inputtext;//将按键获取内容置给变量A
                result_et.setText(a);//文本框显示变量A获取的数字
                firstflag = false;//将第一次标志位置为假
            }

            } else{
                if (oprtcomm.equals("")) {//如果操作符字符串为空
                    if (a.indexOf(".") > 0 && inputtext.equals(".")) {//当a字符串中包含点且在次点击为点时,不响应。
                        return;
                    }

                    if (a.equals("0") && !(inputtext.equals("."))) {//当字符串A等于0且没有点击点,则不响应
                        return;
                    }
                   //if (a.equals("-") && inputtext.equals(".")) {
                      //  return;
                   // }
                    a = a + inputtext;//字符串a第一获取内容+在次获取的数字
                    result_et.setText(a);//文本框获取到的内容
                }


                if (!a.equals("") && !oprtcomm.equals("")) {//如果数字字符串和操作符不为空时
                    if (b.indexOf(".") > 0 && inputtext.equals(".")) {
                        return;
                    }

                    if (b.equals("0") && !(inputtext.equals("."))) {
                        return;
                    }
                    //if (b.equals("-") && inputtext.equals(".")) {
                       // return;
                    //}
                    b = b + inputtext;
                    result_et.setText(a + oprtcomm + b);//数字字符串a获取内容加上操作符字符串加上数字字符串b
                }


            }
        }
    }

//操作符监听器
class OpratorListener implements View.OnClickListener {//操作符监听器

            @Override
            public void onClick(View z) {
                Button ortbt = (Button) z;//首先获取按钮
                String optstr = ortbt.getText().toString();//获取操作符按钮
                String optnum = result_et.getText().toString();//获取文本框内容
                if (firstflag) {
                    if (ortbt.equals("-")) {//只有按了“-”操作符按钮才响应,其他不响应
                        a = a + optstr;//数字字符串a加上操作符字符串
                        result_et.setText(a);
                        firstflag = false;
                    }
                } else {
                    if (b.equals("")) {
                        oprtcomm = optstr;
                        result_et.setText(a + oprtcomm);
                    }
                    if (optstr.equals("=")) {//当按键为等号时
                        result = calc(Double.parseDouble(a), Double.parseDouble(b));//运算结果中将字符串a和b强制转换成double类型
                        result_et.setText(a + oprtcomm +b+ "=" +result);//得出完整表达式
                        firstflag = true;
                        a = "";//重新将其置空
                        b = "";
                        oprtcomm = "";
                        return;
                    }


                }


                //  if (oprtcomm.equals("-") || oprtcomm.equals("+") || oprtcomm.equals("/") || oprtcomm.equals("*")) {
                // result_et.setText("");
                // }
            }


            private double calc(double a, double b) {
                if (oprtcomm.equals("+")) {
                    result = a + b;
                }
                if (oprtcomm.equals("-")) {
                    result = a - b;
                }
                if (oprtcomm.equals("*")) {
                    result *= a * b;
                }
                if (oprtcomm.equals("/")) {
                    result = a / b;
                }
                result_et.setText(result + "");
                return result;
            }
        }
    }

下面是运行界面图
在这里插入图片描述

我是才开始学android studio ,可能写的不好,有什么更好的建议,感谢大家多多提点意见。在注释可能存在名词解释错误。下面是关于Android studio的百度百科

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值