使用android实现简易的计算器的功能
1、给计算机布局:activity_main_xml:

<?xml version="1.0" encoding="utf-8"?>
<GridLayout 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"tools:context=".MainActivity"android:orientation="vertical"><GridLayout
        android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="top"><!--输入的文本框--><EditText
            android:id="@+id/main_et_result"android:layout_width="match_parent"android:layout_height="100dp"android:hint="请输入数字"/></GridLayout><GridLayout
        android:layout_width="match_parent"android:layout_height="wrap_content"android:rowCount="5"android:columnCount="4"android:layout_gravity="center|top"><Button
            android:id="@+id/main_btn1"android:layout_width="90dp"android:layout_height="60dp"android:text="1"/><Button
            android:id="@+id/main_btn2"android:layout_width="90dp"android:layout_height="60dp"android:text="2"/><Button
            android:id="@+id/main_btn3"android:layout_width="90dp"android:layout_height="60dp"android:text="3"/><Button
            android:id="@+id/main_btnc"android:layout_width="90dp"android:layout_height="60dp"android:text="/"/><Button
            android:id="@+id/main_btn4"android:layout_width="90dp"android:layout_height="60dp"android:text="4"/><Button
            android:id="@+id/main_btn5"android:layout_width="90dp"android:layout_height="60dp"android:text="5"/><Button
            android:id="@+id/main_btn6"android:layout_width="90dp"android:layout_height="60dp"android:text="6"/><Button
            android:id="@+id/main_btnx"android:layout_width="90dp"android:layout_height="60dp"android:text="*"/><Button
            android:id="@+id/main_btn7"android:layout_width="90dp"android:layout_height="60dp"android:text="7"/><Button
            android:id="@+id/main_btn8"android:layout_width="90dp"android:layout_height="60dp"android:text="8"/><Button
            android:id="@+id/main_btn9"android:layout_width="90dp"android:layout_height="60dp"android:text="9"/><Button
            android:id="@+id/main_btndel"android:layout_width="90dp"android:layout_height="60dp"android:text="del"/><Button
            android:id="@+id/main_btnj"android:layout_width="90dp"android:layout_height="63dp"android:text="-" /><Button
            android:id="@+id/main_btn0"android:layout_width="90dp"android:layout_height="60dp"android:layout_columnSpan="1"android:layout_gravity="fill_horizontal"android:text="0" /><Button
            android:id="@+id/main_btnd"android:layout_width="90dp"android:layout_height="60dp"android:text="."/><Button
            android:id="@+id/main_btn1a"android:layout_width="90dp"android:layout_height="71dp"android:layout_rowSpan="2"android:layout_gravity="fill_vertical"android:text="+" /><Button
            android:id="@+id/main_btn1d"android:layout_width="180dp"android:layout_height="60dp"android:layout_columnSpan="3"android:layout_gravity="fill_horizontal"android:text="="/><Space /></GridLayout></GridLayout>

说明:在这里我给编辑框(EditText) 设了一个id,用来通过id处理结果集;给每个按钮(Button)也设了一个id ,目的是通过id去获取它自己的值。

*合并行:layout_columnSpan*, 合并列 :layout_rowSpan

效果图:

说明:我这里运用的是一个大的GridLayout布局,里面嵌套两个小的GridLayout布局,分别存放输入框【编辑框(EditText)】和按钮(Button),
EditText和TextView非常相似,它与TextView共用了绝大总分XML属性和文法,
二者最大区别在于:EditText可以接受用户输入。

2、实现的方法:MainActivtiy.java:

