设计一个简单的计算器。

第一个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实现计算器功能的更多相关文章

  1. 完成一段简单的Python程序,用于实现一个简单的加减乘除计算器功能

    #!/bin/usr/env python#coding=utf-8'''完成一段简单的Python程序,用于实现一个简单的加减乘除计算器功能'''try: a=int(raw_input(" ...

  2. Android Studio调试功能使用总结【转】

    Android Studio调试功能使用总结[转]   这段时间一直在使用Intellij IDEA, 今天把调试区工具的使用方法记录于此. 先编译好要调试的程序. 1.设置断点 选定要设置断点的代码 ...

  3. javaWeb 使用 jsp 和 javaBean 实现计算器功能

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  4. 第一个Android项目——计算器

    第一个Android项目——计算器 效果 开始学Android一两个星期了,学习了一下基本的Activity.简单控件及几个简单布局,打算找个东西来练练手,于是就选择发计算器.关于计算器中用到的四则运 ...

  5. Android 实现闹钟功能

      原文地址:Android 实现闹钟功能作者:Android_Learners 一.手机闹钟主要用到了AlarmManager类,AlarmManager类提供了访问系统定时服务的途径,开发人员可以 ...

  6. Android Studio调试功能使用总结---转

    Android Studio调试功能使用总结[转]   这段时间一直在使用Intellij IDEA, 今天把调试区工具的使用方法记录于此. 先编译好要调试的程序. 1.设置断点 选定要设置断点的代码 ...

  7. C#实现按键计算器功能

    C#实现按键计算器功能 (一次失败的编程) 界面: 代码如下: using System; using System.Collections.Generic; using System.Compone ...

  8. 集成Android免费语音合成功能(在线、离线、离在线融合)

    集成Android免费语音合成功能(在线.离线.离在线融合),有这一篇文章就够了(离线)集成Android免费语音合成功能(在线.离线.离在线融合),有这一篇文章就够了(离在线融合) 转眼间,大半年没 ...

  9. Python-正则表达式实现计算器功能

    需求: 用户输入运算表达式,终端显示计算结果 源代码: # !/usr/bin/env/ python3 # -*- coding: utf-8 -*- """用户输入计 ...

随机推荐

  1. JavaScript的68个技巧一

    1. 严格模式 在自己的项目中 你可以坚持只使用" 严格模式 " 或只使用" 非严格模式 "的策略.但如果你要编写健壮的代码应对各种各样的代码连接 你有两个可选 ...

  2. Universal-Image-Loader 示例 工具

    Base public class MyApplication extends Application {     @Override     public void onCreate() {     ...

  3. C#之out与ref的共性与区别以及用法

    引入: 首先看一个例子: class Program { static void Main(string[] args) { ; int result = Test(number); Console. ...

  4. C# 还原SQL数据库(非存储过程方式)

    Winform的代码,最好是在数据所在电脑上运行代码,不然会因为权限问题导致失败. 数据库备份: SqlConnection con = new SqlConnection("Data So ...

  5. django: startproject

    python 的 django 框架的安装教程很多,这里不列举安装过程,直接开始记开发应用过程. 1  startprojec,新建项目 $ django-admin.py startproject ...

  6. 【回顾整理】暴走的SQL语句练习!!!

    设计一个学生成绩数据库,该库包含学生,老师,课程和成绩等信息并完成后面的练习.学生:学号(SNO).姓名(SNAME).性别(SSEX).生日(SBIRTHDAY ).所属班级(SCLASS )课程: ...

  7. (转)ASP.NET MVC 学习第一天

    天道酬勤0322   博客园 | 首页 | 发新随笔 | 发新文章 | 联系 | 订阅  | 管理 随笔:10 文章:0 评论:9 引用:0 ASP.NET MVC 学习第一天 今天开始第一天学习as ...

  8. 127.0.0.1与localhost与ip的区别

    127.0.0.1与localhost与ip的区别 May 18, 2014 localhost 不联网不使用网卡,不受防火墙和网卡限制本机访问 一般使用 本地套接字文件AF_UNIX 应用程序一般约 ...

  9. 单源最短路径 hdu 2066

    #include<string.h> //和prim类似#include<stdio.h> //prim所考虑的是节点与当前所建成的树的距离最小using namespace ...

  10. uva12489 Combating cancer(树同构)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud https://uva.onlinejudge.org/index.php?opt ...