Android——计算器第一次完善
完善:
1- 处理首位为0
2- 处理首位为“.”
3- 处理前两位为“0.”,此时首位为0,但是不能处理
4- 处理小数点不能重复输入
发现bug:12.3x6 = 如下图:

xml
<?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">
<!--6行4列
实现占满整个屏幕--> <EditText
android:layout_columnSpan="4"
android:layout_gravity="fill_horizontal"
android:layout_rowWeight="2"
android:editable="false"
android:id="@+id/et"
android:gravity="right|center"
android:textSize="50dp"
android:text="0"/>
<!--跨四列 自动填充 权重2-->
<Button
android:text="清除"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="20dp"
android:textColor="#00F"
android:id="@+id/bt_clear"/>
//列 行权重为1
<Button
android:text="后退"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="20dp"
android:textColor="#00f"
android:id="@+id/bt_goback"/>
//列 行权重为1
<Button
android:text="/"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="20dp"
android:id="@+id/chu"/>
//列 行权重为1
<Button
android:text="x"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="20dp"
android:id="@+id/cheng"/>
//列 行权重为1
<Button
android:text="7"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="20dp"
android:id="@+id/bt_7"/>
//列 行权重为1
<Button
android:text="8"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="20dp"
android:id="@+id/bt_8"/>
//列 行权重为1
<Button
android:text="9"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="20dp"
android:id="@+id/bt_9"/>
//列 行权重为1
<Button
android:text="-"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="20dp"
android:id="@+id/jian"/>
//列 行权重为1
<Button
android:text="4"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="20dp"
android:id="@+id/bt_4"/>
//列 行权重为1
<Button
android:text="5"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="20dp"
android:id="@+id/bt_5"/>
//列 行权重为1
<Button
android:text="6"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="20dp"
android:id="@+id/bt_6"/>
//列 行权重为1
<Button
android:text="+"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="20dp"
android:id="@+id/jia"/>
//列 行权重为1
<Button
android:text="1"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="20dp"
android:id="@+id/bt_1"/>
//列 行权重为1
<Button
android:text="2"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="20dp"
android:id="@+id/bt_2"/>
//列 行权重为1
<Button
android:text="3"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="20dp"
android:id="@+id/bt_3"/>
//列 行权重为1
<Button
android:text="="
android:layout_rowSpan="2"
android:layout_gravity="fill_vertical"
android:layout_columnWeight="1"
android:layout_rowWeight="2"
android:textSize="20dp"
android:background="#22ac38"
android:id="@+id/result"/>
//跨两行 自动填充 绿色 列权重1 行权重2
<Button
android:text="0"
android:layout_columnSpan="2"
android:layout_gravity="fill_horizontal"
android:layout_columnWeight="2"
android:layout_rowWeight="1"
android:textSize="20dp"
android:id="@+id/bt_0"/>
//跨两列 自动填充 列权重2 行权重1
<Button
android:text="."
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="20dp"
android:id="@+id/dian"/>
//列 行 权重1 </GridLayout>
java
package com.example.chenshuai.test322; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; /**
* Created by chenshuai anniu1 2016/3/26.
*/
public class Activity2 extends AppCompatActivity implements View.OnClickListener{ EditText et;
Button bt_clear;
Button bt_goback;
Button bt_1;
Button bt_2;
Button bt_3;
Button bt_4;
Button bt_5;
Button bt_6;
Button bt_7;
Button bt_8;
Button bt_9;
Button bt_0;
Button jia;
Button jian;
Button cheng;
Button chu;
Button result;
Button dian; //显示屏 全局变量
//存储显示的内容
//StringBuilder 操作字符串的工具类
private StringBuilder str_show = new StringBuilder(); //当前运算符按钮的id
private int id_yunsf = 0; //保存输入的值 用包装类,基本数值类型不能判断为空
private Double number_1;//运算符前面的数值
private Double number_2;//运算符后面的数值 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridlayout); //实例化监听器
et = (EditText)findViewById(R.id.et);
bt_1 = (Button)findViewById(R.id.bt_1);
bt_2= (Button)findViewById(R.id.bt_2);
bt_3 = (Button)findViewById(R.id.bt_3);
bt_4= (Button)findViewById(R.id.bt_4);
bt_5 = (Button)findViewById(R.id.bt_5);
bt_6= (Button)findViewById(R.id.bt_6);
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_0= (Button)findViewById(R.id.bt_0);
jia = (Button)findViewById(R.id.jia);
jian= (Button)findViewById(R.id.jian);
cheng = (Button)findViewById(R.id.cheng);
chu= (Button)findViewById(R.id.chu);
result = (Button)findViewById(R.id.result);
dian= (Button)findViewById(R.id.dian);
bt_clear = (Button)findViewById(R.id.bt_clear);
bt_goback= (Button)findViewById(R.id.bt_goback); bt_0.setOnClickListener(this);
bt_1.setOnClickListener(this);
bt_2.setOnClickListener(this);
bt_3.setOnClickListener(this);
bt_4.setOnClickListener(this);
bt_5.setOnClickListener(this);
bt_6.setOnClickListener(this);
bt_7.setOnClickListener(this);
bt_8.setOnClickListener(this);
bt_9.setOnClickListener(this);
jia.setOnClickListener(this);
jian.setOnClickListener(this);
cheng.setOnClickListener(this);
chu.setOnClickListener(this);
dian.setOnClickListener(this);
result.setOnClickListener(this);
bt_goback.setOnClickListener(this);
bt_clear.setOnClickListener(this); } //View 事件源组件
public void onClick(View v)
{
Button bt = (Button)v; int id = bt.getId(); switch (id)
{
case R.id.bt_clear:
//重新实例化,实现清除
str_show = new StringBuilder();
et.setText(str_show); number_1 = null;
number_2 = null;
id_yunsf = 0;
break; case R.id.bt_goback:
//防止删到最后一个
if(str_show.length()>0)
//实现后退 索引值从0开始 删除最后一个字符 {
str_show.deleteCharAt(str_show.length() - 1);
et.setText(str_show);
} break; case R.id.bt_0:
case R.id.bt_1:
case R.id.bt_2:
case R.id.bt_3:
case R.id.bt_4:
case R.id.bt_5:
case R.id.bt_6:
case R.id.bt_7:
case R.id.bt_8:
case R.id.bt_9: if (id !=R.id.bt_0
||(id==R.id.bt_0 && !str_show.toString().equals("0")) ) {
str_show.append(bt.getText()); //长度大于等于2,截取第一位,判断为0的话,字符串从第一位开始截取,实现首位不为0,
// 同时判断第二位是否是点,如果输入的是"0."对首位为0不再操作
if (str_show.length() >= 2) { //首位不为0
String shouwei = str_show.substring(0, 1); String second = str_show.substring(1,2); //首位不为0,第二位不为点
if (shouwei.equals("0") && !second.equals(".") ) { //et.setText(str_show.substring(1)); String dian = str_show.substring(1); et.setText(dian);
}
else { et.setText(str_show); } }
else { et.setText(str_show); } } break; case R.id.dian: //判断.是否存在 如果不存在
if (str_show.indexOf(".") == -1)
{
//先把输入的值放进来再判断
str_show.append(bt.getText()); //第一次输入的是'.',转换为'0.'
if (str_show.substring(0,1).equals("."))
{
str_show.replace(0, 1, "0.");
}
//str_show.append(bt.getText());
}
else
{ }
et.setText(str_show); break;
case R.id.result:
case R.id.jia:
case R.id.jian:
case R.id.cheng:
case R.id.chu: //前面值是空,转换一下
if (number_1==null)
{
//运算符不能在第一个
if (str_show.length()>0)
{
number_1 = new Double(str_show.toString()); id_yunsf = bt.getId(); //重新实例化,实现清除
str_show = new StringBuilder();
//et.setText(str_show);
}
} else//直接计算
{
if (str_show.length()>0)
{
//运算符后边的内容赋值
number_2 = new Double(str_show.toString());
} //判断运算符
switch (id_yunsf)
{
case R.id.jia: //运算
number_1 = number_1.doubleValue() + number_2.doubleValue(); break; case R.id.jian:
//运算 //结果给number_1
number_1 = number_1.doubleValue() - number_2.doubleValue(); break;
case R.id.cheng:
//运算 //结果给number_1
number_1 = number_1.doubleValue() * number_2.doubleValue(); break;
case R.id.chu:
//运算 //结果给number_1
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;
}
} }
Android——计算器第一次完善的更多相关文章
- Android计算器简单逻辑实现
Android计算器简单逻辑实现 引言: 我的android计算器的实现方式是:按钮输入一次,就处理一次. 但是如果你学过数据结构(栈),就可以使用表达式解析(前缀,后缀)处理. 而这个方式已经很成熟 ...
- Android计算器布局
Android(安桌)计算器布局实现 ——解决整个屏幕方案 引言: 学完了android布局的几种方式,做了一个android计算器. 我在网上搜索了这方面的资料,发现了布局都 ...
- Android计算器尝试
学了一段时间Android了,一直都是在看,没有什么尝试,刚好最近大致学会了gridview配合simpleadpter的使用,于是想着动手练习一下,就选择了写一个最简单的计算器来实现. 只包含+-* ...
- Android应用第一次安装成功点击“打开”后Home键切出应用后再点击桌面图标返回导致应用重启问题
最近项目中遇到一个问题,用户第一次安装应用在系统的安装器安装完成界面有“完成”和“打开”两个按钮. 当用户点击“打开”按钮进入用户注册页面进行手机号验证码发送和验证码输入等操作界面,若此时用户点击Ho ...
- java实现可有括号的android计算器
写了一个android版的计算器,可以计算带括号的表达式,不过前提是:正确的表达式才行 小缺陷是没有做表达式括号的控制,现在还没有想到好的控制方式 package javaAdvanced; impo ...
- Android计算器开发实例
Android简单计算器开发实例如图: ==================================================== activity_main.xml 代码如下: < ...
- 结对实验报告-android计算器设计
一:引言 目前手机可以说是普及率非常高的电子设备了,由于其便于携带,使用方便,资费适中等等原因,现在手机已经在一定程度开始代替固定电话的通话功能,以及一些原来电脑软件上的功能了.手机上的软件也随着 ...
- Android Studio 第一次新建Android Gradle项目超级慢的解决方案
大家有什么问题,欢迎问我! 注:Android Studio在第一次新建一个Gradle项目时需要下载Gradle,所以启动很慢(Gradle-bin大约三十几兆),所以我们应该事先帮他下载好. 首先 ...
- Android——计算器
layout文件: <?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:andr ...
随机推荐
- 【OpenCV】给图像加入噪声
图像噪声使图像在获取或是传输过程中收到随机信号干扰,妨碍人们对图像理解及分析处理的信号.非常多时候将图像噪声看做多维随机过程,因而描写叙述噪声的方法全然能够借用随机过程的描写叙述,也就是使用随机过程的 ...
- hibernate的hql查询语句总结
这篇随笔将会记录hql的常用的查询语句,为日后查看提供便利. 在这里通过定义了三个类,Special.Classroom.Student来做测试,Special与Classroom是一对多,Class ...
- 第一篇:初识ASP.NET控件开发_第二节:HelloWorld
1)步骤一:新建类库项目:Controls,创建新解决方案:CustomLibrary 2)步骤二:在类库项目中添加“ASP.NET服务器控件”新建项:RenderHelloWorld.cs (也可以 ...
- Ubuntu下设置开机后自动运行命令(转)
从道理上来讲,Ubuntu开机应该是能够设置执行一些脚本的,事实上确实如此,网上给出了很多解决的方案,基本上是分为两种, 第一种是编辑/etc/下的rc.local脚本, 然后把对应的需要执行的脚本写 ...
- 使用 sqlyog 导入导出数据显示 lost connection to mysql server during query
mysql中经常需要备份数据,在使用 sqlyog 进行备份数据库为转储文件,然后在其他数据库中导入发生 lost connection 经过查询大量资料是数据库配置的 max_allowed_pac ...
- Axure chrome 扩展显示已损坏的解决方法
下载地址 链接:https://pan.baidu.com/s/11K3t_mvgJg51siO_jNRejg 提取码:goz1 如果链接失效,请留言或站内信提醒我更新 疑问 之前用的好好的Axure ...
- 【Android UI】Android颜色系大全
原文:http://android.eoe.cn/topic/summary 利用颜色的变化来突出信息.选择契合您应用主题的颜色系,并且提供视觉对比效果.注意,色弱的人士可能无法分辨红色和绿色. 调色 ...
- 微信小程序 开发过程中遇到的坑(一)
2124 1.我们使用app.json文件来对微信小程序进行全局配置,决定页面文件的路径.窗口表现.设置网络超时时间.设置多 tab 的时候在pages中写注释的时候回报错. 例如: { &quo ...
- MD5 和的价值体现在哪里,它是用来做什么的?
MD5 和的价值体现在哪里,它是用来做什么的? MD5 和是由字母和数字构成的字符串,起到了文件指纹的作用.如果两个文件有相同的 MD5 和值,那么,文件完全相同.您可以为每一软件下载使用所提供的 M ...
- python常用运维脚本实例
转载 file是一个类,使用file('file_name', 'r+')这种方式打开文件,返回一个file对象,以写模式打开文件不存在则会被创建.但是更推荐使用内置函数open()来打开一个文件 ...