最近接触了android开发就试着写了一个计算器的小程序:

在xml文件中的布局代码如下:

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3     xmlns:tools="http://schemas.android.com/tools"
  4     android:id="@+id/activity_ji_suan02"
  5     android:layout_width="match_parent"
  6     android:layout_height="match_parent"
  7     android:orientation="vertical"
  8     tools:context="com.example.whs.sample01_1_activity.JiSuanActivity02">
  9
 10     <TextView
 11         android:id="@+id/tv"
 12         android:layout_width="match_parent"
 13         android:layout_height="40dp"
 14         android:textSize="30dp"
 15         android:text="0"
 16         android:textColor="#ff0000"
 17         android:gravity="center_vertical|right"
 18         android:layout_marginRight="5dp"
 19         android:layout_marginLeft="5dp"
 20         android:background="#FFFF00"
 21         />
 22     <!--7 8 9 + -->
 23     <LinearLayout
 24         android:layout_width="match_parent"
 25         android:layout_height="wrap_content"
 26         android:paddingTop="5dp"
 27         android:orientation="horizontal"
 28         >
 29
 30         <Button
 31             android:id="@+id/Button07"
 32             android:text="7"
 33             android:textSize="25dp"
 34             android:layout_width="80dp"
 35             android:layout_height="wrap_content" />
 36         <Button
 37             android:id="@+id/Button08"
 38             android:text="8"
 39             android:textSize="25dp"
 40             android:layout_width="80dp"
 41             android:layout_height="wrap_content" />
 42         <Button
 43             android:id="@+id/Button09"
 44             android:text="9"
 45             android:textSize="25dp"
 46             android:layout_width="80dp"
 47             android:layout_height="wrap_content" />
 48         <Button
 49             android:id="@+id/ButtonJia"
 50             android:text="+"
 51             android:textSize="25dp"
 52             android:layout_width="80dp"
 53             android:layout_height="wrap_content" />
 54
 55     </LinearLayout>
 56
 57     <!--4 5 6 - -->
 58     <LinearLayout
 59         android:layout_width="match_parent"
 60         android:layout_height="wrap_content"
 61         android:paddingTop="5dp"
 62         android:orientation="horizontal"
 63         >
 64
 65         <Button
 66             android:id="@+id/Button04"
 67             android:text="4"
 68             android:textSize="25dp"
 69             android:layout_width="80dp"
 70             android:layout_height="wrap_content" />
 71         <Button
 72             android:id="@+id/Button05"
 73             android:text="5"
 74             android:textSize="25dp"
 75             android:layout_width="80dp"
 76             android:layout_height="wrap_content" />
 77         <Button
 78             android:id="@+id/Button06"
 79             android:text="6"
 80             android:textSize="25dp"
 81             android:layout_width="80dp"
 82             android:layout_height="wrap_content" />
 83         <Button
 84             android:id="@+id/ButtonJian"
 85             android:text="-"
 86             android:textSize="25dp"
 87             android:layout_width="80dp"
 88             android:layout_height="wrap_content" />
 89
 90     </LinearLayout>
 91
 92     <!--1 2 3 * -->
 93     <LinearLayout
 94         android:layout_width="match_parent"
 95         android:layout_height="wrap_content"
 96         android:paddingTop="5dp"
 97         android:orientation="horizontal"
 98         >
 99
100         <Button
101             android:id="@+id/Button01"
102             android:text="1"
103             android:textSize="25dp"
104             android:layout_width="80dp"
105             android:layout_height="wrap_content" />
106         <Button
107             android:id="@+id/Button02"
108             android:text="2"
109             android:textSize="25dp"
110             android:layout_width="80dp"
111             android:layout_height="wrap_content" />
112         <Button
113             android:id="@+id/Button03"
114             android:text="3"
115             android:textSize="25dp"
116             android:layout_width="80dp"
117             android:layout_height="wrap_content" />
118         <Button
119             android:id="@+id/ButtonCheng"
120             android:text="*"
121             android:textSize="25dp"
122             android:layout_width="80dp"
123             android:layout_height="wrap_content" />
124
125     </LinearLayout>
126
127     <!--0 C = / -->
128     <LinearLayout
129         android:layout_width="match_parent"
130         android:layout_height="wrap_content"
131         android:paddingTop="5dp"
132         android:orientation="horizontal"
133         >
134
135         <Button
136             android:id="@+id/Button00"
137             android:text="0"
138             android:textSize="25dp"
139             android:layout_width="80dp"
140             android:layout_height="wrap_content" />
141         <Button
142             android:id="@+id/ButtonC"
143             android:text="C"
144             android:textSize="25dp"
145             android:layout_width="80dp"
146             android:layout_height="wrap_content" />
147         <Button
148             android:id="@+id/ButtonDeng"
149             android:text="="
150             android:textSize="25dp"
151             android:layout_width="80dp"
152             android:layout_height="wrap_content" />
153         <Button
154             android:id="@+id/ButtonChu"
155             android:text="/"
156             android:textSize="25dp"
157             android:layout_width="80dp"
158             android:layout_height="wrap_content" />
159
160     </LinearLayout>
161
162
163
164
165
166 </LinearLayout>

