<?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" > <LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvResult"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:height="50dp"
android:text="@string/tvResult"
/>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btnBackspace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="150dp"
android:layout_marginLeft="10dp"
android:text="@string/btnbackspace"/>
<Button
android:id="@+id/btnCE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="150dp"
android:text="@string/btnCE"/>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:width="75dp"
android:text="@string/btn7"/>
<Button
android:id="@+id/btn8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btn8"/>
<Button
android:id="@+id/btn9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btn9"/>
<Button
android:id="@+id/btnDiv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btnDiv"/>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:width="75dp"
android:text="@string/btn4"/>
<Button
android:id="@+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btn5"/>
<Button
android:id="@+id/btn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btn6"/>
<Button
android:id="@+id/btnMul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btnMul"/>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:width="75dp"
android:text="@string/btn1"/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btn2"/>
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btn3"/>
<Button
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btnAdd"/>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:width="75dp"
android:text="@string/btn0"/>
<Button
android:id="@+id/btnC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btnC"/>
<Button
android:id="@+id/btnEqu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btnEqu"/>
<Button
android:id="@+id/btnSub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btnSub"/>
</LinearLayout>
</LinearLayout>
package com.example.week2;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity; public class MainActivity extends Activity implements OnClickListener{ //声明一些控件
Button btn0=null;
Button btn1=null;
Button btn2=null;
Button btn3=null;
Button btn4=null;
Button btn5=null;
Button btn6=null;
Button btn7=null;
Button btn8=null;
Button btn9=null;
Button btnBackspace=null;
Button btnCE=null;
Button btnC=null;
Button btnAdd=null;
Button btnSub=null;
Button btnMul=null;
Button btnDiv=null;
Button btnEqu=null;
TextView tvResult=null;
//声明两个参数。接收tvResult前后的值
double num1=0,num2=0;
double Result=0;//计算结果
int op=0;//判断操作数,
boolean isClickEqu=false;//判断是否按了“=”按钮 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//从布局文件中获取控件,
btn0=(Button)findViewById(R.id.btn0);
btn1=(Button)findViewById(R.id.btn1);
btn2=(Button)findViewById(R.id.btn2);
btn3=(Button)findViewById(R.id.btn3);
btn4=(Button)findViewById(R.id.btn4);
btn5=(Button)findViewById(R.id.btn5);
btn6=(Button)findViewById(R.id.btn6);
btn7=(Button)findViewById(R.id.btn7);
btn8=(Button)findViewById(R.id.btn8);
btn9=(Button)findViewById(R.id.btn9);
btnBackspace=(Button)findViewById(R.id.btnBackspace);
btnCE=(Button)findViewById(R.id.btnCE);
btnC=(Button)findViewById(R.id.btnC);
btnEqu=(Button)findViewById(R.id.btnEqu);
btnAdd=(Button)findViewById(R.id.btnAdd);
btnSub=(Button)findViewById(R.id.btnSub);
btnMul=(Button)findViewById(R.id.btnMul);
btnDiv=(Button)findViewById(R.id.btnDiv);
tvResult=(TextView)findViewById(R.id.tvResult); //添加监听\
btnBackspace.setOnClickListener(this);
btnCE.setOnClickListener(this); btn0.setOnClickListener(this);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
btn6.setOnClickListener(this);
btn7.setOnClickListener(this);
btn8.setOnClickListener(this);
btn9.setOnClickListener(this); btnAdd.setOnClickListener(this);
btnSub.setOnClickListener(this);
btnMul.setOnClickListener(this);
btnDiv.setOnClickListener(this);
btnEqu.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
//btnBackspace和CE--------------------
case R.id.btnBackspace:
String myStr=tvResult.getText().toString();
try {
tvResult.setText(myStr.substring(0, myStr.length()-1));
} catch (Exception e) {
tvResult.setText("");
} break;
case R.id.btnCE:
tvResult.setText(null);
break; //btn0--9---------------------------
case R.id.btn0:
if(isClickEqu)
{
tvResult.setText(null);
isClickEqu=false;
}
String myString=tvResult.getText().toString();
myString+="0";
tvResult.setText(myString);
break;
case R.id.btn1:
if(isClickEqu)
{
tvResult.setText(null);
isClickEqu=false;
}
String myString1=tvResult.getText().toString();
myString1+="1";
tvResult.setText(myString1);
break;
case R.id.btn2:
if(isClickEqu)
{
tvResult.setText(null);
isClickEqu=false;
}
String myString2=tvResult.getText().toString();
myString2+="2";
tvResult.setText(myString2);
break;
case R.id.btn3:
if(isClickEqu)
{
tvResult.setText(null);
isClickEqu=false;
}
String myString3=tvResult.getText().toString();
myString3+="3";
tvResult.setText(myString3);
break;
case R.id.btn4:
if(isClickEqu)
{
tvResult.setText(null);
isClickEqu=false;
}
String myString4=tvResult.getText().toString();
myString4+="4";
tvResult.setText(myString4);
break;
case R.id.btn5:
if(isClickEqu)
{
tvResult.setText(null);
isClickEqu=false;
}
String myString5=tvResult.getText().toString();
myString5+="5";
tvResult.setText(myString5);
break;
case R.id.btn6:
if(isClickEqu)
{
tvResult.setText(null);
isClickEqu=false;
}
String myString6=tvResult.getText().toString();
myString6+="6";
tvResult.setText(myString6);
break;
case R.id.btn7:
if(isClickEqu)
{
tvResult.setText(null);
isClickEqu=false;
}
String myString7=tvResult.getText().toString();
myString7+="7";
tvResult.setText(myString7);
break;
case R.id.btn8:
if(isClickEqu)
{
tvResult.setText(null);
isClickEqu=false;
}
String myString8=tvResult.getText().toString();
myString8+="8";
tvResult.setText(myString8);
break;
case R.id.btn9:
if(isClickEqu)
{
tvResult.setText(null);
isClickEqu=false;
}
String myString9=tvResult.getText().toString();
myString9+="9";
tvResult.setText(myString9);
break; //btn+-*/=--------------------------------
case R.id.btnAdd:
String myStringAdd=tvResult.getText().toString();
if(myStringAdd.equals(null))
{
return;
}
num1=Double.valueOf(myStringAdd);
tvResult.setText(null);
op=1;
isClickEqu=false;
break; case R.id.btnSub:
String myStringSub=tvResult.getText().toString();
if(myStringSub.equals(null))
{
return;
}
num1=Double.valueOf(myStringSub);
tvResult.setText(null);
op=2;
isClickEqu=false;
break;
case R.id.btnMul:
String myStringMul=tvResult.getText().toString();
if(myStringMul.equals(null))
{
return;
}
num1=Double.valueOf(myStringMul);
tvResult.setText(null);
op=3;
isClickEqu=false;
break;
case R.id.btnDiv:
String myStringDiv=tvResult.getText().toString();
if(myStringDiv.equals(null))
{
return;
}
num1=Double.valueOf(myStringDiv);
tvResult.setText(null);
op=4;
isClickEqu=false;
break;
case R.id.btnEqu:
String myStringEqu=tvResult.getText().toString();
if(myStringEqu.equals(null))
{
return;
}
num2=Double.valueOf(myStringEqu);
tvResult.setText(null);
switch (op) {
case 0:
Result=num2;
break;
case 1:
Result=num1+num2;
break;
case 2:
Result=num1-num2;
break;
case 3:
Result=num1*num2;
break;
case 4:
Result=num1/num2;
break;
default:
Result=0;
break;
}
tvResult.setText(String.valueOf(Result));
isClickEqu=true;
break; default:
break;
}
}
}

