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设计技巧,重点注意合理使用布局 实 ...
随机推荐
- Maven学习之(四)Maven插件创建web项目
另一种maven web项目的创建. 创建出来的目录是这样的,此时试一下,不能加入到tomcat中去启动. 这里要将项目转化为web项目. 右键->项目 选中下面的动态web项目,然后OK 此时 ...
- 【WPF】TabControl禁用部分选项卡
需求:使用TabControl分页栏控件时,由于部分分页下的内容尚未开发完成,不希望用户能够点击切换到那些分页. 百度搜到的一些做法比较麻烦,或者说是直接把分页移除的,这些都不符合需求.需求要的是能看 ...
- Win10技巧:如何确定电脑是否适用Hyper-V虚拟机?
既然微软想要为Hyper-V的普及铺路,那么各种套路……配套措施当然也会一并跟上.比如想要看出电脑是否符合Hyper-V配置要求,有至少两种方式可以参考. 方法一:系统信息 这方法很简单,在Corta ...
- R语言绘制沈阳地铁线路图
##使用leaflet绘制地铁线路图,要求 ##(1)图中绘制地铁线路 library(dplyr) library(leaflet) library(data.table) stations< ...
- laravel 5.1 性能优化对比 - 框架提供的方法
写了一个项目发现性能不如人意. 于是便测试下, 看下性能瓶颈在什么地方. 使用 ab -n 20 http://www.lartest.com/ 软件环境: OS : windows 8.1 CPU: ...
- 判断asp.net中session过期的方法
判断asp.net中session过期的方法 转载自:http://www.cnblogs.com/xilipu31/archive/2013/04/12/3016830.html 方法一:最麻烦也是 ...
- Jquery仿IGoogle实现可拖动窗口(源码)
google可谓是ajax的特效用的淋漓尽致,google suggest, google map,igoogle 可拖动窗口等等...今天仿照iGoogle做了一个简单的小demo. 这个的demo ...
- [oracle] 安装卸载及常见问题
(1)安装oracle10g备注: ① 检查安装版本是否复合安装主机的硬件要求,避免版本不兼容.如64位的oracle就不能在x86的机器上运行安装. ② 检查安装主机是否满足oracle的硬件要求, ...
- C语言 格式化输出--%m.n
格式字符:格式字符用以指定输出项的数据类型和输出格式. ①d格式:用来输出十进制整数(int).有以下几种用法: %d:按整型数据的实际长度输出. %m.nd:m为指定的输出字段的宽度,n定义为实际输 ...
- 数据库 proc编程八
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <stri ...