package com.example.t212_a05;import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;public class MainActivity extends AppCompatActivity implements View.OnClickListener {//    结果集private EditText editText;//数字1-9private Button main_btn1;private Button main_btn2;private Button main_btn3;private Button main_btn4;private Button main_btn5;private Button main_btn6;private Button main_btn7;private Button main_btn8;private Button main_btn9;private  Button main_btn0;//运算符private  Button main_btn1a  ;// +private  Button main_btnj;  // -private  Button main_btnx;  // *private  Button main_btnc;  // /private  Button main_btnd;  //小数点private  Button main_btn1d;  //=//清除private  Button main_btndel;boolean clear_flag;//清空标识@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//数字1-9View main_btn1 = findViewById(R.id.main_btn1);View main_btn2 = findViewById(R.id.main_btn2);View main_btn3= findViewById(R.id.main_btn3);View main_btn4 = findViewById(R.id.main_btn4);View main_btn5 = findViewById(R.id.main_btn5);View main_btn6 = findViewById(R.id.main_btn6);View main_btn7 = findViewById(R.id.main_btn7);View main_btn8 = findViewById(R.id.main_btn8);View main_btn9 = findViewById(R.id.main_btn9);View main_btn0 = findViewById(R.id.main_btn0);//运算符View main_btn1a = findViewById(R.id.main_btn1a);// +View main_btnj = findViewById(R.id.main_btnj);// -View main_btnx = findViewById(R.id.main_btnx);// *View main_btnc = findViewById(R.id.main_btnc); // /View main_btnd = findViewById(R.id.main_btnd);//小数点View main_btn1d = findViewById(R.id.main_btn1d);//=View main_btndel = findViewById(R.id.main_btndel);//清空editText = (EditText) findViewById(R.id.main_et_result);//结果集//添加点击事件main_btn0.setOnClickListener(this);main_btn1.setOnClickListener(this);main_btn2.setOnClickListener(this);main_btn3.setOnClickListener(this);main_btn4.setOnClickListener(this);main_btn5.setOnClickListener(this);main_btn6.setOnClickListener(this);main_btn7.setOnClickListener(this);main_btn8.setOnClickListener(this);main_btn9.setOnClickListener(this);main_btnd.setOnClickListener(this);main_btndel.setOnClickListener(this);main_btn1a.setOnClickListener(this);main_btnj.setOnClickListener(this);main_btnx.setOnClickListener(this);main_btnc.setOnClickListener(this);main_btn1d.setOnClickListener(this);}//读取每个按钮的点击的内容@Overridepublic void onClick(View view) {//获取文本内容String input = editText.getText().toString();switch (view.getId()){case R.id.main_btn0:case R.id.main_btn1:case R.id.main_btn2:case R.id.main_btn3:case R.id.main_btn4:case R.id.main_btn5:case R.id.main_btn6:case R.id.main_btn7:case R.id.main_btn8:case R.id.main_btn9:case R.id.main_btnd:if(clear_flag){clear_flag = false;editText.setText("");//赋值为空}editText.setText(input + ((Button)view).getText());//结果集就为本身break;case R.id.main_btn1a:case R.id.main_btnj:case R.id.main_btnx:case R.id.main_btnc:if(clear_flag){clear_flag = false;input = "";editText.setText("");}editText.setText(input + " " + ((Button)view).getText() + " ");break;case R.id.main_btndel:if(clear_flag){clear_flag = false;input = "";editText.setText("");}else if(input != null || !input.equals("")) {//如果获取到的内容为空editText.setText(input.substring(0, input.length() - 1));//结果集为空}break;case R.id.main_btn1d://运算结果  =getResult();//调用处理结果集的方法break;}}//运算结果的方法private void getResult(){String exp = editText.getText().toString();//获取文本框的内容if(exp==null||exp.equals("")){return;}if(!exp.contains(" ")){return;}if(clear_flag){clear_flag = false;return;}clear_flag = true;double result = 0;//进行截取//运算符前的数字String s1 = exp.substring(0,exp.indexOf(" "));//运算符String op = exp.substring(exp.indexOf(" ")+1,exp.indexOf(" ")+2);//运算符后的数字String s2 = exp.substring(exp.indexOf(" ")+3);if(!s1.equals("")&&!s2.equals("")) {//如果包含小数点的运算double d1 = Double.parseDouble(s1);//则数字都是double类型double d2 = Double.parseDouble(s2);if (op.equals("+")) {//如果是 +result = d1 + d2;} else if (op.equals("-")) {result = d1 - d2;} else if (op.equals("*")) {result = d1 * d2;} else if (op.equals("/")) {if (d2 == 0) { //如果被除数是0result = 0; //则结果是0}else {//否则执行正常是除法运算result = d1 / d2;}}if (!s1.contains(".") && !s2.contains(".") && !op.equals("/")) {//如果是整数类型int r = (int) result; //都是整形editText.setText(r + "");} else{editText.setText(result + "");}}else if(!s1.equals("") && s2.equals("")){//如果是只输入运算符前的数editText.setText(exp);//直接返回当前文本框的内容}else if(s1.equals("") && !s2.equals("")){//如果是只输入运算符后面的数double d2 = Double.parseDouble(s2);//运算符前没有输入数字if (op.equals("+")) {result = 0 + d2;} else if (op.equals("-")) {result = 0 - d2;} else if (op.equals("*")) {result = 0;} else if (op.equals("/")) {result = 0;}if (!s1.contains(".") && !s2.contains(".")) {int r = (int) result;editText.setText(r + "");} else{editText.setText(result + "");}}else {editText.setText("");}}
}

说明: 在写方法的时候,让MainActivtiy实现View.OnClickListener 这个接口,在实现点击事件的时候,就可以直接用 this ,例如: main_btn0.setOnClickListener(this);

运行结果:

样式比较丑,凑合着吧,功能实现出来就好!
Android 实现计算器功能,只是简单的实现。

