Android 计算器
首先在activity_main.xml加入一个EditText
通过xml的方式来沈成一个图像
在drawable中新建一个white_bg.xml文件,同时选择一个shape标签
corners设置圆角
<corners android:radius="5dp"/>
gradient设置颜色渐变
<gradient
android:startColor="#FFFFFF"
android:endColor="#00FFFF"
/>
stroke设置边框的宽度和颜色
<stroke
android:color="#FFFFFF"
android:width="1dp"
/>
将white_bg.xml部署到activity_main.xml的EditText的android:background属性中
android:background="@drawable/white_bg"
但是我们现在要做的计算器的EditText有特定的要求,所以注释掉white_bg.xml里面的gradient和stroke属性,添加solid属性。
<solid android:color="#FFFFFF"/>
需要对当前这个activity更改一下背景颜色为黑色并且没有标题栏,所以需要修改一下AndroidManifest.xml中activity的android:theme。
android:theme="@android:style/Theme.Black.NoTitleBar"
这里遇到了一个问题,就是在AndroidManifest.xml中修改了activity的android:theme属性后调试的时候程序出现闪退的情况,原来是因为我的MainActivity类继承自ActionBarActivity,将MainActivity类改为继承自FragmentActivity类即可解决这个问题。
但是EditText中的内容是不可以编辑的,并且文字需要显示在右下方,所以需要设置EditText的editable和gravity属性
android:editable="false"
android:gravity="right|bottom"
然后就是繁琐的进行Button的布局的管理,因为这里用的是LinearLayout,所以要特别注意layout_weight的使用
android:layout_weight的真实含义是:一旦View设置了该属性(假设有效的情况下),那么该 View的宽度等于原有宽度(android:layout_width)加上剩余空间的占比!
Google官方推荐,当使用weight属性时,将width设为0dip即可,效果跟设成wrap_content是一样的。这样weight就可以理解为占比了。
修改按钮样式
修改按钮默认颜色白色,点击后颜色红色。
修改“=”键默认颜色绿色,点击后颜色蓝色。
在drawable/目录下新建集中不同颜色对应的xml文件(并在创建时选择shape)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:radius="5dp"/>
<solid android:color="#FFFFFF"/>
</shape>
white_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:radius="5dp"/>
<solid android:color="#FFFF00"/>
</shape>
yellow_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:radius="5dp"/>
<solid android:color="#00FF00"/> </shape>
green_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:radius="5dp"/>
<solid android:color="#0000FF"/> </shape>
blue_bg.xml
在drawable/目录下再创建两个样式xml文件(并在创建时选择selector),一个样式要求在没有按下时显示白色,按下时显示绿色。
<item android:drawable="@drawable/white_bg" android:state_pressed="false" />
<item android:drawable="@drawable/green_bg" android:state_pressed="true" />
另一个样式要求在没有按下时显示黄色,按下时显示蓝色。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/white_bg" android:state_pressed="false" />
<item android:drawable="@drawable/green_bg" android:state_pressed="true" /> </selector>
white_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/yellow_bg" android:state_pressed="false" />
<item android:drawable="@drawable/blue_bg" android:state_pressed="true" />
</selector>
yellow_selector.xml
最后给每一个button添加“android:background="@drawable/white_selector"”实现样式的转换。
因为文字太靠边了,需设置一下内边距:android:paddingRight和android:padding:bottom。
实例化对象
button_0 = (Button) findViewById(R.id.btn_0);
button_1 = (Button) findViewById(R.id.btn_1);
button_2 = (Button) findViewById(R.id.btn_2);
button_3 = (Button) findViewById(R.id.btn_3);
button_4 = (Button) findViewById(R.id.btn_4);
button_5 = (Button) findViewById(R.id.btn_5);
button_6 = (Button) findViewById(R.id.btn_6);
button_7 = (Button) findViewById(R.id.btn_7);
button_8 = (Button) findViewById(R.id.btn_8);
button_9 = (Button) findViewById(R.id.btn_9);
button_divide = (Button) findViewById(R.id.btn_divide);
button_multiply = (Button) findViewById(R.id.btn_multiply);
button_minus = (Button) findViewById(R.id.btn_minus);
button_plus = (Button) findViewById(R.id.btn_plus);
button_clear = (Button) findViewById(R.id.btn_clear);
button_delete = (Button) findViewById(R.id.btn_delete);
button_dot = (Button) findViewById(R.id.btn_dot);
button_equals = (Button) findViewById(R.id.btn_equals);
然后就是业务逻辑的实现
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
String s = editText.getText().toString();
if (view == button_clear) {
s = "";
} else if (view == button_delete) {
s = s.length() == 0 ? s : s.substring(0, s.length()-1);
} else if (view == button_equals) {
int index = -1;
for (String ss : new String[] { "x", "/", "+", "-" }) {
int tmpIndex = s.indexOf(ss);
if (tmpIndex != -1) {
index = tmpIndex;
break;
}
}
if (index != -1) {
String outputFormat = "%.2f";
try {
double x = Double.parseDouble(s.substring(0, index));
double y = Double.parseDouble(s.substring(index+1));
switch (s.charAt(index)) {
case 'x':
s = String.format(outputFormat, x * y);
break;
case '/':
s = String.format(outputFormat, x / y);
break;
case '+':
s = String.format(outputFormat, x + y);
break;
case '-':
s = String.format(outputFormat, x - y);
break;
default: break;
}
} catch (Exception e) {
s = "我只会算两个数的运算,这题我不会";
}
}
} else {
s += ((Button) view).getText() + "";
}
editText.setText(s);
}
package com.example.calculator; import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText; public class MainActivity extends FragmentActivity implements OnClickListener { private Button button_0;
private Button button_1;
private Button button_2;
private Button button_3;
private Button button_4;
private Button button_5;
private Button button_6;
private Button button_7;
private Button button_8;
private Button button_9;
private Button button_clear;
private Button button_delete;
private Button button_divide;
private Button button_multiply;
private Button button_minus;
private Button button_plus;
private Button button_dot;
private Button button_equals;
private EditText editText; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); button_0 = (Button) findViewById(R.id.btn_0);
button_1 = (Button) findViewById(R.id.btn_1);
button_2 = (Button) findViewById(R.id.btn_2);
button_3 = (Button) findViewById(R.id.btn_3);
button_4 = (Button) findViewById(R.id.btn_4);
button_5 = (Button) findViewById(R.id.btn_5);
button_6 = (Button) findViewById(R.id.btn_6);
button_7 = (Button) findViewById(R.id.btn_7);
button_8 = (Button) findViewById(R.id.btn_8);
button_9 = (Button) findViewById(R.id.btn_9);
button_divide = (Button) findViewById(R.id.btn_divide);
button_multiply = (Button) findViewById(R.id.btn_multiply);
button_minus = (Button) findViewById(R.id.btn_minus);
button_plus = (Button) findViewById(R.id.btn_plus);
button_clear = (Button) findViewById(R.id.btn_clear);
button_delete = (Button) findViewById(R.id.btn_delete);
button_dot = (Button) findViewById(R.id.btn_dot);
button_equals = (Button) findViewById(R.id.btn_equals);
editText = (EditText) findViewById(R.id.editText1); button_0.setOnClickListener(this);
button_1.setOnClickListener(this);
button_2.setOnClickListener(this);
button_3.setOnClickListener(this);
button_4.setOnClickListener(this);
button_5.setOnClickListener(this);
button_6.setOnClickListener(this);
button_7.setOnClickListener(this);
button_8.setOnClickListener(this);
button_9.setOnClickListener(this);
button_divide.setOnClickListener(this);
button_plus.setOnClickListener(this);
button_minus.setOnClickListener(this);
button_multiply.setOnClickListener(this);
button_clear.setOnClickListener(this);
button_delete.setOnClickListener(this);
button_dot.setOnClickListener(this);
button_equals.setOnClickListener(this);
} @Override
public void onClick(View view) {
// TODO Auto-generated method stub
String s = editText.getText().toString();
if (view == button_clear) {
s = "";
} else if (view == button_delete) {
s = s.length() == 0 ? s : s.substring(0, s.length()-1);
} else if (view == button_equals) {
int index = -1;
for (String ss : new String[] { "x", "/", "+", "-" }) {
int tmpIndex = s.indexOf(ss);
if (tmpIndex != -1) {
index = tmpIndex;
break;
}
}
if (index != -1) {
String outputFormat = "%.2f";
try {
double x = Double.parseDouble(s.substring(0, index));
double y = Double.parseDouble(s.substring(index+1));
switch (s.charAt(index)) {
case 'x':
s = String.format(outputFormat, x * y);
break;
case '/':
s = String.format(outputFormat, x / y);
break;
case '+':
s = String.format(outputFormat, x + y);
break;
case '-':
s = String.format(outputFormat, x - y);
break;
default: break;
}
} catch (Exception e) {
s = "我只会算两个数的运算,这题我不会";
}
}
} else {
s += ((Button) view).getText() + "";
}
editText.setText(s);
} }
MainActivity.java
<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:orientation="vertical"
> <EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@drawable/white_bg"
android:editable="false"
android:gravity="right|bottom"
android:textSize="40sp"
android:layout_margin="10dp"
>
</EditText> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:layout_gravity="center_horizontal"
> <Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="CLS"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_clear"
android:background="@drawable/white_selector"
android:paddingRight="10dp"
android:paddingBottom="10dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="DEL"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_delete"
android:background="@drawable/white_selector"
android:paddingRight="10dp"
android:paddingBottom="10dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="/"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_divide"
android:background="@drawable/white_selector"
android:paddingRight="10dp"
android:paddingBottom="10dp"
/> <Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="x"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_multiply"
android:background="@drawable/white_selector"
android:paddingRight="10dp"
android:paddingBottom="10dp"
/> </LinearLayout> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:layout_gravity="center_horizontal"
> <Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="7"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_7"
android:background="@drawable/white_selector"
android:paddingRight="10dp"
android:paddingBottom="10dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="8"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_8"
android:background="@drawable/white_selector"
android:paddingRight="10dp"
android:paddingBottom="10dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="9"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_9"
android:background="@drawable/white_selector"
android:paddingRight="10dp"
android:paddingBottom="10dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="-"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_minus"
android:background="@drawable/white_selector"
android:paddingRight="10dp"
android:paddingBottom="10dp"
/> </LinearLayout> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:layout_gravity="center_horizontal"
> <Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="4"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_4"
android:background="@drawable/white_selector"
android:paddingRight="10dp"
android:paddingBottom="10dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="5"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_5"
android:background="@drawable/white_selector"
android:paddingRight="10dp"
android:paddingBottom="10dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="6"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_6"
android:background="@drawable/white_selector"
android:paddingRight="10dp"
android:paddingBottom="10dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="+"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_plus"
android:background="@drawable/white_selector"
android:paddingRight="10dp"
android:paddingBottom="10dp"
/> </LinearLayout> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:layout_gravity="center_vertical"
android:orientation="horizontal"
> <LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="3"
android:orientation="vertical"
> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:orientation="horizontal"
> <Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="1"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_1"
android:background="@drawable/white_selector"
android:paddingRight="10dp"
android:paddingBottom="10dp"
/>
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="2"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_2"
android:background="@drawable/white_selector"
android:paddingRight="10dp"
android:paddingBottom="10dp"
/>
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="3"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_3"
android:background="@drawable/white_selector"
android:paddingRight="10dp"
android:paddingBottom="10dp"
/> </LinearLayout> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:orientation="horizontal"
> <Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="2"
android:text="0"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_0"
android:background="@drawable/white_selector"
android:paddingRight="10dp"
android:paddingBottom="10dp"
/>
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="."
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_dot"
android:background="@drawable/white_selector"
android:paddingRight="10dp"
android:paddingBottom="10dp"
/> </LinearLayout> </LinearLayout> <Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="="
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_equals"
android:background="@drawable/yellow_selector"
android:paddingRight="10dp"
android:paddingBottom="10dp"
/> </LinearLayout> </LinearLayout>
activity_main.xml
项目代码:https://github.com/moonlightpoet/Calculator
apk下载:https://raw.githubusercontent.com/moonlightpoet/Calculator/master/bin/resources.ap_
效果:
Android 计算器的更多相关文章
- Android计算器简单逻辑实现
Android计算器简单逻辑实现 引言: 我的android计算器的实现方式是:按钮输入一次,就处理一次. 但是如果你学过数据结构(栈),就可以使用表达式解析(前缀,后缀)处理. 而这个方式已经很成熟 ...
- Android计算器布局
Android(安桌)计算器布局实现 ——解决整个屏幕方案 引言: 学完了android布局的几种方式,做了一个android计算器. 我在网上搜索了这方面的资料,发现了布局都 ...
- Android计算器尝试
学了一段时间Android了,一直都是在看,没有什么尝试,刚好最近大致学会了gridview配合simpleadpter的使用,于是想着动手练习一下,就选择了写一个最简单的计算器来实现. 只包含+-* ...
- java实现可有括号的android计算器
写了一个android版的计算器,可以计算带括号的表达式,不过前提是:正确的表达式才行 小缺陷是没有做表达式括号的控制,现在还没有想到好的控制方式 package javaAdvanced; impo ...
- Android计算器开发实例
Android简单计算器开发实例如图: ==================================================== activity_main.xml 代码如下: < ...
- 结对实验报告-android计算器设计
一:引言 目前手机可以说是普及率非常高的电子设备了,由于其便于携带,使用方便,资费适中等等原因,现在手机已经在一定程度开始代替固定电话的通话功能,以及一些原来电脑软件上的功能了.手机上的软件也随着 ...
- Android——计算器
layout文件: <?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:andr ...
- android计算器
一:引言 目前手机可以说是普及率非常高的电子设备了,由于其便于携带,使用方便,资费适中等等原因,现在手机已经在一定程度开始代替固定电话的通话功能,以及一些原来电脑软件上的功能了.手机上的软件也随 ...
- 【Android实验】UI设计-Android计算器
目录 实验目的 实验要求 实验过程 1. 界面设计 2. 功能设计 3. 运算处理 实验目的 自主完成一个简单APP的设计工作,综合应用已经学到的Android UI设计技巧,重点注意合理使用布局 实 ...
随机推荐
- JavaScrip——练习(求整数和、求整数积)
用HTML和JSp来实现 1.HTML调用JSp语法:<script type="text/javascript" src="整数和jsp.js"> ...
- 一站式学习Wireshark(七):Statistics统计工具功能详解与应用
Wireshark一个强大的功能在于它的统计工具.使用Wireshark的时候,我们有各种类型的工具可供选择,从简单的如显示终端节点和会话到复杂的如Flow和IO图表.本文将介绍基本网络统计工具.包括 ...
- Maven_POM配置结构
本文转载,转载地址:http://blog.csdn.net/ithomer/article/details/9332071 <project> <parent> ...
- datanode启动失败
当我动态加入一个hadoop从节点的之后,出现了一个问题: [root@hadoop current]# hadoop-daemon.sh start datanode starting datano ...
- Jquery与.net MVC结合,通过Ajax
在工作中做了这么一个东西. Html端: @using Test.fh.Project.Storefront.ViewModels @using Test.fh.Project.Storefront. ...
- 15 +免费及收费的jQuery滚动插件
免费的 jQuery Scrolling 插件 Tiny Scrollbar SUPERSCROLLORAMA jScrollPane Curtain.js Plugin JQuery : Scrol ...
- socket和http
套接字(socket)是通信的基石,是支持TCP/IP协议的网络通信的基本操作单元.它是网络通信过程中端点的抽象表示,包含进行网络通信必须的五种信息:连接使用的协议,本地主机的IP地址,本地进程的协议 ...
- 【转】C# 调用WebService的方法
很少用C#动态的去调用Web Service,一般都是通过添加引用的方式,这样的话是自动成了代理,那么动态代理调用就是我们通过代码去调用这个WSDL,然后自己去生成客户端代理.更多的内容可以看下面的两 ...
- PHP 初学之登录查询小case
说明:如误入本文,请忽略即可,内容仅为记录. 功能:登录(不验证),查询所有列表,删除记录.--很简单,仅为熟悉代码. // MySQL,新建数据库data,导入如下sql ; -- -------- ...
- 第二百八十三节,MySQL数据库-MySQL存储过程
MySQL数据库-MySQL存储过程 MySQL存储过程,也就是有点像MySQL函数,但是他与MySQL函数是有区别的,后面会讲到函数,所以注意区分 注意:函数与存储过程的区别 存储过程是:CREAT ...