android简易计算器的实现

摘要:
最近,当我接触Android开发时,我尝试编写一个计算器小程序:xml文件中的布局代码如下:1˂?

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

android简易计算器的实现第1张

在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 }

免责声明:文章转载自《android简易计算器的实现》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇perl多线程使用RedisDesktopManager软件窗口不显示下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

FragmentTabHost用法

FragmentTabHost组成 Tabhost,TabWidget,切换的内容容器FrameLayout 层级关系 ----FragmentTabHost |-----TabWidget |-----FrameLayout 布局实现 实现tabhost采用android.support.v4.app.FragmentTa...

【snmp】win7安装和配置snmp

一、安装SNMP 1、打开控制面板—>程序和功能—>打开或关闭Windows功能 勾上复选框,点击确定按钮 2、开始安装SNMP 3、snmp安装完成,需要重启计算机才能生效,可以立即重启或稍后重启  安装完成后,再次打开控制面板—>程序和功能—>打开或关闭Windows功能,简单网络管理协议(SNMP)这里是勾选上的   二...

[转载]ExtJs4 笔记(6) Ext.MessageBox 消息对话框

 作者:李盼(Lipan) 出处:[Lipan] (http://www.cnblogs.com/lipan/)版权声明:本文的版权归作者与博客园共有。转载时须注明本文的详细链接,否则作者将保留追究其法律责任。 本篇演示消息对话框的用法,ExtJs封装了可能用到的各类消息框,并支持自定义的配置。 如下是用到的html: [html] <h1>各...

android layout 按比例布局

为了创建比例大小的子View,可以将LinearLayout的宽度和高度设为fill_parent, 而将子View的宽度或是高度设为0,然后为子View设置不同权重(weight) ,这样子View的大小就会权值成比例。 本例使用横向LinearLayout,LinearLayout的android:layout_width=”match_paren...

第一行代码

基本布局 线性布局:LinearLayout android:orientation:指定排列的方向,参数:vertical/horizontal。不指定时,默认的排列方向是horizontal。 android:layout_gravity:指定控件在布局的对齐方式。可以用“|”分割,同时指定多个参数。 注意: 排列方向为horizontal时...

Android开发 TextView的开发记录

前言  此篇博客是记录一些TextView开发上一些少用的开发功能项.目前开发记录如下:   添加图片   文字滚动   添加省略号   实现长文的收起和展开功能   改变一个字符串里自定字符的颜色或者大小 效果字体(粗体/斜体/下划线)增加效果字体的方法有很多既可以在xml属性里设置,又可以在代码里设置.(这里我们除了不演示使用SpannableSt...