实现功能:

简单计算器

布局及美化

采用LinearLayout嵌套LinearLayout实现布局。

要求

1. 按钮所有文字居于右下角

2. 按钮为白色,点击变成橘色

3. 显示屏文字居右显示并且为橘色

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"> <!-- 显示屏组件 -->
<EditText
android:id="@+id/et_input"
android:layout_width="330dp"
android:layout_height="wrap_content"
android:editable="false"
android:gravity="right"
android:textColor="#ffbf16"/> <!-- 一个垂直线性布局,用来放置所有按钮 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:orientation="vertical"> <!-- 一个水平线性布局,用来放置C DEL ÷ × -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal"> <Button
android:id="@+id/btn_clear"
android:layout_width="75dp"
android:layout_height="75dp"
android:background="@drawable/white_btn_selector"
android:gravity="right|bottom"
android:text="C "
android:textSize="25sp" /> <Button
android:id="@+id/btn_del"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginLeft="10dp"
android:background="@drawable/white_btn_selector"
android:gravity="right|bottom"
android:text="DEL "
android:textSize="25sp" /> <Button
android:id="@+id/btn_divide"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginLeft="10dp"
android:background="@drawable/white_btn_selector"
android:gravity="right|bottom"
android:text="÷ "
android:textSize="25sp" /> <Button
android:id="@+id/btn_multiply"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginLeft="10dp"
android:background="@drawable/white_btn_selector"
android:gravity="right|bottom"
android:text="× "
android:textSize="25sp" /> </LinearLayout> <!-- 一个水平线性布局,用来放置7 8 9 - -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:orientation="horizontal"> <Button
android:id="@+id/btn_7"
android:layout_width="75dp"
android:layout_height="75dp"
android:background="@drawable/white_btn_selector"
android:gravity="right|bottom"
android:text="7 "
android:textSize="25sp" /> <Button
android:id="@+id/btn_8"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginLeft="10dp"
android:background="@drawable/white_btn_selector"
android:gravity="right|bottom"
android:text="8 "
android:textSize="25sp" /> <Button
android:id="@+id/btn_9"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginLeft="10dp"
android:background="@drawable/white_btn_selector"
android:gravity="right|bottom"
android:text="9 "
android:textSize="25sp" /> <Button
android:id="@+id/btn_minus"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginLeft="10dp"
android:background="@drawable/white_btn_selector"
android:gravity="right|bottom"
android:text="- "
android:textSize="25sp" />
</LinearLayout> <!-- 一个水平线性布局,用来放置4 5 6 7 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:orientation="horizontal"> <Button
android:id="@+id/btn_4"
android:layout_width="75dp"
android:layout_height="match_parent"
android:background="@drawable/white_btn_selector"
android:gravity="right|bottom"
android:text="4 "
android:textSize="25sp" /> <Button
android:id="@+id/btn_5"
android:layout_width="75dp"
android:layout_height="75dp" android:layout_marginLeft="10dp"
android:background="@drawable/white_btn_selector"
android:gravity="right|bottom"
android:text="5 "
android:textSize="25sp" /> <Button
android:id="@+id/btn_6"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginLeft="10dp"
android:background="@drawable/white_btn_selector"
android:gravity="right|bottom"
android:text="6 "
android:textSize="25sp" /> <Button
android:id="@+id/btn_sum"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginLeft="10dp"
android:background="@drawable/white_btn_selector"
android:gravity="right|bottom"
android:text="+ "
android:textSize="25sp" />
</LinearLayout> <!-- 一个水平布局,用来放置剩余按钮 -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"> <!-- 一个垂直布局,用来放置1 2 3 0 . 按钮 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <!-- 一个水平布局,用来放置1 2 3 按钮 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"> <Button
android:id="@+id/btn_1"
android:layout_width="75dp"
android:layout_height="75dp"
android:background="@drawable/white_btn_selector"
android:gravity="right|bottom"
android:text="1 "
android:textSize="25sp" /> <Button
android:id="@+id/btn_2"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginLeft="10dp"
android:background="@drawable/white_btn_selector"
android:gravity="right|bottom"
android:text="2 "
android:textSize="25sp" /> <Button
android:id="@+id/btn_3"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginLeft="10dp"
android:background="@drawable/white_btn_selector"
android:gravity="right|bottom"
android:text="3 "
android:textSize="25sp" />
</LinearLayout> <!-- 一个水平布局,用来放置0 . 按钮 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:orientation="horizontal"> <Button
android:id="@+id/btn_0"
android:layout_width="160dp"
android:layout_height="75dp"
android:background="@drawable/white_btn_selector"
android:gravity="right|bottom"
android:text="0 "
android:textSize="25sp" /> <Button
android:id="@+id/btn_point"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginLeft="10dp"
android:background="@drawable/white_btn_selector"
android:gravity="right|bottom"
android:text=". "
android:textSize="25sp" />
</LinearLayout>
</LinearLayout> <Button
android:id="@+id/btn_equal"
android:layout_width="75dp"
android:layout_height="160dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/white_btn_selector"
android:gravity="right|bottom"
android:text="= "
android:textSize="25sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>

