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 -*- """用户输入计 ...
随机推荐
- magento产品eav笔记【持续跟新...】
//magento把产品信息分在子表中,最顶上的表是catalog_product_entity,仅仅包含产品的信息(SKU) //表eav_attribute,这张表在magento里为全部不 同的 ...
- 【GitHub-SwipeMenuListView】针对ListView item的側滑菜单
项目地址:https://github.com/baoyongzhang/SwipeMenuListView Usage Step 1:import swipemenulistview.jar Ste ...
- Java调用天气Webservice的小应用
废话不多说,直接贴代码: CityReq.java package com.weather; import javax.xml.bind.annotation.XmlElement; import j ...
- Java基础知识强化61:经典查找之 常见查找算法小结
一.顺序查找 条件:无序或有序队列. 原理:按顺序比较每个元素,直到找到关键字为止. 时间复杂度:O(n) 二.二分查找(折半查找) 条件:有序数组 原理:查找过程从数组的中间元素开始,如果中间元素正 ...
- 内容提供者 DocumentProvider Uri工具类
Activity /**详见http://blog.csdn.net/coder_pig/article/details/47905881 Calendar Provider:日历提供者,就是针对针对 ...
- IE标签a嵌套table标签,链接点击无效
在IE中,使用如下代码将无法触发跳转: <a href="http://xx.xx.com"> <table> <tr> <td>点 ...
- javascript获取标签样式(获取背景为例)
function getStyle(el){ if(window.getComputedStyle){ return window.getComputedStyle(el,null); } retur ...
- Typecho中文验证码Captcha插件
前言实在是受不了每天都要删除掉上百条的垃圾评论,干脆自己做了个验证码插件,顺带做的完善了些,分享给大家. 本插件是在评论验证码插件基础上完善而来.所不同的是,采用了最新的securimage 3.0. ...
- php 中_set()_get()实例解析
<?php class Person { // 下面是人的成员属性, 都是封装的私有成员 private $name; // 人的名子 private $sex; // 人的性别 private ...
- CSAPP--存储器及程序的局部性
作为一名程序员,你需要理解计算机存储系统的层次结构,他对应用程序的性能有着巨大的影响,如果程序所需要的数据存储在cpu的寄存器中,那么指令在执行期间,就可以花费零个周期来进行访问,而在Cache中则需 ...