android 计算器的实现(转)的更多相关文章

  1. Android计算器简单逻辑实现

    Android计算器简单逻辑实现 引言: 我的android计算器的实现方式是:按钮输入一次,就处理一次. 但是如果你学过数据结构(栈),就可以使用表达式解析(前缀,后缀)处理. 而这个方式已经很成熟 ...

  2. Android计算器布局

    Android(安桌)计算器布局实现         ——解决整个屏幕方案 引言:     学完了android布局的几种方式,做了一个android计算器. 我在网上搜索了这方面的资料,发现了布局都 ...

  3. Android计算器尝试

    学了一段时间Android了,一直都是在看,没有什么尝试,刚好最近大致学会了gridview配合simpleadpter的使用,于是想着动手练习一下,就选择了写一个最简单的计算器来实现. 只包含+-* ...

  4. java实现可有括号的android计算器

    写了一个android版的计算器,可以计算带括号的表达式,不过前提是:正确的表达式才行 小缺陷是没有做表达式括号的控制,现在还没有想到好的控制方式 package javaAdvanced; impo ...

  5. Android计算器开发实例

    Android简单计算器开发实例如图: ==================================================== activity_main.xml 代码如下: < ...

  6. 结对实验报告-android计算器设计

     一:引言  目前手机可以说是普及率非常高的电子设备了,由于其便于携带,使用方便,资费适中等等原因,现在手机已经在一定程度开始代替固定电话的通话功能,以及一些原来电脑软件上的功能了.手机上的软件也随着 ...

  7. Android——计算器

    layout文件: <?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:andr ...

  8. android计算器

    一:引言    目前手机可以说是普及率非常高的电子设备了,由于其便于携带,使用方便,资费适中等等原因,现在手机已经在一定程度开始代替固定电话的通话功能,以及一些原来电脑软件上的功能了.手机上的软件也随 ...

  9. 【Android实验】UI设计-Android计算器

    目录 实验目的 实验要求 实验过程 1. 界面设计 2. 功能设计 3. 运算处理 实验目的 自主完成一个简单APP的设计工作,综合应用已经学到的Android UI设计技巧,重点注意合理使用布局 实 ...

  10. Android 计算器

    首先在activity_main.xml加入一个EditText 通过xml的方式来沈成一个图像在drawable中新建一个white_bg.xml文件,同时选择一个shape标签corners设置圆 ...