white_btn_selector.xml

<?xml version="1.0" encoding="utf-8"?>

    <selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_selected="true" android:drawable="@drawable/orange"/>
<item android:state_focused="true" android:drawable="@drawable/orange" />
<item android:state_pressed="true" android:drawable="@drawable/orange" />
<item android:drawable="@drawable/white"/> </selector>

orange.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffbf20"/>
</shape>

white.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffff"/>
<corners android:radius="2.0dp" />
</shape>

运行结果:

逻辑实现

MainActivity.java

package cn.lixyz.counterdemo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText; public class MainActivity extends Activity implements View.OnClickListener { Button btn_0; //0数字按钮
Button btn_1; //1数字按钮
Button btn_2; //2数字按钮
Button btn_3; //3数字按钮
Button btn_4; //4数字按钮
Button btn_5; //5数字按钮
Button btn_6; //6数字按钮
Button btn_7; //7数字按钮
Button btn_8; //8数字按钮
Button btn_9; //9数字按钮
Button btn_point; //小数点按钮
Button btn_clear; //clear数字按钮
Button btn_del; //delete数字按钮
Button btn_sum; //加按钮
Button btn_minus; //减按钮
Button btn_multiply; //乘按钮
Button btn_divide; //除按钮
Button btn_equal; //等于按钮 String[] arr = new String[]{"+", "-", "×", "÷"}; EditText et_input;//显示屏 boolean clear_flag = true;//清空标识 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//实例化按钮
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_multiply = (Button) findViewById(R.id.btn_multiply);
btn_point = (Button) findViewById(R.id.btn_point);
btn_clear = (Button) findViewById(R.id.btn_clear);
btn_del = (Button) findViewById(R.id.btn_del);
btn_sum = (Button) findViewById(R.id.btn_sum);
btn_minus = (Button) findViewById(R.id.btn_minus);
btn_divide = (Button) findViewById(R.id.btn_divide);
btn_equal = (Button) findViewById(R.id.btn_equal); //实例化显示屏
et_input = (EditText) findViewById(R.id.et_input); //设置这些按钮的点击事件
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_multiply.setOnClickListener(this);
btn_point.setOnClickListener(this);
btn_clear.setOnClickListener(this);
btn_del.setOnClickListener(this);
btn_sum.setOnClickListener(this);
btn_minus.setOnClickListener(this);
btn_divide.setOnClickListener(this);
btn_equal.setOnClickListener(this); } @Override
public void onClick(View v) {
//获取目前显示屏上显示的数字,因为为了美观,在布局时,Text属性添加了空格,所以在获取到按钮内容之后,需要去掉空格
String str = et_input.getText().toString(); switch (v.getId()) {
//如果点击的是0~9或者小数点,直接累加到屏幕上
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:
case R.id.btn_sum:
case R.id.btn_minus:
case R.id.btn_divide:
case R.id.btn_multiply:
//因为传入的是一个View对象,所以需要强制转换为Button对象,才能使用getText()方法
if (clear_flag) {
et_input.setText(str + ((Button) v).getText());
} else {
et_input.setText(((Button) v).getText());
clear_flag = true;
}
Log.d("TEST", ((Button) v).getText().toString() + " " + clear_flag + "");
break;
//如果点击的是C,则将显示屏设置为空
case R.id.btn_clear:
et_input.setText("");
break;
//如果点击的是DEL,如果显示屏不为空,则将显示屏的内容最后一个字符去掉
case R.id.btn_del:
if (str != null && !str.equals("")) {
et_input.setText(et_input.getText().toString().substring(0, str.length() - 1));
}
break;
//如果点击的是等号,则进行运算,判断显示屏上的内容是否存在运算,如果不存在,直接显示当前文字,如果存在运算,进行运算,显示结果
case R.id.btn_equal:
for (int i = 0; i <= arr.length-1; i++) {
if (et_input.getText().toString().contains(arr[i])) {
getResult(et_input.getText().toString());
break;
}
}
// et_input.setText(str);
clear_flag = false;
break;
}
} //运算方法
public void getResult(String string) {
Log.d("TEST", string);
String[] arr2;
Double d1;
Double d2;
if (string.contains("+")) {
arr2 = string.split("\\+");
if (arr2[0].trim().length() == 0) {
d1 = 0.0;
} else {
d1 = Double.parseDouble(arr2[0].replace(" ", ""));
} if (arr2[1].trim().length() == 0) {
d2 = d1;
} else {
d2 = Double.parseDouble(arr2[1].replace(" ", ""));
}
et_input.setText((d1 + d2) + ""); } else if (string.contains("-")) { arr2 = string.split("\\-");
if (arr2[0].trim().length() == 0) {
d1 = 0.0;
} else {
d1 = Double.parseDouble(arr2[0].replace(" ", ""));
} if (arr2[1].trim().length() == 0) {
d2 = d1;
} else {
d2 = Double.parseDouble(arr2[1].replace(" ", ""));
}
et_input.setText((d1 - d2) + ""); } else if (string.contains("×")) {
arr2 = string.split("\\×");
if (arr2[0].trim().length() == 0) {
d1 = 0.0;
} else {
d1 = Double.parseDouble(arr2[0].replace(" ", ""));
} if (arr2[1].trim().length() == 0) {
d2 = d1;
} else {
d2 = Double.parseDouble(arr2[1].replace(" ", ""));
}
et_input.setText((d1 * d2) + "");
} else if (string.contains("÷")) {
arr2 = string.split("\\÷");
if (arr2[0].trim().length() == 0) {
d1 = 0.0;
} else {
d1 = Double.parseDouble(arr2[0].replace(" ", ""));
} if (arr2[1].trim().length() == 0) {
d2 = d1;
} else {
d2 = Double.parseDouble(arr2[1].replace(" ", ""));
}
if (d2 == 0) {
et_input.setText("除数不能为0");
} else {
et_input.setText((d1 / d2) + "");
}
}
}
}

