欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

从零开始学android实现计算器功能示例分享(计算器源码)

程序员文章站 2023-11-08 14:43:40
下面是效果展示: 复制代码 代码如下:

下面是效果展示:

从零开始学android实现计算器功能示例分享(计算器源码)

从零开始学android实现计算器功能示例分享(计算器源码)

复制代码 代码如下:

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