随机推荐

  1. 用Maven创建第一个项目

    1.在Eclipse左侧的空白处点击鼠标右键,选择:New>Other : 2.选择Maven项目,点击"Next"按钮: 3.保持默认,直接点击“Next”按钮: 4.选择 ...

  2. Git 的origin和master分析 push/diff/head(转)

    1.origin/master : 一个叫 origin 的远程库的 master 分支 2.HEAD指向当前工作的branch,master不一定指向当前工作的branch 3.git  push ...

  3. Android软键盘弹出时把布局顶上去的解决方法

    原文: 解决Andriod软键盘出现把原来的布局给顶上去的方法(转) 链接:http://blog.sina.com.cn/s/blog_9564cb6e0101g2eb.html 决方法,在main ...

  4. Android入门:绑定本地服务

    一.绑定服务介绍   前面文章中讲过一般的通过startService开启的服务,当访问者关闭时,服务仍然存在: 但是如果存在这样一种情况:访问者需要与服务进行通信,则我们需要将访问者与服务进行绑定: ...

  5. Sqlerver_各类函数

    SQL Aggregate 函数 SQL Aggregate 函数计算从列中取得的值,返回一个单一的值. 有用的 Aggregate 函数: AVG() - 返回平均值-SELECT AVG(colu ...

  6. quatz2d使用注意点

    1使用图层设置圆角,cornerRadiu一般设置为10的参数,若设置圆形,cornerRadiu设为宽度的一半

  7. gdb 调试学习

    gdb 是unix/linux 系统下的程序调试工具,和IDE(如VS, Eclipse等)的图形化调试工具相比,gdb在断点,跟踪显示方面有着不足,但是它在某些方面比图形化调试工具更加丰富的功能. ...

  8. python操作mongodb之四cp数据库

    from pymongo import MongoClient #连接数据库 client=MongoClient('192.168.30.252',27017) #获取现有数据库的名称 client ...

  9. java 集合(Map)

    -------------------|Map  储存的数据都是以键值对的形式,键不可重复,值可重复. ----------------------------| HashMap ---------- ...

  10. MySQL和OneSQL并行插入性能对比

    按照我的作风,没图说个啥 环境 下面是受叶金荣老师的启发把相关环境交代清楚 MySQL和OneSQL的关键参数配置如下 数据库 sync_binlog innodb_flush_log_at_trx_ ...