Android课程---计算器的实现
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hanqi.test5"> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".LongClickActivityActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".UIActivity1">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".CalculatorActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Activity1" /> </application> </manifest>
package com.hanqi.test5; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; public class CalculatorActivity extends Activity implements View.OnClickListener {
EditText et;
Button bt_clear;
Button bt_goback; //全局变量
Button bt_7;
Button bt_8;
Button bt_9;
Button bt_6;
Button bt_5;
Button bt_4;
Button bt_3;
Button bt_2;
Button bt_1;
Button bt_0;
Button bt_jia;
Button bt_jian;
Button bt_cheng;
Button bt_chu;
Button bt_dian;
Button bt_deng; //存储显式的内容
//StringBuilder:操作字符串的工具类
private StringBuilder str_show = new StringBuilder();//实例化 //当前运算符按钮的id
private int id_yunsf = 0;
//运算符前面的数值 Double:包装类
private Double number_1;
//运算符后面的数值
private Double number_2; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator); et = (EditText)findViewById(R.id.et);
bt_clear = (Button)findViewById(R.id.clear);
bt_goback = (Button)findViewById(R.id.goback);
bt_7 = (Button)findViewById(R.id.bt_7);
bt_8 = (Button)findViewById(R.id.bt_8);
bt_9 = (Button)findViewById(R.id.bt_9);
bt_6 = (Button)findViewById(R.id.bt_6);
bt_5 = (Button)findViewById(R.id.bt_5);
bt_4 = (Button)findViewById(R.id.bt_4);
bt_3 = (Button)findViewById(R.id.bt_3);
bt_2 = (Button)findViewById(R.id.bt_2);
bt_1 = (Button)findViewById(R.id.bt_1);
bt_0 = (Button)findViewById(R.id.bt_0);
bt_jia = (Button)findViewById(R.id.bt_jia);
bt_jian = (Button)findViewById(R.id.bt_jian);
bt_cheng = (Button)findViewById(R.id.bt_cheng);
bt_chu = (Button)findViewById(R.id.bt_chu);
bt_dian = (Button)findViewById(R.id.bt_dian);
bt_deng = (Button)findViewById(R.id.bt_deng); bt_clear.setOnClickListener(this);
bt_goback.setOnClickListener(this);
bt_7.setOnClickListener(this);
bt_8.setOnClickListener(this);
bt_9.setOnClickListener(this);
bt_6.setOnClickListener(this);
bt_5.setOnClickListener(this);
bt_4.setOnClickListener(this);
bt_3.setOnClickListener(this);
bt_2.setOnClickListener(this);
bt_1.setOnClickListener(this);
bt_0.setOnClickListener(this);
bt_jia.setOnClickListener(this);
bt_jian.setOnClickListener(this);
bt_cheng.setOnClickListener(this);
bt_chu.setOnClickListener(this);
bt_dian.setOnClickListener(this);
bt_deng.setOnClickListener(this); }
//事件源组件
public void onClick(View v)
{
Button bt = (Button)v; int id = bt.getId(); switch (id)
{
case R.id.clear:
str_show = new StringBuilder(); //重新实例化就可以清除
//设置显示框内容
et.setText(str_show); //et:显示框 number_1 = null;
number_2 = null;
id_yunsf = 0; break; case R.id.goback: if (str_show.length()>0)
{
//删除最后一个字符 索引值减1
str_show.deleteCharAt(str_show.length()-1);
et.setText(str_show);
break;
} case R.id.bt_7:
case R.id.bt_8:
case R.id.bt_9:
case R.id.bt_6:
case R.id.bt_5:
case R.id.bt_4:
case R.id.bt_3:
case R.id.bt_2:
case R.id.bt_1:
case R.id.bt_0: if (id == R.id.bt_0
||(id == R.id.bt_0 && !str_show.toString().equals("0")))
{
str_show.append(bt.getText());
}
et.setText(str_show);
break;
case R.id.bt_deng:
case R.id.bt_jia:
case R.id.bt_jian:
case R.id.bt_cheng:
case R.id.bt_chu: if (number_1 == null)
{
if (str_show.length()>0)
{
//把字符串转成Double
number_1 = new Double(str_show.toString());
id_yunsf = bt.getId();
str_show = new StringBuilder(); //重新实例化就可以清除
//设置显示框内容
//et.setText(str_show); //et:显示框
}
}
else//计算
{
if (str_show.length()>0)
{
//把字符串转成Double 赋值
number_2 = new Double(str_show.toString());
} switch (id_yunsf)
{
case R.id.bt_jia:
//运算
double res = number_1.doubleValue() + number_2.doubleValue();
break; case R.id.bt_jian:
//运算
number_1 = number_1.doubleValue() - number_2.doubleValue();
break;
case R.id.bt_cheng:
//运算
number_1 = number_1.doubleValue() * number_2.doubleValue();
break;
case R.id.bt_chu:
//运算
if (number_2 !=0)
{
number_1 = number_1.doubleValue() / number_2.doubleValue();
}
else
{
Toast.makeText(this,"不能除0",Toast.LENGTH_LONG).show();
}
break;
}
//运算符
id_yunsf = bt.getId();
//显示当前运算结果
et.setText(number_1.toString());
//清空
str_show = new StringBuilder(); }
break;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="6"
android:columnCount="4">
<!--行:rowCount 列:columnCount--> <EditText
android:layout_columnSpan="4"
android:layout_gravity="fill_horizontal"
android:layout_rowWeight="1"
android:textSize="30sp"
android:editable="false"
android:gravity="right|center_vertical"/>
<!--水平填充:fill_horizontal 竖直填充:fill_vertical
行号:layout_row 列号:layout_column(行号和列号都可以指定,但注意都是从0开始的)-->
<Button
android:text="清除"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="20sp"
android:id="@+id/clear"/>
<Button
android:text="后退"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="20sp"
android:textColor="#0ff"
android:id="@+id/goback"/>
<Button
android:text="/"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="20sp"
android:id="@+id/bt_chu"/>
<Button
android:text="x"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="20sp"
android:id="@+id/bt_cheng"/>
<Button
android:text="7"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="20sp"
android:id="@+id/bt_7"/>
<Button
android:text="8"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="20sp"
android:id="@+id/bt_8"/>
<Button
android:text="9"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="20sp"
android:id="@+id/bt_9"/>
<Button
android:text="-"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="20sp"
android:id="@+id/bt_jian"/>
<Button
android:text="4"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="20sp"
android:id="@+id/bt_4"/>
<Button
android:text="5"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="20sp"
android:id="@+id/bt_5"/>
<Button
android:text="6"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="20sp"
android:id="@+id/bt_6"/>
<Button
android:text="+"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="20sp"
android:id="@+id/bt_jia"/>
<Button
android:text="1"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="20sp"
android:id="@+id/bt_1"/>
<Button
android:text="2"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="20sp"
android:id="@+id/bt_2"/>
<Button
android:text="3"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="20sp"
android:id="@+id/bt_3"/>
<Button
android:text="="
android:layout_rowSpan="2"
android:layout_gravity="fill_vertical"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="20sp"
android:id="@+id/bt_deng"/>
<Button
android:text="0"
android:layout_columnSpan="2"
android:layout_gravity="fill_horizontal"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="20sp"
android:background="#FF0000"
android:id="@+id/bt_0"/>
<Button
android:text="."
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="20sp"
android:textColor="#FF0000"
android:id="@+id/bt_dian"/> </GridLayout>
这些代码可以实现计算器的基本功能
效果:
Android课程---计算器的实现的更多相关文章
- 第一个Android项目——计算器
第一个Android项目——计算器 效果 开始学Android一两个星期了,学习了一下基本的Activity.简单控件及几个简单布局,打算找个东西来练练手,于是就选择发计算器.关于计算器中用到的四则运 ...
- 一培训机构设计的学习android课程内容:供大家参考
转自:http://www.cnblogs.com/csj007523/archive/2011/06/16/2082682.html 一培训机构设计的学习android课程内容:供大家参考 第一阶段 ...
- Android 简单计算器源码....
PS:今天算是闲着没事做了一个小型的计算器...顺便熟悉一下Android的布局,组件,以及时间监听的方法...就当是做了一个小小的练习吧... 顺便去对比了一下别人写的代码...有的使用到了 ...
- android实现计算器功能
设计一个简单的计算器. 第一个Activity的界面. 第二个Activity显示算式和计算结果. 第一个Activity代码: import android.app.Activity; import ...
- 【留念贴】Android开发——计算器
[过程] 在电商学霸&&代码女神XuFei的影响下,接触到了关于Android客户端的一些开发,第一次在Android平台搞出了一个App,真的是激动不已,所以必须开个留念贴记录一下. ...
- Android 简单计算器实现源码
1.string.xml代码 <?xml version="1.0" encoding="utf-8"?> <resources> &l ...
- android的计算器
今天我闲着无聊,便想仿照Iphone的计算器自己写一个出来玩玩,于是就开动脑经,来场头脑风暴了!我拿什么了写呢?wp?这是个不错的选择,但是我最近在研究安卓的开发(求各位MSP不要烧我,我买不起MAC ...
- android课程第一节(TextView控件使用)
TextView控件使用 一.TextView基本使用(创建方式) 1.在程序中创建TextView对象 如下代码: @Override protected void onCreate(Bundle ...
- Android学习(七) Android实现计算器
前台页面代码,通过线性布局方式实现计算器页面:如图所示 color.xml,自定义颜色values: <?xml version="1.0" encoding="u ...
随机推荐
- json入门(二)
背景 之前最早的时候,也见过类似于这样的字符串: {"list":[ {"ArticleId":7392749,"BlogId&q ...
- hdu2546 01背包
http://acm.split.hdu.edu.cn/showproblem.php?pid=2546 01背包问题,首先拿出5元买最贵的东西,那接下来就是背包容量m-5,物品数量n-1 的01背包 ...
- css随记02布局
布局 二栏布局 利用absolute, margin .container { position: relative; } nav { position: absolute; left: 0px; w ...
- Thymeleaf 集成spring
Thymeleaf 集成spring 如需先了解Thymeleaf的单独使用,请参考<Thymeleaf模板引擎使用>一文. 依赖的jar包 Thymeleaf 已经集成了spring的3 ...
- C# 词法分析器(四)构造 NFA
系列导航 (一)词法分析介绍 (二)输入缓冲和代码定位 (三)正则表达式 (四)构造 NFA (五)转换 DFA (六)构造词法分析器 (七)总结 有了上一节中得到的正则表达式,那么就可以用来构造 N ...
- apache activemq 学习笔记
0.activemq的概念 activemq实现了jms(java Message server),用于接收,发送,处理消息的开源消息总线. 1.activemq和jms的区别 jms说白了就是jav ...
- Repeater 双向排序
做项目的时候,DataGrid ,DataList,Repeater 三个控件都是很优秀的数据显示控件,DataGrid的方便,简单易用,功能强大,但对性能会有所影响,在loading页面的时候大 ...
- Nodejs基础中间件Connect
http://www.tuicool.com/articles/emeuie 关于作者 张丹(Conan), 程序员Java,R,PHP,Javascript weibo:@Conan_Z blog: ...
- android 百度地图 通过剪裁图片添加 Marker
初始化百度地图: private void initViews() { mMapView = (MapView) findViewById(R.id.bmapView); mBaiduMap = mM ...
- lua 获取文件名和扩展名
local str = "aaa.bbb.bbb.txt" --获取文件名 function getFileName(str) local idx = str:match(&quo ...