Android笔记(十六) 简易计算器的更多相关文章

  1. python3.4学习笔记(十六) windows下面安装easy_install和pip教程

    python3.4学习笔记(十六) windows下面安装easy_install和pip教程 easy_install和pip都是用来下载安装Python一个公共资源库PyPI的相关资源包的 首先安 ...

  2. (C/C++学习笔记) 十六. 预处理

    十六. 预处理 ● 关键字typeof 作用: 为一个已有的数据类型起一个或多个别名(alias), 从而增加了代码的可读性. typedef known_type_name new_type_nam ...

  3. Android笔记(六十)Android总结:四大组件——BroadcastReceiver篇

    什么是BroadcastReceiver BroadcastReceiver是Android体系的四大组件之一,本质上是一种全局的监听器,用于监听系统全局的广播消息,正式因为其本质为全局监听,因此可以 ...

  4. Unity 游戏框架搭建 2019 (四十六) 简易消息机制 & 集成到 MonoBehaviourSimplify 里

    在上一篇,我们接触了单例,使用单例解决了我们脚本之间访问的问题. 脚本之间访问其实有更好的方式. 我们先分下脚本访问脚本的几种形式. 第一种,A GameObject 是 B GameObject 的 ...

  5. Android入门(十六)调用摄像头相册

    原文链接:http://www.orlion.ga/665/ 一.调用摄像头 创建一个项目ChoosePicDemo,修改activity_main.xml: <LinearLayout xml ...

  6. Android进阶(十六)子线程调用Toast报Can't create handler inside thread that has not called Looper.prepare() 错误

    原子线程调用Toast报Can't create handler inside thread that has not called Looper.prepare() 错误 今天用子线程调Toast报 ...

  7. SpringBoot笔记十六:ElasticSearch

    目录 ElasticSearch官方文档 ElasticSearch安装 ElasticSearch简介 ElasticSearch操作数据,RESTful风格 存储 检查是否存在 删除 查询 更新 ...

  8. Android笔记(六):线程及线程通信

    线程 由于Android的Activity中默认所有代码都在主线程(UI线程)中执行,如果在这里面执行耗时任务(例如下载),界面就会无反应且不可操作,直到耗时任务执行完毕. 如果想在执行耗时任务的同时 ...

  9. Android笔记(六十一)动态添加组件

    想要一个功能,点击按钮,可以在已有的布局上,新添加一组组件. 动态的创建组件,本质上还是创建组件,只不过是在程序中根据逻辑来创建.大致步骤是找到要创建控件的位置,然后将要创建的组件添加进去. 看代码: ...

  10. Android笔记(六) Android中的组件

    一个软件可以吸引到用户,除了优秀的功能可以解决用户的问题之外,良好的用户界面也并不可少.一个软件的功能不管多么优秀,但是没有提供友好的界面来让用户操作,将很难吸引到最终用户. Android提供了大量 ...

