首先在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 计算器的更多相关文章

  1. Android计算器简单逻辑实现

    Android计算器简单逻辑实现 引言: 我的android计算器的实现方式是:按钮输入一次,就处理一次. 但是如果你学过数据结构(栈),就可以使用表达式解析(前缀,后缀)处理. 而这个方式已经很成熟 ...

  2. Android计算器布局

    Android(安桌)计算器布局实现         ——解决整个屏幕方案 引言:     学完了android布局的几种方式,做了一个android计算器. 我在网上搜索了这方面的资料,发现了布局都 ...

  3. Android计算器尝试

    学了一段时间Android了,一直都是在看,没有什么尝试,刚好最近大致学会了gridview配合simpleadpter的使用,于是想着动手练习一下,就选择了写一个最简单的计算器来实现. 只包含+-* ...

  4. java实现可有括号的android计算器

    写了一个android版的计算器,可以计算带括号的表达式,不过前提是:正确的表达式才行 小缺陷是没有做表达式括号的控制,现在还没有想到好的控制方式 package javaAdvanced; impo ...

  5. Android计算器开发实例

    Android简单计算器开发实例如图: ==================================================== activity_main.xml 代码如下: < ...

  6. 结对实验报告-android计算器设计

     一:引言  目前手机可以说是普及率非常高的电子设备了,由于其便于携带,使用方便,资费适中等等原因,现在手机已经在一定程度开始代替固定电话的通话功能,以及一些原来电脑软件上的功能了.手机上的软件也随着 ...

  7. Android——计算器

    layout文件: <?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:andr ...

  8. android计算器

    一:引言    目前手机可以说是普及率非常高的电子设备了,由于其便于携带,使用方便,资费适中等等原因,现在手机已经在一定程度开始代替固定电话的通话功能,以及一些原来电脑软件上的功能了.手机上的软件也随 ...

  9. 【Android实验】UI设计-Android计算器

    目录 实验目的 实验要求 实验过程 1. 界面设计 2. 功能设计 3. 运算处理 实验目的 自主完成一个简单APP的设计工作,综合应用已经学到的Android UI设计技巧,重点注意合理使用布局 实 ...

随机推荐

  1. Gtest创建第一个测试

    gtest测试框架是在不同平台上(Linux,Mac OS X,Windows,Cygwin,Windows CE和Symbian)为编写C++测试而生成的.它是基于xUnit架构的测试框架,支持自动 ...

  2. DHCP配置实例

    配置DHCP的思路: 1.创建dhcp服务2.添加一个网络号(或者说地址池)3.排除路由器的网管4.排除DHCP的网关 代码: Router>enableRouter#configRouter# ...

  3. Eclipse里安装插件

    1.在eclipse中选择 help->install new software. 2.在work with 框中输入:Indigo - http://download.eclipse.org/ ...

  4. JavaScrip——对话框的简单应用(判断isNaN)

    综合运用 isNaN的用法:判断是不是一个合法的数字类型,是数字返回false,不是返回true 1.我们用prompt来接收输入的内容 2.通过isNaN来做判断 3.最后用alert输出结果 &l ...

  5. protobuf--数据序列化及反序列化

    ProtoBuf是一种灵活高效的独立于语言平台的结构化数据表示方法,可用于表示通信协议和数据存储等各方面,与XML相比,ProtoBuF更小更快更简单.你可以用定义自己ProtoBuf的数据结构,用P ...

  6. 我的开源主页Blog Lite配置指南

    JinHengyu.github.io --- Blog Lite 0.1.1 好看的东西看多了就会不好看, 简单的东西永远不会难看 GitHub Pages 提供静态网站托管服务的厂商还是很多的, ...

  7. The Qt Resource System

    The Qt Resource System The Qt resource system is a platform-independent mechanism for storing binary ...

  8. archdexls主题游戏页面game-play.php有评论时,报错( ! ) Warning: printf(): Too few arguments in D:\wamp\www\wp-content\themes\arcadexls\games-play.php on line 97

    ( ! ) Warning: printf(): Too few arguments in D:\wamp\www\wp-content\themes\arcadexls\games-play.php ...

  9. 执行大数据量SQL文件

    sqlserver2008中需要执行大文件的脚本,查询分析器中打不开,需要用到sql命令,开始使用osql命令 使用sqlcmd可以执行:在DOS中,调用sqlcmd命令,并使用对应选项    sql ...

  10. Office 2013 标点符号自动变成calibri字体

    在字体设置中,已经设置西文字体为“(使用中文字体)”,结果office 2013还是自动将输入的英文符号自动变成calibri字体. 举例:输入以下一段话 好好学习,天天向上. 中文字体中后面跟着标点 ...