android实现计算器功能
设计一个简单的计算器。
第一个Activity的界面。
第二个Activity显示算式和计算结果。
第一个Activity代码:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity { Button one,two,three,four,five,six,seven,eight,nine,zero,div,mod,mul,fenshu,sub,equal,point,add;
double num1,num2,num,xiaoshudian;
int flag,ispoint;
char operation[]=new char[2];
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num1=0;num2=0;num=0;flag=0;ispoint=0;xiaoshudian=0.1;
one=(Button)findViewById(R.id.one);
two=(Button)findViewById(R.id.two);
three=(Button)findViewById(R.id.three);
four=(Button)findViewById(R.id.four);
five=(Button)findViewById(R.id.five);
six=(Button)findViewById(R.id.six);
seven=(Button)findViewById(R.id.seven);
eight=(Button)findViewById(R.id.eight);
nine=(Button)findViewById(R.id.nine);
zero=(Button)findViewById(R.id.zero);
div=(Button)findViewById(R.id.div);
mod=(Button)findViewById(R.id.mod);
mul=(Button)findViewById(R.id.mul);
fenshu=(Button)findViewById(R.id.fenshu);
sub=(Button)findViewById(R.id.sub);
add=(Button)findViewById(R.id.add);
equal=(Button)findViewById(R.id.equal);
point=(Button)findViewById(R.id.point);
one.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if(flag==0){
if(ispoint==0)num1=num1*10+1;
else {num1=num1+1*xiaoshudian;xiaoshudian/=10;}
}
else{
if(ispoint==0)num2=num2*10+1;
else{num2=num2+1*xiaoshudian;xiaoshudian/=10;}
}
}
});
two.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if(flag==0){
if(ispoint==0)num1=num1*10+2;
else {num1=num1+2*xiaoshudian;xiaoshudian/=10;}
}
else{
if(ispoint==0)num2=num2*10+2;
else{num2=num2+2*xiaoshudian;xiaoshudian/=10;}
}
}
});
three.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if(flag==0){
if(ispoint==0)num1=num1*10+3;
else {num1=num1+3*xiaoshudian;xiaoshudian/=10;}
}
else{
if(ispoint==0)num2=num2*10+3;
else{num2=num2+3*xiaoshudian;xiaoshudian/=10;}
}
}
});
four.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if(flag==0){
if(ispoint==0)num1=num1*10+4;
else {num1=num1+4*xiaoshudian;xiaoshudian/=10;}
}
else{
if(ispoint==0)num2=num2*10+4;
else{num2=num2+4*xiaoshudian;xiaoshudian/=10;}
}
}
});
five.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if(flag==0){
if(ispoint==0)num1=num1*10+5;
else {num1=num1+5*xiaoshudian;xiaoshudian/=10;}
}
else{
if(ispoint==0)num2=num2*10+5;
else{num2=num2+5*xiaoshudian;xiaoshudian/=10;}
}
}
});
six.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if(flag==0){
if(ispoint==0)num1=num1*10+6;
else {num1=num1+6*xiaoshudian;xiaoshudian/=10;}
}
else{
if(ispoint==0)num2=num2*10+6;
else{num2=num2+6*xiaoshudian;xiaoshudian/=10;}
}
}
});
seven.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if(flag==0){
if(ispoint==0)num1=num1*10+7;
else {num1=num1+7*xiaoshudian;xiaoshudian/=10;}
}
else{
if(ispoint==0)num2=num2*10+7;
else{num2=num2+7*xiaoshudian;xiaoshudian/=10;}
}
}
});
eight.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if(flag==0){
if(ispoint==0)num1=num1*10+8;
else {num1=num1+8*xiaoshudian;xiaoshudian/=10;}
}
else{
if(ispoint==0)num2=num2*10+8;
else{num2=num2+8*xiaoshudian;xiaoshudian/=10;}
}
}
});
nine.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if(flag==0){
if(ispoint==0)num1=num1*10+9;
else {num1=num1+9*xiaoshudian;xiaoshudian/=10;}
}
else{
if(ispoint==0)num2=num2*10+9;
else{num2=num2+9*xiaoshudian;xiaoshudian/=10;}
}
}
});
zero.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if(flag==0){
if(ispoint==0)num1=num1*10;
else {xiaoshudian/=10;}
}
else{
if(ispoint==0)num2=num2*10;
else{xiaoshudian/=10;}
}
}
});
point.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
ispoint=1;
}
});
add.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
flag=1;
ispoint=0;
}
});
sub.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
flag=2;
ispoint=0;
}
});
mul.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
flag=3;
ispoint=0;
}
});
div.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
flag=4;
ispoint=0;
}
});
equal.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if(flag==1){num=num1+num2;operation[0]='+';}
else if(flag==2){num=num1-num2;operation[0]='-';}
else if(flag==3){num=num1*num2;operation[0]='*';}
else if(flag==4){num=num1/num2;operation[0]='/';}
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("num1", new Double(num1).toString());
intent.putExtra("opreation",new String(operation).toString());
intent.putExtra("num2", new Double(num2).toString());
intent.putExtra("num", new Double(num).toString());
startActivity(intent);
}
});
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
第二个Activity代码:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText; public class SecondActivity extends Activity{
EditText text;
Button myButton;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
text=(EditText)findViewById(R.id.edt);
myButton=(Button)findViewById(R.id.btn);
Intent intent=getIntent();
String num1=intent.getStringExtra("num1");
String operation=intent.getStringExtra("opreation");
String num2=intent.getStringExtra("num2");
String num=intent.getStringExtra("num");
text.setText(num1+operation+num2+"="+num);
myButton.setOnClickListener(new myButtonListener());
}
class myButtonListener implements OnClickListener{
public void onClick(View arg0){
Intent intent=new Intent(SecondActivity.this,MainActivity.class);
startActivity(intent);
}
}
}
第一个Activity布局:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/seven"
android:layout_width="40px"
android:layout_height="wrap_content"
android:text="7"
/>
<Button
android:id="@+id/eight"
android:layout_toRightOf="@id/seven"
android:layout_width="40px"
android:layout_height="wrap_content"
android:text="8"
/>
<Button
android:id="@+id/nine"
android:layout_toRightOf="@id/eight"
android:layout_width="40px"
android:layout_height="wrap_content"
android:text="9"
/>
<Button
android:id="@+id/div"
android:layout_toRightOf="@id/nine"
android:layout_width="40px"
android:layout_height="wrap_content"
android:text="/"
/>
<Button
android:id="@+id/mod"
android:layout_toRightOf="@id/div"
android:layout_width="40px"
android:layout_height="wrap_content"
android:text="%"
/>
<Button
android:id="@+id/four"
android:layout_below="@id/seven"
android:layout_width="40px"
android:layout_height="wrap_content"
android:text="4"
/>
<Button
android:id="@+id/five"
android:layout_toRightOf="@id/four"
android:layout_below="@id/eight"
android:layout_width="40px"
android:layout_height="wrap_content"
android:text="5"
/>
<Button
android:id="@+id/six"
android:layout_toRightOf="@id/five"
android:layout_below="@id/nine"
android:layout_width="40px"
android:layout_height="wrap_content"
android:text="6"
/>
<Button
android:id="@+id/mul"
android:layout_toRightOf="@id/six"
android:layout_below="@id/div"
android:layout_width="40px"
android:layout_height="wrap_content"
android:text="*"
/>
<Button
android:id="@+id/fenshu"
android:layout_toRightOf="@id/mul"
android:layout_below="@id/mod"
android:layout_width="40px"
android:layout_height="wrap_content"
android:text="1/x"
/>
<Button
android:id="@+id/one"
android:layout_below="@id/four"
android:layout_width="40px"
android:layout_height="wrap_content"
android:text="1"
/>
<Button
android:id="@+id/two"
android:layout_toRightOf="@id/one"
android:layout_below="@id/five"
android:layout_width="40px"
android:layout_height="wrap_content"
android:text="2"
/>
<Button
android:id="@+id/three"
android:layout_toRightOf="@id/two"
android:layout_below="@id/six"
android:layout_width="40px"
android:layout_height="wrap_content"
android:text="3"
/>
<Button
android:id="@+id/sub"
android:layout_toRightOf="@id/three"
android:layout_below="@id/mul"
android:layout_width="40px"
android:layout_height="wrap_content"
android:text="-"
/>
<Button
android:id="@+id/equal"
android:layout_toRightOf="@id/sub"
android:layout_below="@id/fenshu"
android:layout_width="40px"
android:layout_height="70px"
android:text="="
/>
<Button
android:id="@+id/zero"
android:layout_below="@id/one"
android:layout_width="80px"
android:layout_height="wrap_content"
android:text="0"
/>
<Button
android:id="@+id/point"
android:layout_toRightOf="@id/zero"
android:layout_below="@id/three"
android:layout_width="40px"
android:layout_height="wrap_content"
android:text="."
/>
<Button
android:id="@+id/add"
android:layout_toRightOf="@id/point"
android:layout_below="@id/sub"
android:layout_width="40px"
android:layout_height="wrap_content"
android:text="+"
/> </RelativeLayout>
第二个Activity布局:
<?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" > <EditText
android:id="@+id/edt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/one"
/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/edt"
android:text="返回"
/>
</LinearLayout>
android实现计算器功能的更多相关文章
- 完成一段简单的Python程序,用于实现一个简单的加减乘除计算器功能
#!/bin/usr/env python#coding=utf-8'''完成一段简单的Python程序,用于实现一个简单的加减乘除计算器功能'''try: a=int(raw_input(" ...
- Android Studio调试功能使用总结【转】
Android Studio调试功能使用总结[转] 这段时间一直在使用Intellij IDEA, 今天把调试区工具的使用方法记录于此. 先编译好要调试的程序. 1.设置断点 选定要设置断点的代码 ...
- javaWeb 使用 jsp 和 javaBean 实现计算器功能
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- 第一个Android项目——计算器
第一个Android项目——计算器 效果 开始学Android一两个星期了,学习了一下基本的Activity.简单控件及几个简单布局,打算找个东西来练练手,于是就选择发计算器.关于计算器中用到的四则运 ...
- Android 实现闹钟功能
原文地址:Android 实现闹钟功能作者:Android_Learners 一.手机闹钟主要用到了AlarmManager类,AlarmManager类提供了访问系统定时服务的途径,开发人员可以 ...
- Android Studio调试功能使用总结---转
Android Studio调试功能使用总结[转] 这段时间一直在使用Intellij IDEA, 今天把调试区工具的使用方法记录于此. 先编译好要调试的程序. 1.设置断点 选定要设置断点的代码 ...
- C#实现按键计算器功能
C#实现按键计算器功能 (一次失败的编程) 界面: 代码如下: using System; using System.Collections.Generic; using System.Compone ...
- 集成Android免费语音合成功能(在线、离线、离在线融合)
集成Android免费语音合成功能(在线.离线.离在线融合),有这一篇文章就够了(离线)集成Android免费语音合成功能(在线.离线.离在线融合),有这一篇文章就够了(离在线融合) 转眼间,大半年没 ...
- Python-正则表达式实现计算器功能
需求: 用户输入运算表达式,终端显示计算结果 源代码: # !/usr/bin/env/ python3 # -*- coding: utf-8 -*- """用户输入计 ...
随机推荐
- 论js闭包的重要性
很久没写博客了,今天发现了一个很有意思的问题,写下来分享一下 话不多说,贴前端代码: <script type="text/javascript" src="js/ ...
- LDAP 后缀操作
创建目录服务器实例之后,必须为服务器的目录信息树(Directory Information Tree,DIT)创建一个或多个后缀.DIT由服务器中的所有条目组成,这些条目使用各自的标识名(Disti ...
- Java初转型-Maven入门
原系列名:Maven学习总结(一) 原博文出自于:http://www.cnblogs.com/xdp-gacl/p/3498271.html 感谢! 一.Maven的基本概念 Maven(翻译为&q ...
- django: template - built-in tags
本节介绍模板中的内置标签:if for 承上文,修改 views.py 如下: from django.shortcuts import render_to_response class Person ...
- 浅述Oracle分布式事务概念
着系统的复杂性不断增加,我们所面对的分布式系统渐渐增加.分布式文件系统.分布式消息队列系统等等层出不穷,在一些行业特别是互联网行业应用广泛.分布式数据库也是目前使用比较常用的分布式系统之一. 简单来说 ...
- [.NET]Repeater控件使用技巧
1.控制Repeater表格中的按钮显隐 1.1 定义方法 public void Repeater1_ItemDataBinding(object sender, RepeaterItemEvent ...
- acm poj1260 dp
题目大意: 买珍珠,每买一种珍珠需要额外付出十个这种珍珠的钱,但你可以买比这种珍珠高品质的珍珠来替换它(那么就只需要那高品质付出那额外的十个珍珠的钱了,但是每个珍珠的价钱也变化了) 这是一个dp. 令 ...
- Css3小技术
圆角border-radius border-radius:length *注: 这是一个缩写,相当于四个角设置同样的值,用px或者百分比都可以,想要成为圆形,就用50%,你也可以单独设置每个角,语法 ...
- PHP数组的排序函数
对保存在数组中的相关数据进行排序是一件非常有意义的事情.在PHP中提供了很多函数可以对数组进行排序,这些函数提供了多种排序的方法.例如,可以通过元素的值或键及自定义排序等. ①简单的数组排序函数简单的 ...
- Linux下的管道命令有这些:
Linux下的管道命令有这些: 选取命令:cat grep 排序命令:sort wc uniq 双向重定向:tee 字符转换命令: tr, col, join, paste, expand 切割命令: ...