随机推荐

  1. 123457123456#5#----com.MC.HuaHuaGame866----前拼后广--babyDrawGame-mc555

    com.MC.HuaHuaGame866----前拼后广--babyDrawGame-mc

  2. redis八大应用场景

    1.缓存 缓存现在几乎是所有中大型网站都在用的必杀技,合理的利用缓存不仅能够提升网站访问速度,还能大大降低数据库的压力.Redis提供了键过期功能,也提供了灵活的键淘汰策略,所以,现在Redis用在缓 ...

  3. Linux查找含有特定字符串的文件

    Linux查找含有特定字符串的文件命令为grep.以下为详细的使用方法 grep [OPTIONS] PATTERN [FILE...] #实例:递归查找当前文件夹下所有含有test的文件,并显示行号 ...

  4. Linux入门-1

    操作系统 列举系统:Windows  Mac  Linux  ios  Android   操作系统的定义:操作系统是一个用来协调.管理和控制计算机硬件和软件资源的系统程序,它位于硬件和应用程序之间. ...

  5. android基础---->ContentProvider的使用

    内容提供器(Content Provider)主要用于在不同的应用程序之间实现数据共享的功能,它提供了一套完整的机制,允许一个程序访问另一个程序中的数据,同时还能保证被访数据的安全性.目前,使用内容提 ...

  6. oracle 存储过程详细介绍(创建,删除存储过程,参数传递等)

    这篇文章主要介绍了oracle 创建,删除存储过程,参数传递,创建,删除存储函数,存储过程和函数的查看,包,系统包等相关资料,需要的朋友可以参考下   oracle 创建,删除存储过程,参数传递,创建 ...

  7. 一文带你全面了解RxJava

    工作需要,刚好在学习 RxJava网络请求框架,网上搜了一些 关于RxJava 的教程,但都并不是很好理解,所幸最后找到了几篇有助于初学者了解 RxJava 的文章,于是结合自己的理解,重新整理成一篇 ...

  8. rsync同步本地和服务器之间的文件

    同步本地文件到服务器 rsync -zvrtopg --progress --delete test -e 'ssh -p 6665' yueyao@172.16.0.99:/media/sdb/us ...

  9. LeetCode 179. 最大数(Largest Number) 21

    179. 最大数 179. Largest Number 题目描述 给定一组非负整数,重新排列它们的顺序使之组成一个最大的整数. 每日一算法2019/5/24Day 21LeetCode179. La ...

  10. 025 Android 带进度条的对话框(ProgressDialog)

    1.ProgressDialog介绍 ProgressDialog可以在当前界面弹出一个置顶于所有界面元素的对话框,同样具有屏蔽其他控件的交互能力,用于提示用户当前操作正在运行,让用户等待: 2.应用 ...