练习2是实现一个计算器的功能;可以加减乘除;可以倒退,可以清空文本。

下面是效果展示:

-----------------------布局-------------------------------

<?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;
        }
    }
 
 
}
 
下载
 
 

Technorati 标签: android,安卓,计算器

android实操--练习2的更多相关文章

  1. android实操--练习1

    这两天有空,打算把一些文档整理一下,快要考试了,找一些简单的例子来做做,重温安卓的知识. 下面是第一个练习: 实现很简单,下面我们来看看: 首先新建一个安卓项目Demo1 接着是界面的布局(包括act ...

  2. Appium常用Api实操

    本文是基于python语言在android上实操的,仅记录(忽略排版~~~) 会不时更新的: from appium import webdriver from selenium.webdriver. ...

  3. ABP入门系列(1)——学习Abp框架之实操演练

    作为.Net工地搬砖长工一名,一直致力于挖坑(Bug)填坑(Debug),但技术却不见长进.也曾热情于新技术的学习,憧憬过成为技术大拿.从前端到后端,从bootstrap到javascript,从py ...

  4. 号外号外:9月13号《Speed-BI云平台案例实操--十分钟做报表》开讲了

    引言:如何快速分析纷繁复杂的数据?如何快速做出老板满意的报表?如何快速将Speed-BI云平台运用到实际场景中?         本课程将通过各行各业案例背景,将Speed-BI云平台运用到实际场景中 ...

  5. Mysql MHA(GTID)配置(实操)

    实现环境 centos6.7 MYSQL5.6.36 主:192.168.1.191 从1:192.168.1.145 从2:192.168.1.146 监测:放在从2上 192.168.1.146 ...

  6. Selenium之unittest测试框架详谈及实操

    申明:本文是基于python3.x及selenium3.x. unittest,也可以称为PyUnit,可以用来创建全面的测试套件,可以用于单元自动化测试(模块).功能自动化测试(UI)等等. 官方文 ...

  7. unittest测试框架详谈及实操(二)

    类级别的setUp()方法与tearDown()方法 在实操(一)的例子中,通过setUp()方法为每个测试方法都创建了一个Chrome实例,并且在每个测试方法执行结束后要关闭实例.是不是觉得有个多余 ...

  8. Android实训案例(九)——答题系统的思绪,自己设计一个题库的体验,一个思路清晰的答题软件制作过程

    Android实训案例(九)--答题系统的思绪,自己设计一个题库的体验,一个思路清晰的答题软件制作过程 项目也是偷师的,决心研究一下数据库.所以写的还是很详细的,各位看官,耐着性子看完,实现结果不重要 ...

  9. Android实训案例(八)——单机五子棋游戏,自定义棋盘,线条,棋子,游戏逻辑,游戏状态存储,再来一局

    Android实训案例(八)--单机五子棋游戏,自定义棋盘,线条,棋子,游戏逻辑,游戏状态存储,再来一局 阿法狗让围棋突然就被热议了,鸿洋大神也顺势出了篇五子棋单机游戏的视频,我看到了就像膜拜膜拜,就 ...

随机推荐

  1. ASP.NET Cookie概念、CURD操作、原理、实际运用

    会话就WEB开发来说,一个会话就是你通过浏览器与服务器之间的一次通话,只不过这种通话是以用浏览器浏览的方式来实现的. 就会话的应用来说,一般会话是用来识别用户的,比如你可以使用会话级变量记录当前用户已 ...

  2. C++ 标准输出cout与printf

    C++标准输出cout与printf都可以,printf用法更死板一些. #include <iostream> int main(int argc, char** argv) { usi ...

  3. Django---项目如何创建

    首先是安装好Django,找到 Scripts 目录配置环境变量: 只要添加到环境变量,在任何目录执行 django-admin startproject mysite 就可以创建 Django 程序 ...

  4. VS2015:出现devenv.sln解决方案保存对话框

    问题描述: 打开VS2015项目时,提示保存“devenv.sln” 解决方法: 找到文件:C:\Program Files (x86)\Common Files\microsoft shared\M ...

  5. C#调用C++ DLL的方式

    动态链接库(DLL)是一个包含可由多个程序同时使用的代码和数据的库,DLL不是可执行文件.可以说在windows操作系统中随处可见,打开主分区盘下的system32.在一些项目中,如果有大量运算或者涉 ...

  6. [实战]MVC5+EF6+MySql企业网盘实战(1)

    写在前面 不久前,一个朋友让帮他弄一个单位的企业网盘的管理站点,一直忙,最近抽出了点时间,也想琢磨琢磨mvc,ef,mysql,这算是边琢磨,边实践吧. 系列文章 [实战]MVC5+EF6+MySql ...

  7. 源代码的管理与在eclipse中使用maven进行代码测试

    管理源代码的工具 开发历史记录 SVN :集中式的源代码管理工具 通常必须连到公司的服务器上才能正常工作 (提交代码,查看代码的历史记录 查看代码的分支) 在公司中开发项目时 每天必须至少提交(Com ...

  8. Nginx限制IP访问及获取客户端realip实战

    做网站时经常会用到remote_addr和x_forwarded_for这两个头信息来获取客户端的IP,然而当有反向代理或者CDN的情况下,这两个值就不够准确了,需要调整一些配置.Nginx作为web ...

  9. linux:帮助命令help、man、info

    笔记内容如下: 1.内建命令与外部命令之分2.help , man , info命令的使用以及区别 内建命令与外部命令 有一些查看帮助的工具在内建命令与外建命令上是有区别对待的. 内建命令实际上是 s ...

  10. array string mysql IN LIKE

    $ids = $_POST['ids']; $str = implode(',',$ids); $str = '('.$str.')'; $db_region->region_del($str) ...