使用Android 实现计算器功能相关推荐

  1. android实现计算器功能吗,利用Android实现一个简单的计算器功能

    利用Android实现一个简单的计算器功能 发布时间:2020-11-20 16:25:01 来源:亿速云 阅读:90 作者:Leah 今天就跟大家聊聊有关利用Android实现一个简单的计算器功能, ...

  2. android实现计算器功能吗,简单实现Android计算器功能

    自己写的安卓的计算器: 注:这个是在mac中开发的,如果要在windows的eclipse中运行可能会出现路径问题,解决办法从windows中已有的安卓工程根目录下复制一下classpath文件,然后 ...

  3. android实现计算器功能吗,安卓实现一个计算器的功能

    新建项目工程 第一步 布局的设置 android:layout_width="368dp" android:layout_height="495dp" andr ...

  4. Android实现计算器功能

    .xml文件: <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:andro ...

  5. android计算器开发论文,基于Android计算器功能的实现毕业设计论文

    <基于Android计算器功能的实现毕业设计论文.doc>由会员分享,可免费在线阅读全文,更多与<基于Android计算器功能的实现毕业设计论文>相关文档资源请在帮帮文库(ww ...

  6. Android实现简单的计算器功能

    Android实现简单的计算器功能 **前言:**通过Android实现简单的计算器功能,实现简单的加.减.乘.除操作. 效果图如下: 第一步 布局文件:activity_main.xml <? ...

  7. android计算器功能实现,在android中利用 studio实现一个简单的计算器功能

    在android中利用 studio实现一个简单的计算器功能 发布时间:2020-11-07 15:35:20 来源:亿速云 阅读:168 作者:Leah 这篇文章将为大家详细讲解有关在android ...

  8. Java仿小米计算器源码_xiaomi-Calculator 仿照小米的计算器功能,作为一个Android的初学者可以参考,界面简单大方。 259万源代码下载- www.pudn.com...

    文件名称: xiaomi-Calculator下载  收藏√  [ 5  4  3  2  1 ] 开发工具: Java 文件大小: 795 KB 上传时间: 2016-06-13 下载次数: 0 提 ...

  9. android角度计算器,Calckit高级版一款安卓多功能计算器,内置高度定制的科学计算器...

    前言 手机中的计算器功能你多久用一次呢,我相信对于多数人来说计算器是一个低频使用软件. 其原因可能是用不上计算功能也很可能是单一的计算功能并不能满足你对于计算的需求. 那么如果现在有一款可以完成中学到 ...

  10. Android 房租计算器,简单计算器

    房租计算器 功能简介 1.根据入住时间,计算入住至今的月份间隔,x月x天: 2.自定义计算器,无括号,简单计算器: 3.使用SqLite存储计算结果,使用RecyclerView显示数据,实现刷新和加 ...

最新文章

  1. 【java】兴唐第二十四节课
  2. WPS菜单栏自动隐藏的解决方法
  3. 杭州电子科技大学保研计算机,杭州电子科技大学计算机学院软件工程(专业学位)保研条件...
  4. jsoup 获取html中body内容_Java 进阶 利用Jsoup获取HTML页面的各分页中的标题信息...
  5. 【Matlab】离散点拟合曲面
  6. php debug pit,start.php
  7. linux cmake 快速安装
  8. [Vmware卸载] Vmware12卸载
  9. 实例讲解反向传播(简单易懂)
  10. web绿色服务器单文件,Web个人临时共享服务器
  11. 使用 vimdiff 比较文件的技巧
  12. 【mud】set (long格式造成的进入mud之后“你的四周灰蒙蒙地一片,什么也没有。”
  13. 系统动力学视角的智慧城市模型研究
  14. vue实现音乐平台项目
  15. 什么是前端模块化?为什么要进行模块化开发?前端技术文章分享
  16. 莫拉克电梯服务器说明书_默纳克操作手册
  17. 等价无穷小替换及其习题 笔记
  18. Visual Studio 2019 和 qt 5.15.1 下 opengl 的运用 - Lighting - 06 - MultipleLights
  19. 双系统重装Ubuntu
  20. 专升本管理学知识点总结——人力资源管理

热门文章

  1. 高级商务办公软件应用【10】
  2. 独家CleanMyMac使用教程
  3. fiddler抓包手机app数据(手机连接WiFi)
  4. VB代码-excel自动排序
  5. c语言中strncpy的用法,C语言中函数strcpy ,strncpy ,strlcpy的用法【转】
  6. 多多农场游戏源码果园种植+养殖游戏 对接广告联盟APP+小程序
  7. 基于深度学习的单目人体姿态估计方法综述(一)
  8. UDP Socket编程
  9. WINCCV7.5入门指南学习简介
  10. 无线电射频能量的收集