应一个朋友的邀请,叫我写一个计算器,开始觉得,就一个计算器嘛,很简单的,但是写着写着发现自己写出来的逻辑真不严谨,于是搜索了一下,看到mk(没有打广告….)上有视频,于是看了下他的逻辑,以前还真是小瞧计算器了,计算器要是高级一点的处理起来更加繁琐,这里就做了一个简单一点的,这次用的是Android Studio 开发的,毕竟自己也是一直很推崇AS,但是在之前的博客里都只是为了方便,一直在用Eclipse
一直强烈推荐大家还是转移到AS上去,虽然我工作一直在用Eclipse,不过AS的脚步一直没有落下,好处多多!
这个仍然还是有许多不完善的地方,如果看官觉得可以改的话,欢迎参与
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@drawable/main_bg"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center">
<EditText
android:id="@+id/et_showview"
android:layout_width="350dip"
android:layout_height="100dip"
android:background="@drawable/whitebg"
android:editable="false"
android:gravity="bottom|right"
android:textSize="20sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="20dip"
android:gravity="center_horizontal"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/btn_clear"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="C"
android:textSize="20sp" />
<Button
android:id="@+id/btn_del"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dip"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="DEL"
android:textSize="20sp" />
<Button
android:id="@+id/btn_divide"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dip"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="÷"
android:textSize="20sp" />
<Button
android:id="@+id/btn_multiply"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dip"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="×"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/btn_7"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="7"
android:textSize="20sp" />
<Button
android:id="@+id/btn_8"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dip"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="8"
android:textSize="20sp" />
<Button
android:id="@+id/btn_9"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dip"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="9"
android:textSize="20sp" />
<Button
android:id="@+id/btn_minus"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dip"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="-"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/btn_4"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="4"
android:textSize="20sp" />
<Button
android:id="@+id/btn_5"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dip"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="5"
android:textSize="20sp" />
<Button
android:id="@+id/btn_6"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dip"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="6"
android:textSize="20sp" />
<Button
android:id="@+id/btn_pluse"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dip"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="+"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:gravity="center_horizontal"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_1"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="1"
android:textSize="20sp" />
<Button
android:id="@+id/btn_2"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dip"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="2"
android:textSize="20sp" />
<Button
android:id="@+id/btn_3"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dip"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="3"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:orientation="horizontal">
<Button
android:id="@+id/btn_0"
android:layout_width="170dp"
android:layout_height="80dp"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="0"
android:textSize="20sp" />
<Button
android:id="@+id/btn_point"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dip"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="."
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/btn_equal"
android:layout_width="80dp"
android:layout_height="170dip"
android:layout_marginLeft="10dip"
android:background="@drawable/orange_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="="
android:textSize="20sp" />
</LinearLayout>
<TextView
android:gravity="center"
android:autoLink="web"
android:layout_width="350dp"
android:layout_height="0dp"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:text="微博:http://weibo.com/Glorystys
博客:http://blog.csdn.net/qq_26787115"
android:textColor="@android:color/white"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
package com.lgl.calculator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
/**
* 计算器
*/
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_0;
private Button btn_1;
private Button btn_2;
private Button btn_3;
private Button btn_4;
private Button btn_5;
private Button btn_6;
private Button btn_7;
private Button btn_8;
private Button btn_9;
private Button btn_point;// 小数点
private Button btn_divide;// 除以
private Button btn_multiply;// 乘以
private Button btn_minus;// 减去
private Button btn_pluse;// 加
private Button btn_equal;// 等于
private Button btn_clear; //清空
private Button btn_del; //取消
private EditText et_showview; //输入框
private double dou = 0; //接收结果
/**
* 加一个标识
* 需求就是我们运算出结果的时候,再次点击数字及你选哪个下一次运算,这个时候,输入框应该自动为空
*/
private boolean flag; //清空标识
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
btn_0 = (Button) findViewById(R.id.btn_0);
btn_1 = (Button) findViewById(R.id.btn_1);
btn_2 = (Button) findViewById(R.id.btn_2);
btn_3 = (Button) findViewById(R.id.btn_3);
btn_4 = (Button) findViewById(R.id.btn_4);
btn_5 = (Button) findViewById(R.id.btn_5);
btn_6 = (Button) findViewById(R.id.btn_6);
btn_7 = (Button) findViewById(R.id.btn_7);
btn_8 = (Button) findViewById(R.id.btn_8);
btn_9 = (Button) findViewById(R.id.btn_9);
btn_point = (Button) findViewById(R.id.btn_point);// 小数点
btn_divide = (Button) findViewById(R.id.btn_divide);// 除以
btn_multiply = (Button) findViewById(R.id.btn_multiply);// 乘以
btn_minus = (Button) findViewById(R.id.btn_minus);// 减去
btn_pluse = (Button) findViewById(R.id.btn_pluse);// 加
btn_equal = (Button) findViewById(R.id.btn_equal);// 等于
btn_clear = (Button) findViewById(R.id.btn_clear);
btn_del = (Button) findViewById(R.id.btn_del);
et_showview = (EditText) findViewById(R.id.et_showview);
btn_0.setOnClickListener(this);
btn_1.setOnClickListener(this);
btn_2.setOnClickListener(this);
btn_3.setOnClickListener(this);
btn_4.setOnClickListener(this);
btn_5.setOnClickListener(this);
btn_6.setOnClickListener(this);
btn_7.setOnClickListener(this);
btn_8.setOnClickListener(this);
btn_9.setOnClickListener(this);
btn_point.setOnClickListener(this);
btn_divide.setOnClickListener(this);
btn_multiply.setOnClickListener(this);
btn_minus.setOnClickListener(this);
btn_pluse.setOnClickListener(this);
btn_equal.setOnClickListener(this);
btn_clear.setOnClickListener(this);
btn_del.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//我们在这里实现业务逻辑
String str = et_showview.getText().toString();
switch (v.getId()) {
//我们点击数字键,输入框就收到我们的计算数字
case R.id.btn_0:
case R.id.btn_1:
case R.id.btn_2:
case R.id.btn_3:
case R.id.btn_4:
case R.id.btn_5:
case R.id.btn_6:
case R.id.btn_7:
case R.id.btn_8:
case R.id.btn_9:
case R.id.btn_point:
//再次输入的时候判断,把前面的清空,再把点击的按钮放在输入框上
if (flag) {
flag = false;
str = "";
et_showview.setText("");
}
//我们只要点击键盘,相应的数字添加在EditText上
et_showview.setText(str + ((Button) v).getText());
break;
//运算符也是一样需要添加的
case R.id.btn_pluse:
case R.id.btn_minus:
case R.id.btn_multiply:
case R.id.btn_divide:
//再次输入的时候判断,把前面的清空,再把点击的按钮放在输入框上
if (flag) {
flag = false;
str = "";
et_showview.setText("");
}
//为了计算方便,我们可以在前后添加空格
et_showview.setText(str + " " + ((Button) v).getText() + " ");
break;
case R.id.btn_del:
//再次输入的时候判断,把前面的清空,再把点击的按钮放在输入框上
if (flag) {
flag = false;
str = "";
et_showview.setText("");
} else if (str != null && !str.equals(" ")) {
//判断,因为我们需要一个个删,首先确定是不是空或者""
//用substring()截取字符长度-1 再设置回去 形成删一个的效果
et_showview.setText(str.substring(0, str.length() - 1));
}
break;
//清除按钮,直接把输入框设置成空
case R.id.btn_clear:
flag = false;
str = "";
et_showview.setText(" ");
break;
//计算结果
case R.id.btn_equal:
getResult();
break;
}
}
//等号计算结果
private void getResult() {
//首先取一下你输入完后现在输入框的内容
String result = et_showview.getText().toString();
//当我们的输入框是null或者""时我们不进行操作
if (result == null || result.equals(" ")) {
return;
}
//我们要计算结果,必须知道输入框是否有操作符,判断条件就是前后是否在空格,否则不进行操作
if (!result.contains(" ")) {
//没有运算符,所以不用运算
return;
}
//当我点击等号的时候,清空标识设置为true,但是如果之前有的话,设置成false
if (flag) {
flag = false;
return;
}
flag = true;
//如果有空格,我们就截取前后段再获取运算符进行计算
String str1 = result.substring(0, result.indexOf(" ")); //运算符前面字段
String op = result.substring(result.indexOf(" ") + 1, result.indexOf(" ") + 2); //截取运算符
String str2 = result.substring(result.indexOf(" ") + 3); //运算符后面字段
//这里我们还是要判断str1和str2都不是空才
if (!str1.equals("") && !str2.equals("")) { //第一种情况:都不是空
//强转
double d1 = Double.parseDouble(str1);
double d2 = Double.parseDouble(str2);
//开始计算,定义一个double变量接收结果,定义为全局
if (op.equals("+")) { //加
dou = d1 + d2;
} else if (op.equals("-")) { //减
dou = d1 - d2;
} else if (op.equals("×")) { //乘
dou = d1 * d2;
} else if (op.equals("÷")) { //除
//除数为0不计算
if (d2 == 0) {
dou = 0;
} else {
dou = d1 / d2;
}
}
//我们之前把他强转为double类型了,但是如果没有小数点,我们就是int类型
if (!str1.contains(".") && !str2.contains(".") && !op.equals("÷")) {
int i = (int) dou;
et_showview.setText(i + "");
} else { //如果有小数点
et_showview.setText(dou + "");
}
} else if (!str1.equals("") && str2.equals("")) { //第二种情况:str2是空
//这种情况就不运算了
et_showview.setText(result);
} else if (str1.equals("") && !str2.equals("")) { //第三种情况:str1是空
//这种情况我们就需要判断了,因为此时str1 = 0;
double d3 = Double.parseDouble(str2);
if (op.equals("+")) { //加
dou = 0 + d3;
} else if (op.equals("-")) { //减
dou = 0 - d3;
} else if (op.equals("×")) { //乘
dou = 0;
} else if (op.equals("÷")) { //除
//除数为0不计算
dou = 0;
}
//我们之前把他强转为double类型了,但是如果没有小数点,我们就是int类型
if (!str2.contains(".")) {
int i = (int) dou;
et_showview.setText(i + "");
} else { //如果有小数点
et_showview.setText(dou + "");
}
} else { //最后一种情况,他们两个都是空
//我们清空掉
et_showview.setText("");
}
}
}
文章浏览阅读408次。 在ubuntu 8.04下安装Oracle 11g2008年05月22日 星期四 11:02oracle 11g 数据库虽然提供了linux x86的版本,但是支持的linux版本只有Red Hat,Novell and Solaris 这几个,debian 和 ubuntu 不在支持之列,所以在ubuntu下安装就相对麻烦一些,请照着下文的方法一步一步的安装,不
文章浏览阅读166次。新东方在线中考网整理了《初一英语下册语法知识点全汇总》,供同学们参考。一. 情态动词can的用法can+动词原形,它不随主语的人称和数而变化。1. 含有can的肯定句:主语+can+谓语动词的原形+其他。2. 含有can的否定句:主语+can't+动词的原形+其他。3. 变一般疑问句时,把can提前:Can+主语+动词原形+其他? 肯定回答:Yes,主语+can。否定回答:No,主语+can't...._七年级下册计算机知识点
文章浏览阅读3k次。在平时开发中,可能会遇到UFUN函数没有的功能,比如创建PTP的加工程序(我目前没找到,哪位大神可以指点一下),可以使用Grip创建PTP,然后用UFUN函数UF_call_grip调用Grip程序。具体如下截图(左侧UFUN,右侧Grip程序):..._uf调用grip
文章浏览阅读156次。第一个:原生普通样式(随着主题不同,样式会变)第二个:原生普通样式-小icon第三个:自定义RatingBar 颜色第四个:自定义RatingBar DrawableRatingBar 各样式实现===============原生样式原生样式其实没什么好说的,使用系统提供的style 即可<RatingBarstyle="?android:attr/ratingBarStyleIndicator"android:layout_width=“wrap_cont.._ratingbar样式修改
文章浏览阅读4.6k次,点赞6次,收藏11次。安装vs2017:参考vs2017下载和安装。安装cmake3.12.3:cmake是一个工程文件生成工具。用户可以使用预定义好的cmake脚本,根据自己的选择(像是Visual Studio, Code::Blocks, Eclipse)生成不同IDE的工程文件。可以从它官方网站的下载页上获取。这里我选择的是Win32安装程序,如图所示:然后就是运行安装程序进行安装就行。配置glfw3...._vs2017的opengl环境搭建(完整篇)
文章浏览阅读976次。MLC NAND,UBIFS_ubifs warning
文章浏览阅读2.2k次。计算机系统的两种存储器形式介绍时间:2016-1-6计算机系统的存储器一般应包括两个部分;一个是包含在计算机主机中的主存储器,简称内存,它直接和运算器,控制器及输入输出设备联系,容量小,但存取速度快,一般只存放那些急需要处理的数据或正在运行的程序;另一个是包含在外设中的外存储器,简称外存,它间接和运算器,控制器联系,存取速度虽然慢,但存储容量大,是用来存放大量暂时还不用的数据和程序,一旦要用时,就..._计算机存储器系统采用的是主辅结构,主存速度快、容量相对较小,用于 1 分 程序,外
文章浏览阅读5.6k次。1. STEP 7(Simatic Manager):STEP 7或者Simatic Manager是西门子PLC编程最常用的软件开发环境。4. STEP 7 MicroWin:STEP 7 MicroWn是一款专门针对微型PLC(S7-200系列PLC)的编程软件,是Simatic Manager的简化版。如果需要与PLC系统配合使用,则需要与PLC编程工具进行配合使用。除了上述软件之外,西门子还提供了一些配套软件和工具,如PLC模拟器、硬件调试工具等,以帮助PLC编程人员快速地进行调试和测试。_西门子plc编程软件
文章浏览阅读36次。【代码】HashMap扩容。_hashma扩容
文章浏览阅读2.9k次。1mvn dependency:copy-dependencies2 项目右键 -> Maven -> Disable Maven Nature3 项目右键 -> Configure -> Convert to Maven Project_maven资源加载不全,怎么重新加载
文章浏览阅读527次。DMLDML的全称是Database management Language,数据库管理语言。主要包括以下操作:insert、delete、update、optimize。本篇对其逐一介绍INSERT数据库表插入数据的方式:1、insert的完整语法:(做项目的过程中将字段名全写上,这样比较容易看懂)单条记录插入语法:insert into table_name (column_name1,......_dml的全称是
文章浏览阅读136次。可以参考: http://git.oschina.net/jrain-group/ 组织下的Java Modbus支持库Modbus-系列文章1、虚拟成对串口(1)下载虚拟串口软件VSPD(可在百度中搜索)image.png(2)打开软件,添加虚拟串口。在设备管理中,看到如下表示添加成功。..._最好用的 modebus调试工具