欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

從零開始學android實現(xiàn)計算器功能示例分享(計算器源碼)

 更新時間:2014年02月08日 15:04:02   作者:  
這篇文章主要介紹了android實現(xiàn)的計算器功能示例,可以加減乘除;可以倒退,可以清空文本,大家參考使用吧

下面是效果展示:



復制代碼 代碼如下:

<?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;
    //聲明兩個參數(shù)。接收tvResult前后的值
    double num1=0,num2=0;
    double Result=0;//計算結果
    int op=0;//判斷操作數(shù),
    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);

        //添加監(jiān)聽\
        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 判斷是否連接成功了指定wifi

    Android 判斷是否連接成功了指定wifi

    本文主要介紹了Android 判斷是否連接成功了指定wifi的相關知識。具有很好的參考價值。下面跟著小編一起來看下吧
    2017-04-04
  • Android DrawableTextView圖片文字居中顯示實例

    Android DrawableTextView圖片文字居中顯示實例

    在我們開發(fā)中,TextView設置Android:drawableLeft一定使用的非常多,但Drawable和Text同時居中顯示可能不好控制,小編想到通過自定義TextView實現(xiàn),具體詳情大家參考下本文
    2017-03-03
  • Android?中?FrameLayout?布局及屬性的使用詳解

    Android?中?FrameLayout?布局及屬性的使用詳解

    這篇文章主要介紹了Android?中?FrameLayout?布局及屬性的使用,FrameLayout?在實現(xiàn)簡單布局時非常方便,特別適用于疊加式布局,如顯示疊加的圖層或浮動按鈕等,需要的朋友可以參考下
    2024-03-03
  • Android清空編輯框內(nèi)容功能的實現(xiàn)實例代碼

    Android清空編輯框內(nèi)容功能的實現(xiàn)實例代碼

    本篇文章主要介紹了Android清空編輯框數(shù)據(jù)功能的實現(xiàn)實例代碼,非常具有實用價值,需要的朋友可以參考下。
    2017-03-03
  • Android?NDK開發(fā)(C語言--聯(lián)合體與枚舉)

    Android?NDK開發(fā)(C語言--聯(lián)合體與枚舉)

    這篇文章主要介紹了Android?NDK開發(fā)C語言聯(lián)合體與枚舉,共用體是一種特殊的數(shù)據(jù)類型,允許您在相同的內(nèi)存位置存儲不同的數(shù)據(jù)類型。您可以定義一個帶有多成員的共用體,但是任何時候只能有一個成員帶有值。下面詳細介紹該內(nèi)容,需要的朋友可以參考一下
    2021-12-12
  • Android Studio項目適配AndroidX(Android 9.0)的方法步驟

    Android Studio項目適配AndroidX(Android 9.0)的方法步驟

    這篇文章主要介紹了Android Studio項目適配AndroidX(Android 9.0)的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-11-11
  • Android控件之TabHost用法實例分析

    Android控件之TabHost用法實例分析

    這篇文章主要介紹了Android控件之TabHost用法,以完整實例形式較為詳細的分析了TabHost控件實現(xiàn)選項卡的相關技巧,需要的朋友可以參考下
    2015-09-09
  • Android 點擊ImageButton時有“按下”的效果的實現(xiàn)

    Android 點擊ImageButton時有“按下”的效果的實現(xiàn)

    這篇文章主要介紹了 Android 點擊ImageButton時有“按下”的效果的實現(xiàn)的相關資料,需要的朋友可以參考下
    2017-03-03
  • Kotlin by關鍵字作用及使用介紹

    Kotlin by關鍵字作用及使用介紹

    Kotlin 中的 by 關鍵字在 Java 中是沒有的,這使我對它感到非常陌生。Kotlin 中為什么要新增 by 關鍵字呢?by 關鍵字在 Kotlin 中是如何使用的?本文會介紹 by 關鍵字的使用分類,具體的示例,Kotlin 內(nèi)置的 by 使用,希望能夠幫助到大家
    2022-10-10
  • Android編程實現(xiàn)監(jiān)控apk安裝,卸載,替換的方法

    Android編程實現(xiàn)監(jiān)控apk安裝,卸載,替換的方法

    這篇文章主要介紹了Android編程實現(xiàn)監(jiān)控apk安裝,卸載,替換的方法,涉及Android基于Intent監(jiān)控apk狀態(tài)的功能實現(xiàn)技巧,需要的朋友可以參考下
    2016-01-01

最新評論