怎么实现android实现计算器计算器功能

Android实现简单计算器功能(Button控件实现)
Android实现简单计算器功能(Button控件实现)
通过安卓编程实现一个简易计算器的功能,用的都是按钮Button控件和显示结果的EditText控件,下面是效果图:
输入第一个数:
输入第二个数:
这里只是简单的演示了两个数相乘,实际可以实现加减乘除功能。
布局文件:
/MyCalculator/res/layout/activity_main.xml
&LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" &
android:id="@+id/et_show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="4"
android:gravity="right"
android:text="" /&
&LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="12"
android:orientation="vertical" &
&LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" &
android:id="@+id/btn_7"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/bg_key_selector"
android:text="7" /&
android:id="@+id/btn_8"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/bg_key_selector"
android:text="8" /&
android:id="@+id/btn_9"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/bg_key_selector"
android:text="9" /&
android:id="@+id/btn_chuyi"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/bg_key_selector"
android:text="/" /&
&/LinearLayout&
&LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" &
android:id="@+id/btn_4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/bg_key_selector"
android:text="4" /&
android:id="@+id/btn_5"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/bg_key_selector"
android:text="5" /&
android:id="@+id/btn_6"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/bg_key_selector"
android:text="6" /&
android:id="@+id/btn_chengyi"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/bg_key_selector"
android:text="*" /&
&/LinearLayout&
&LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" &
android:id="@+id/btn_1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/bg_key_selector"
android:text="1" /&
android:id="@+id/btn_2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/bg_key_selector"
android:text="2" /&
android:id="@+id/btn_3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/bg_key_selector"
android:text="3" /&
android:id="@+id/btn_jian"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/bg_key_selector"
android:text="-" /&
&/LinearLayout&
&LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" &
android:id="@+id/btn_0"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/bg_key_selector"
android:text="0" /&
android:id="@+id/btn_dian"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/bg_key_selector"
android:text="." /&
android:id="@+id/btn_jia"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/bg_key_selector"
android:text="+" /&
android:id="@+id/btn_dengyu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/bg_key_selector"
android:text="=" /&
&/LinearLayout&
&/LinearLayout&
android:id="@+id/btn_clear"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="4"
android:background="@drawable/bg_key_selector"
android:text="clear" /&
&/LinearLayout&
按钮背景文件:
/MyCalculator/res/drawable/bg_key_selector.xml
&?xml version="1.0" encoding="UTF-8"?&
&selector xmlns:android="http://schemas.android.com/apk/res/android"&
&item android:state_pressed="true" android:drawable="@drawable/bg_key_pressed"/&
&item android:state_pressed="false" android:drawable="@drawable/bg_key_normal"/&
&/selector&主Activity文件:
/MyCalculator/src/com/example/mycalculator/MainActivity.java
package com.example.
import android.app.A
import android.app.ActionB
import android.app.F
import android.os.B
import android.util.L
import android.view.LayoutI
import android.view.M
import android.view.MenuI
import android.view.V
import android.view.View.OnClickL
import android.view.ViewG
import android.widget.B
import android.widget.EditT
import android.os.B
public class MainActivity extends Activity implements OnClickListener {
private Button btn_1, btn_2, btn_3, btn_4, btn_5, btn_6, btn_7, btn_8,
btn_9, btn_0, btn_jia, btn_jian, btn_dengyu, btn_clear, btn_chuyi,
btn_chengyi, btn_ //各个按钮
private EditText et_ //显示输入数字和结果
public int flag = 0;
//为判断是加减乘除四种运算中的哪一种设定的标志位
private String text1 = "0",
//获得输入的第一个数
text2 = "0";
//获得输入的第二个数
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("计算器");
//获得各个控件
et_show = (EditText) findViewById(R.id.et_show);
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_jia = (Button) findViewById(R.id.btn_jia);
btn_jian = (Button) findViewById(R.id.btn_jian);
btn_chengyi = (Button) findViewById(R.id.btn_chengyi);
btn_chuyi = (Button) findViewById(R.id.btn_chuyi);
btn_dengyu = (Button) findViewById(R.id.btn_dengyu);
btn_clear = (Button) findViewById(R.id.btn_clear);
btn_dian = (Button) findViewById(R.id.btn_dian);
//设置监听器
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_clear.setOnClickListener(this);
btn_jia.setOnClickListener(this);
btn_jian.setOnClickListener(this);
btn_chengyi.setOnClickListener(this);
btn_chuyi.setOnClickListener(this);
btn_dian.setOnClickListener(this);
btn_dengyu.setOnClickListener(this);
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.btn_0:
et_show.append("0");
case R.id.btn_1:
et_show.append("1");
case R.id.btn_2:
et_show.append("2");
case R.id.btn_3:
et_show.append("3");
case R.id.btn_4:
et_show.append("4");
case R.id.btn_5:
et_show.append("5");
case R.id.btn_6:
et_show.append("6");
case R.id.btn_7:
et_show.append("7");
case R.id.btn_8:
et_show.append("8");
case R.id.btn_9:
et_show.append("9");
case R.id.btn_dian:
et_show.append(".");
case R.id.btn_jia:
text1 = et_show.getText().toString();
et_show.setText("");
case R.id.btn_jian:
text1 = et_show.getText().toString();
et_show.setText("");
case R.id.btn_chengyi:
text1 = et_show.getText().toString();
et_show.setText("");
case R.id.btn_chuyi:
text1 = et_show.getText().toString();
et_show.setText("");
case R.id.btn_dengyu:
switch (flag) {
// et_show.setText(text1);
et_show.append("0");
Log.d("text1", text1);
Log.d("text2", text2);
text2 = et_show.getText().toString();
Double res = Double.parseDouble(text1)
+ Double.parseDouble(text2);
// et_show.setText("flag=1");
CharSequence r = res.toString();
et_show.setText(res + "");
Log.d("text1", text1);
Log.d("text2", text2);
Log.d("res", res.toString());
text2 = et_show.getText().toString();
Double res2 = (Double.parseDouble(text1) - Double
.parseDouble(text2));
CharSequence r2 = res2.toString();
et_show.setText(res2 + "");
Log.d("text1", text1);
Log.d("text2", text2);
Log.d("res", res2.toString());
// et_show.setText("flag=2");
text2 = et_show.getText().toString();
Double res3 = Double.parseDouble(text1)
* Double.parseDouble(text2);
// et_show.setText("flag=3");
CharSequence r3 = res3.toString();
et_show.setText(res3 + "");
Log.d("text1", text1);
Log.d("text2", text2);
Log.d("res", res3.toString());
text2 = et_show.getText().toString();
Double res4 = Double.parseDouble(text1)
/ Double.parseDouble(text2);
// et_show.setText("flag=4");
CharSequence r4 = res4.toString();
et_show.setText(res4 + "");
Log.d("text1", text1);
Log.d("text2", text2);
Log.d("res", res4.toString());
case R.id.btn_clear:
text1 = "0";
text2 = "0";
et_show.setText("");
如果有兴趣研究下源码,请戳这里下载:。
没有更多推荐了,
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!基于Android计算器功能的实现+源代码
利用Android开发工具Eclipse,Java汇编语言,实现了安装时的滑屏效果;加、减、乘、除的基本运算,更换主题颜色。按钮背景使用的是点九图像技术
摘要:为了满足广大用户的需求,本次设计的主要采用的是面向对象的编程设计方法。在开发过程中利用Android开发工具Eclipse,Java汇编语言,实现了安装时的滑屏效果;加、减、乘、除的基本运算,更换主题颜色。按钮背景使用的是点九图像技术。使用时,用户可以根据自己的喜好选择不同的主题风格。该软件还具有保存一些简单数据的功能;同时还有反馈信息发送短信的功能,以便及时与我取得联系对我的产品进行反馈;也可以了解更多计算器的发展史。从而开发了一款适合大众的Android计算器软件。21561
&关键词:计算器;Android;技术;功能;汇编语言
Android-based Calculator Function to Achieve
Abstract: In order to meet the needs of users, the software is designed primarily uses object-oriented programming design. Use in the development process of the Android development tools eclipse, java assembly language to achieve a sliding screen ef addition, subtraction, multiplication, and pision of basic operations, change the theme color. Button background using Android Nine Technology. When used, the user can select a different theme to your liking. The software also has the function to s as well as the feedback function to send text messages to get in touch with me for also can learn more history of the calculator. Thus developed a calculator for the public's Android software.
Key Words: C A Communication T F Assembly Language.
摘& 要&&& 1
1.开发背景和意义&&& 2 源自L六^维:论`文~网-.加7位QQ www.lwfree.cn
1.1开发背景&&& 2
1.2研究意义&&& 2
2.基于Android平台计算器设计的相关知识&&& 3
2.1需求分析&&& 3
2.2模块设计&&& 3
3.基于Android平台计算器开发的过程&&& 5
3.1开发环境和开发要求&&& 5
3.2 Android用户滑屏效果&&& 6
3.3 Android计算的实现&&& 6
3.4 Android主题更换&&& 7
3.5 Android在线反馈&&& 8
3.6 Android了解更多&&& 8
4.软件测试&&& 9
4.1模拟器概念&&& 9
4.2真机模拟&&& 9
5.软件打包&&& 10
6.软件评价&&& 10
7.总结&&& 11
参考&&& 11
致谢&&& 13
基于Android计算器功能的实现&& 引言
随着通信技术的迅猛发展,手机换代的频率越来越高,其功能也变得多元化。这就意味着3G时代又上了一个台阶,不得不说这是人类创造的杰作。为了推动我国3G手机的发展,我们不断地去开发软件以满足用户的需求[1]。不管是从事经商、务农还是干部身边有一款实用的计算器软件是非常必要的,我们蓦然回首从古至今由一种叫筹策的计算工具到今天的软件计算器,经过了漫长的发展,凝聚了无数先人的智慧,使我们生活变得丰富多彩。计算器方便了我们的生活,受到各行各业的青睐。
1.开发背景和研究意义
1.1开发背景
&如今,已经进入3G时代了,移动互联网不断深入发展,Android操作系统2008年才上市,到目前为止Android操作系统用户是最多的。最早,Android是由Andy Rubin 创立的手机操作系统,后来被Google收购,基于Android操作系统的手机市场逐渐发展壮大,Google希望与各方共同建立一个标准化、开放式的移动电话软件平台。也看到了系统的进步和新的机型的快速的发布。所有的这一切都让我们相信,Android走向世界第一移动操作系统的宝座的步伐是势不可挡的,从而在移动产业内占主导地位。Android企业的实现目标是&随时随地为每个人提供信息&,以满足广大用户的需求[3]。最上层是各种应用软件,包括通话程序,短信程序等,应用软件则由各公司自行开发,以Java作为编写程序的一部分。不存在任何以往阻碍移动产业创新的专有权障碍,号称是首个为移动终端打造的真正开放和完整的移动软件。所以可以把Android看作是一个类似于Windows的操作系统。 基于Android计算器功能的实现+源代码:
------分隔线----------------------------
基于Android平台的音乐播放器无疑极具实用价值。本论文中的音乐...
图书馆管理系统是典型的管理信息系统(MIS),其开发包括了后...
摘要:云计算服务是云计算中重要的产业模式之一,随着云服务...
个人音乐网站使用ASP.NET技术和SQL Server 2005数据库技术,实现了用...
实现了界面布局模块,游戏功能模块,数据库记分模块,在游戏...
Android系统的特征,系统架构及应用程序构建、开发都做了比较详...博客分类:
刚接触android开发三天,看了一些视频,然后就突发奇想的看看可有啥简单的练笔程序,网上找了个关于计算器实现的demo,因此花了几个小时时间实现了该计算器程序,现将整个过程记载下来,也算是学习android途中的一点体会吧!
1、关于页面设计部分是看着网上的图片然后根据所学知识及网络上解决思路(当然也参考了下别人的设计内容啦)设计出了如附件中的界面【具体见附件的计算器的实现.png】
起主要代码设计如下【main.xml】:
&TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="32sp"
android:stretchColumns="1"
tools:context="${relativePackage}.${activityClass}" &
&TableRow&
android:id="@+id/result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
android:layout_span="4"
android:textSize="48sp"
android:gravity="right|center_vertical"
android:cursorVisible="false"
android:inputType="none"
android:lines="1" /&
&/TableRow&
&TableRow&
&LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
&!--android:textSize表示的是该button内数字的的大小--&
android:id="@+id/num7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="32sp"
android:text="@string/num_7"
android:layout_weight="1" /&
android:id="@+id/num8"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="32sp"
android:text="@string/num_8"
android:layout_weight="1" /&
android:id="@+id/num9"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="32sp"
android:text="@string/num_9"
android:layout_weight="1" /&
android:id="@+id/divide"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="32sp"
android:text="@string/divide"
android:layout_weight="1" /&
&/LinearLayout&
&/TableRow&
&TableRow&
&LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"&
android:id="@+id/num4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="32sp"
android:text="@string/num_4"
android:layout_weight="1" /&
android:id="@+id/num5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="32sp"
android:text="@string/num_5"
android:layout_weight="1" /&
android:id="@+id/num6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="32sp"
android:text="@string/num_6"
android:layout_weight="1" /&
android:id="@+id/multiply"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="32sp"
android:text="@string/multiply"
android:layout_weight="1" /&
&/LinearLayout&
&/TableRow&
&TableRow&
&LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"&
android:id="@+id/num1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="32sp"
android:text="@string/num_1"
android:layout_weight="1" /&
android:id="@+id/num2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="32sp"
android:text="@string/num_2"
android:layout_weight="1" /&
android:id="@+id/num3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="32sp"
android:text="@string/num_3"
android:layout_weight="1" /&
android:id="@+id/subtract"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="32sp"
android:text="@string/subtract"
android:layout_weight="1" /&
&/LinearLayout&
&/TableRow&
&TableRow&
&LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"&
android:id="@+id/num0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="32sp"
android:text="@string/num_0"
android:layout_weight="1" /&
android:id="@+id/point"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="32sp"
android:text="@string/point"
android:layout_weight="1" /&
android:id="@+id/add"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="32sp"
android:text="@string/add"
android:layout_weight="1" /&
android:id="@+id/equal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="32sp"
android:text="@string/equal"
android:layout_weight="1" /&
&/LinearLayout&
&/TableRow&
&TableRow&
android:id="@+id/clear"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="@string/clear"
android:layout_span="4"
android:gravity="center_vertical|center_horizontal"/&
&/TableRow&
&/TableLayout&
2、实现了上面的那么多,计算器的实现.png图片基本搞定了,那么接下来就是要编写mainActivity.java的内容了,直接贴上源码
public class MainActivity extends Activity {
private StringBuffer btnsBuffer = new StringBuffer("");
private double num1 = -0.030303;
private double num2 = -0.030303;
private String operateS
Button[] btnArray = new Button[17];
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText resultText = (EditText) findViewById(R.id.result);
btnArray[0] = (Button) findViewById(R.id.num0);
btnArray[1] = (Button) findViewById(R.id.num1);
btnArray[2] = (Button) findViewById(R.id.num2);
btnArray[3] = (Button) findViewById(R.id.num3);
btnArray[4] = (Button) findViewById(R.id.num4);
btnArray[5] = (Button) findViewById(R.id.num5);
btnArray[6] = (Button) findViewById(R.id.num6);
btnArray[7] = (Button) findViewById(R.id.num7);
btnArray[8] = (Button) findViewById(R.id.num8);
btnArray[9] = (Button) findViewById(R.id.num9);
btnArray[10] = (Button) findViewById(R.id.point);
btnArray[11] = (Button) findViewById(R.id.add);
btnArray[12] = (Button) findViewById(R.id.subtract);
btnArray[13] = (Button) findViewById(R.id.multiply);
btnArray[14] = (Button) findViewById(R.id.divide);
btnArray[15] = (Button) findViewById(R.id.equal);
btnArray[16] = (Button) findViewById(R.id.clear);
OnClickListener listener = new OnClickListener() {
public void onClick(View v) {
Button button = (Button)
String btnString = button.getText().toString();
// 如果输入的是 +-*/ 那么就存到operateString中
if (("+").equals(btnString) || ("-").equals(btnString)
|| ("*").equals(btnString) || ("/").equals(btnString)) {
// 如果buffer中内容不为空,那么就将值存入num1中,作为计算的第一个数
if (null != btnsBuffer && btnsBuffer.length() & 0)
num1 = Double.parseDouble(btnsBuffer.toString());
// 如果num1为默认值的话,说明这是非正常输入【不输入数字,直接输入符号情况】
if (num1 != -0.030303)
operateString = btnS
btnsBuffer.delete(0, btnsBuffer.length());
} else if (("=").equals(btnString)) {
if (null != btnsBuffer && !("").equals(btnString))
num2 = Double.parseDouble(btnsBuffer.toString());
} else if (("clear").equals(btnString)) {
num1 = -0.030303;
num2 = -0.030303;
operateString =
btnsBuffer.delete(0, btnsBuffer.length());
resultText.setText(null);
btnsBuffer.append(btnString);
resultText.setText(btnsBuffer);
if (-0.030303 != num1
&& -0.030303 != num2
&& ((null != operateString) && (!""
.equals(operateString)))) {
Double result = 0.0d;
if ("+".equals(operateString)) {
result = num1 + num2;
} else if ("-".equals(operateString)) {
result = num1 - num2;
} else if ("*".equals(operateString)) {
result = num1 * num2;
} else if ("/".equals(operateString)) {
result = num1 / num2;
num2 = -0.030303;
operateString =
btnsBuffer.delete(0, btnsBuffer.length());
resultText.setText(result.toString());
for (Button button : btnArray) {
button.setOnClickListener(listener);
3、至此,一个简单的计算器程序就完成了,当然这个系统还是存在个小bug的,比如:如果数值过大,在显示框内内容无法很好的显示;当你输入的值或者计算后的值恰好等于-0.030303的时候得先clear一下才能继续使用,因为思想局限性,用这类思想暂未想到好的解决思路,后面随着学习深入的话会回过头来做些优化的(估计得更换实现思路)
4、或许其中还会有隐藏着其他bug暂未查出,如果您发现了,也欢迎指正;
浏览: 2771 次
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'

我要回帖

更多关于 android定位功能实现 的文章

 

随机推荐