设计一个简单的计算器。

第一个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. SpringMvc之@RequestParam详解

    @RequestParam是传递参数的. @RequestParam用于将请求参数区数据映射到功能处理方法的参数上. public String queryUserName(@RequestParam ...

  2. iframe 元素

    iframe 元素会创建包含另外一个文档的内联框架(即行内框架). 可以访问:http://www.w3school.com.cn/tags/tag_iframe.asp

  3. SQL从入门到基础–08 Union、Union all及案例

    一.联合结果集 1. 简单的结果集联合: Select FNumber,FName,FAge from T_Employee union select FidCardNumber,FName,FAge ...

  4. ArrayList 练习

    ArrayList list = new ArrayList(); Random rd = new Random(); ; i <; i++) { , ); //是否包含当前数字 if (!li ...

  5. [转]Mac App distribution in App Store

    Mac程序的大包上传和iOS的有些许不同,因为Mac app既可以上传到store,也可以不通过store供人下载.因此,code sign和provision要根据情况(开发,release< ...

  6. uva 10212 - The Last Non-zero Digit.

    #include <cstdio> #define ll long long const ll MOD = 1e9; int main() { ll N, M; while(scanf(& ...

  7. Jquery简略API使用

    都是个人随手笔记,既然开通了博客园就分享给大家.谨做为参考,具体大家自己测试以及使用 ★ $() ★ JQ的一个万能获取对象的函数(获取跟CSS获取元素是一样的)$(function(){}); 替代 ...

  8. MapDB:专为Java设计的高性能的数据库

    MapDB是一个快速.易用的嵌入式Java数据库引擎,它提供了基于磁盘或者堆外(off-heap允许Java直接操作内存空间, 类似于C的malloc和free)存储的并发的Maps.Sets.Que ...

  9. mysql日志文件相关的配置【2】

    1.二进制日志是什么? mysql 的二进制日志用于记录数据库上做的变更. 2.二进制日志什么时间写到磁盘 1.总的来说二进制日志会在释放锁之前就写入磁盘.也就是说在commit完成之前:client ...

  10. OC 冒泡排序 -- 核心代码

    //冒泡 核心代码 for (int i = 0; i < array.count - 1; i++) { int a = [array[i] intValue]; for (int j = i ...