在activity中实现具体的功能

  1 package com.example.whs.sample01_1_activity;
  2
  3 import android.provider.Settings;
  4 import android.support.v7.app.AppCompatActivity;
  5 import android.os.Bundle;
  6 import android.view.View;
  7 import android.widget.Button;
  8 import android.widget.TextView;
  9 import android.widget.Toast;
 10
 11 public class JiSuanActivity02 extends AppCompatActivity {
 12
 13     TextView tv;
 14     int[] buttons;      //数字按钮数组
 15     int result;
 16     int result0;
 17     int  result1;
 18
 19     //按钮对象声明
 20     Button buttonC;
 21     Button buttonJia;
 22     Button buttonJian;
 23     Button buttonCheng;
 24     Button buttonChu;
 25     Button buttonDengyu;
 26
 27     String str1;    //旧输入的值
 28     String str2;    //新输入的值
 29
 30     int flag=0;     //计算标志位,0第一次输入;1加; 2减; 3乘; 4除; 5等于
 31     Button temp;
 32
 33     @Override
 34     protected void onCreate(Bundle savedInstanceState) {
 35         super.onCreate(savedInstanceState);
 36         setContentView(R.layout.activity_ji_suan02);
 37
 38         initButton();
 39         //清空按钮点击事件
 40         buttonC.setOnClickListener(new View.OnClickListener() {
 41             @Override
 42             public void onClick(View view) {
 43                 str1 = "";
 44                 str2 = "";
 45                 tv.setText("0");
 46                 result = 0;
 47                 result1 = 0;
 48                 result0 = 0;
 49                 flag = 0;
 50             }
 51         });
 52
 53         //监听
 54         for (int i = 0; i < buttons.length; i++){
 55             temp = getBtnForId(buttons[i]);
 56             temp.setOnClickListener(new View.OnClickListener() {
 57                 @Override
 58                 public void onClick(View view) {
 59                     if (flag != 0){
 60                         str1 = "";
 61                     }else {
 62                         str1 = tv.getText().toString().trim();
 63                         if (str1.equals("0")){
 64                             str1 = "";
 65                         }
 66                     }
 67
 68                     str1 = str1 + String.valueOf(((Button)view).getText()); //获取新值
 69                     tv.setText(str1);
 70                 }
 71             });
 72         }
 73
 74         buttonListener(buttonJia, 1);
 75         buttonListener(buttonJian, 2);
 76         buttonListener(buttonCheng, 3);
 77         buttonListener(buttonChu, 4);
 78
 79         buttonDengyu.setOnClickListener(new View.OnClickListener() {
 80             @Override
 81             public void onClick(View view) {
 82
 83                 result1 = Integer.parseInt(str1);
 84
 85                 if (flag == 1){
 86                     result = result0 + result1;
 87                 }else if (flag == 2){
 88                     result = result0 - result1;
 89                 }else if (flag == 3){
 90                     result = result0 * result1;
 91                 }else if (flag == 4){
 92                     if (result1 == 0){
 93                         Toast.makeText(JiSuanActivity02.this, "除数不能为0", Toast.LENGTH_SHORT).show();
 94                     }else {
 95                         result = result0 / result1;
 96                     }
 97
 98                 }else if (flag == 0){
 99                     result = result1;
100                 }
101                 String str = (result + "").trim();
102
103                 if (result1 == 0 && flag == 4){
104                     str = "错误";
105                 }
106                 tv.setText(str);
107                 Toast.makeText(JiSuanActivity02.this, "结果是:" + result, Toast.LENGTH_SHORT).show();
108             }
109         });
110     }
111
112     //初始化控件资源
113     public void initButton(){
114         tv = (TextView)this.findViewById(R.id.tv);
115         str1 = String.valueOf(tv.getText());
116         str2 = "";
117         buttonC = getBtnForId(R.id.ButtonC);
118         buttonJia = getBtnForId(R.id.ButtonJia);
119         buttonJian = getBtnForId(R.id.ButtonJian);
120         buttonCheng = getBtnForId(R.id.ButtonCheng);
121         buttonChu = getBtnForId(R.id.ButtonChu);
122         buttonDengyu = getBtnForId(R.id.ButtonDeng);
123
124         buttons = new int[]{
125                 R.id.Button00,R.id.Button01,R.id.Button02,
126                 R.id.Button03,
127                 R.id.Button04,R.id.Button05,R.id.Button06,
128                 R.id.Button07,R.id.Button08,R.id.Button09
129         };
130     }
131     //根据id获取Button
132     public Button getBtnForId(int rID){
133         Button btn = (Button)this.findViewById(rID);
134         return btn;
135     }
136     //按钮监听
137     public void buttonListener(Button button, final int id){
138         button.setOnClickListener(new View.OnClickListener() {
139             @Override
140             public void onClick(View view) {
141                 String str = tv.getText().toString().trim();
142                 result0 = Integer.parseInt(str);
143                 //tv.setText("");
144                 flag = id;
145             }
146         });
147     }
148 }

转载于:https://www.cnblogs.com/whongs/p/7452001.html

android简易计算器的实现相关推荐

  1. android简易计算器

    android简易计算器 说明:完成简易版计算器,能够实现基本的加.减.乘.除运算 图示: 布局文件 <?xml version="1.0" encoding="u ...

  2. android简易计算器(两位数的加减乘除求余)

      该项目是两年前刚学android时,课堂上老师布置的一个作业, 要求是:能够实现两位数(正数或负数)的加减乘除以及求余的功能. 一.简易计算器界面展示 1.两位数的加法 2.两位数的减法 3.两位 ...

  3. Android简易计算器的制作

    今天算是闲着没事做了一个小型的计算器-顺便熟悉一下Android的布局,组件,以及时间监听的方法-就当是做了一个小小的练习吧- 顺便去对比了一下别人写的代码-有的使用到了集合框架去实现,我却是用的数组 ...

  4. android计算器开发报告总结,android简易计算器总结

    一:如图,首先布局计算器主页显示: activity_main.xml xmlns:tools="http://schemas.android.com/tools" android ...

  5. android 坐标点计算器,Android实现简易计算器

    开之前我还是想问问老师,为什么一定要星期天前交作业呢?由于条件限制,作品是赶出来的不是细细琢磨出来的.所以在这版apk中功能较为简易,有待后期再不断更新与优化 总体效果图如下 布局activity_m ...

  6. android计算器弹窗,android实现简易计算器

    本文实例为大家分享了android实现简易计算器展示的具体代码,供大家参考,具体内容如下 效果图: 一.如图,首先布局计算器主页显示 activity_main.xml xmlns:tools=&qu ...

  7. android studio大作业-简易计算器实现

    android studio大作业-简易计算器实现 先看效果图 基本功能:加,减,乘,除 核心代码实现 public class MainActivity extends AppCompatActiv ...

  8. 简单的android小程序计算机,Android实现简易计算器小程序

    本文实例为大家分享了Android实现简易计算器小程序的具体代码,供大家参考,具体内容如下 目标效果: 通过编写代码,可以实现整数和小数的加减乘除运算,以及删除和清空的功能. 1.页面中Button使 ...

  9. Android小程序-简易计算器的实现

    目标效果:   通过编写代码,可以实现整数和小数的加减乘除运算,以及删除和清空的功能. 1.页面中Button使用的是线性布局,最外边一个是父布局,第一行C,DEL,/,*为第一个子布局,第二行7,8 ...

最新文章

  1. RNA-seq最新利器——全长转录组测序
  2. Bitbucket Pipelines在Atlassian的Bitbucket云上提供持续交付功能
  3. 关于边缘计算,那些不边缘的“术”与“道”
  4. EntityFramework Core如何映射动态模型?
  5. 小程序显示服务器开小差,小程序提交一直显示网络错误,网络开小差,请刷新重试,切换网络也上不去,请问是?...
  6. Oracle主库、备库redo日志管理
  7. 2021-2025年中国制药行业MR报告软件行业市场供需与战略研究报告
  8. 螺旋传动设计系统lisp_[罗升机电]第236期 丝杆升降机系统工程之齿轮传动的设计!...
  9. c语言程序设计题库 微盘下载,《C语言程序设计》题库及答案.pdf
  10. 软考中级-软件设计师-查缺补漏
  11. java cobar_Cobar-Client 实现策略总结
  12. 电镀面积计算机公式,教你正确的计算电镀中施镀面积方法。
  13. 林达华推荐有关机器学习的数学书籍
  14. DEEPIN 设置U盘启动
  15. outlook 默认打开ie_如何使用OUTLOOK联系人取代手机联系人,实现电脑和手机联系人、日历、待办的同步...
  16. 考生合格证书打印的部分代码和生成证书方法
  17. C# TCP/IP通讯协议的整理(一)附带——基恩士扫码枪的使用
  18. vue中的数字动画及数字逗号显示
  19. 怎么能看出一个人开车水平高低?
  20. netty解决TCP粘包/拆包导致的半包读写问题的三种方案

热门文章

  1. laravel 使用队列进行微信模板消息的群发
  2. Win10打开文件夹闪退怎么解决
  3. win7设置桌面计算机图标不见了,win7桌面图标不见了_win7桌面图标不见了恢复显示设置教程 - 系统家园...
  4. Java中如何实现添加用户信息_如何通过Java客户端在Active Directory中创建新用户并将其添加到现有组...
  5. VUE登录注册页面,完整vue,直接复制
  6. pdf文件怎么编辑,如何修改pdf文字
  7. 微信小程序取本地数据库数据(实测有图)
  8. java获取jira上的任务
  9. 连接交换路由器的方式
  10. 声学的一些基本知识(1)