久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 4754|回復: 2
打印 上一主題 下一主題
收起左側

Android計算器源碼

[復制鏈接]
跳轉到指定樓層
樓主


jisuanqi.rar (8.85 MB, 下載次數: 41)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@drawable/main"
android:orientation="vertical"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context=".MainActivity">

    <RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center">

        <EditText
android:id="@+id/et_showview"
android:layout_width="350dip"
android:layout_height="100dip"
android:background="@drawable/whitebg"
android:editable="false"
android:gravity="bottom|right"
android:textSize="20sp" />


    </RelativeLayout>
    <LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="20dip"
android:gravity="center_horizontal"
android:orientation="vertical">

        <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">

            <Button
android:id="@+id/btn_clear"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="C"
android:textSize="20sp" />

            <Button
android:id="@+id/btn_del"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dip"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="DEL"
android:textSize="20sp" />

            <Button
android:id="@+id/btn_divide"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dip"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="÷"
android:textSize="20sp" />

            <Button
android:id="@+id/btn_multiply"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dip"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:text="×"
android:textSize="20sp" />
        </LinearLayout>

        <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:gravity="center_horizontal"
android:orientation="horizontal">

            <Button
android:id="@+id/btn_7"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="7"
android:textSize="20sp" />

            <Button
android:id="@+id/btn_8"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dip"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="8"
android:textSize="20sp" />

            <Button
android:id="@+id/btn_9"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dip"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="9"
android:textSize="20sp" />

            <Button
android:id="@+id/btn_minus"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dip"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="-"
android:textSize="20sp" />
        </LinearLayout>
        <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:gravity="center_horizontal"
android:orientation="horizontal">

            <Button
android:id="@+id/btn_4"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="4"
android:textSize="20sp" />

            <Button
android:id="@+id/btn_5"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dip"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="5"
android:textSize="20sp" />

            <Button
android:id="@+id/btn_6"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dip"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="6"
android:textSize="20sp" />

            <Button
android:id="@+id/btn_pluse"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dip"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="+"
android:textSize="20sp" />
        </LinearLayout>

        <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:gravity="center_horizontal"
android:orientation="horizontal">

            <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

                <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">

                    <Button
android:id="@+id/btn_1"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="1"
android:textSize="20sp" />

                    <Button
android:id="@+id/btn_2"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dip"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="2"
android:textSize="20sp" />

                    <Button
android:id="@+id/btn_3"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dip"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="3"
android:textSize="20sp" />
                </LinearLayout>

                <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:orientation="horizontal">

                    <Button
android:id="@+id/btn_0"
android:layout_width="170dp"
android:layout_height="80dp"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="0"
android:textSize="20sp" />

                    <Button
android:id="@+id/btn_point"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dip"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="."
android:textSize="20sp" />
                </LinearLayout>
            </LinearLayout>

            <Button
android:id="@+id/btn_equal"
android:layout_width="80dp"
android:layout_height="170dip"
android:layout_marginLeft="10dip"
android:background="@drawable/orange_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="="
android:textSize="20sp" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>



package com.example;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btn_0;
    private Button btn_1;
    private Button btn_2;
    private Button btn_3;
    private Button btn_4;
    private Button btn_5;
    private Button btn_6;
    private Button btn_7;
    private Button btn_8;
    private Button btn_9;

    private Button btn_point;// 小數點
private Button btn_divide;// 除以
private Button btn_multiply;// 乘以
private Button btn_minus;// 減去
private Button btn_pluse;// 加
private Button btn_equal;// 等于
private Button btn_clear;  //清空
private Button btn_del;  //取消
private EditText et_showview;  //輸入框
private double dou = 0;  //接收結果
    /**
     * 加一個標識
     * 需求就是我們運算出結果的時候,再次點擊數字及你選哪個下一次運算,這個時候,輸入框應該自動為空
     */
private boolean flag;  //清空標識
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        btn_0 = (Button) findViewById(R.id.btn_0);
        btn_1 = (Button) findViewById(R.id.btn_1);
        btn_2 = (Button) findViewById(R.id.btn_2);
        btn_3 = (Button) findViewById(R.id.btn_3);
        btn_4 = (Button) findViewById(R.id.btn_4);
        btn_5 = (Button) findViewById(R.id.btn_5);
        btn_6 = (Button) findViewById(R.id.btn_6);
        btn_7 = (Button) findViewById(R.id.btn_7);
        btn_8 = (Button) findViewById(R.id.btn_8);
        btn_9 = (Button) findViewById(R.id.btn_9);
        btn_point = (Button) findViewById(R.id.btn_point);// 小數點
btn_divide = (Button) findViewById(R.id.btn_divide);// 除以
btn_multiply = (Button) findViewById(R.id.btn_multiply);// 乘以
btn_minus = (Button) findViewById(R.id.btn_minus);// 減去
btn_pluse = (Button) findViewById(R.id.btn_pluse);// 加
btn_equal = (Button) findViewById(R.id.btn_equal);// 等于
btn_clear = (Button) findViewById(R.id.btn_clear);
        btn_del = (Button) findViewById(R.id.btn_del);
        et_showview = (EditText) findViewById(R.id.et_showview);
        btn_0.setOnClickListener(this);
        btn_1.setOnClickListener(this);
        btn_2.setOnClickListener(this);
        btn_3.setOnClickListener(this);
        btn_4.setOnClickListener(this);
        btn_5.setOnClickListener(this);
        btn_6.setOnClickListener(this);
        btn_7.setOnClickListener(this);
        btn_8.setOnClickListener(this);
        btn_9.setOnClickListener(this);
        btn_point.setOnClickListener(this);
        btn_divide.setOnClickListener(this);
        btn_multiply.setOnClickListener(this);
        btn_minus.setOnClickListener(this);
        btn_pluse.setOnClickListener(this);
        btn_equal.setOnClickListener(this);
        btn_clear.setOnClickListener(this);
        btn_del.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        //我們在這里實現業務邏輯
String str = et_showview.getText().toString();
        switch (v.getId()) {
            //我們點擊數字鍵,輸入框就收到我們的計算數字
case R.id.btn_0:
            case R.id.btn_1:
            case R.id.btn_2:
            case R.id.btn_3:
            case R.id.btn_4:
            case R.id.btn_5:
            case R.id.btn_6:
            case R.id.btn_7:
            case R.id.btn_8:
            case R.id.btn_9:
            case R.id.btn_point:
                //再次輸入的時候判斷,把前面的清空,再把點擊的按鈕放在輸入框上
if (flag) {
                    flag = false;
                    str = "";
                    et_showview.setText("");
                }
                //我們只要點擊鍵盤,相應的數字添加在EditText上
et_showview.setText(str + ((Button) v).getText());
                break;
            //運算符也是一樣需要添加的
case R.id.btn_pluse://+
case R.id.btn_minus://-
case R.id.btn_multiply://*
case R.id.btn_divide:///
                //再次輸入的時候判斷,把前面的清空,再把點擊的按鈕放在輸入框上
if (flag) {
                    flag = false;
                    str = "";
                    et_showview.setText("");
                }
                //為了計算方便,我們可以在前后添加空格
et_showview.setText(str + " " + ((Button) v).getText() + " ");
                break;
            case R.id.btn_del:
                //再次輸入的時候判斷,把前面的清空,再把點擊的按鈕放在輸入框上
if (flag) {
                    flag = false;
                    str = "";
                    et_showview.setText("");
                } else if (str != null && !str.equals(" ")) {//判斷,因為我們需要一個個刪,首先確定是不是空或者""
                    //用substring()截取字符長度-1 再設置回去 形成刪一個的效果
et_showview.setText(str.substring(0, str.length() - 1));
                }
                break;
            //清除按鈕,直接把輸入框設置成空
case R.id.btn_clear:
                flag = false;
                str = "";
                et_showview.setText(" ");
                break;
            //計算結果
case R.id.btn_equal:
                getResult();
                break;
        }
    }

    //等號計算結果
private void getResult() {
        //首先取一下你輸入完后現在輸入框的內容
String result = et_showview.getText().toString();
        //當我們的輸入框是null或者""時我們不進行操作
if (result == null || result.equals(" ")) {
            return;
        }
        //我們要計算結果,必須知道輸入框是否有操作符,判斷條件就是前后是否在空格,否則不進行操作
if (!result.contains(" ")) {
            //沒有運算符,所以不用運算
return;
        }
        //當我點擊等號的時候,清空標識設置為true,但是如果之前有的話,設置成false
if (flag) {
            flag = false;
            return;
        }
        flag = true;
        //如果有空格,我們就截取前后段再獲取運算符進行計算
String str1 = result.substring(0, result.indexOf(" "));  //運算符前面字段
String op = result.substring(result.indexOf(" ") + 1, result.indexOf(" ") + 2);  //截取運算符
String str2 = result.substring(result.indexOf(" ") + 3);  //運算符后面字段
        //這里我們還是要判斷str1和str2都不是空才
if (!str1.equals("") && !str2.equals("")) {  //第一種情況:都不是空
double d1 = Double.parseDouble(str1);
            double d2 = Double.parseDouble(str2);
            //開始計算,定義一個double變量接收結果,定義為全局
if (op.equals("+")) {  //加
dou = d1 + d2;
            } else if (op.equals("-")) { //減
dou = d1 - d2;
            } else if (op.equals("×")) { //乘
dou = d1 * d2;
            } else if (op.equals("÷")) {  //除
                //除數為0不計算
if (d2 == 0) {
                    dou = 0;
                } else {
                    dou = d1 / d2;
                }
            }
            //我們之前把他強轉為double類型了,但是如果沒有小數點,我們就是int類型
if (!str1.contains(".") && !str2.contains(".") && !op.equals("÷")) {
                int i = (int) dou;
                et_showview.setText(i + "");
            } else {  //如果有小數點
et_showview.setText(dou + "");
            }
        } else if (!str1.equals("") && str2.equals("")) {  //第二種情況:str2是空
            //這種情況就不運算了
et_showview.setText(result);
        } else if (str1.equals("") && !str2.equals("")) {  //第三種情況:str1是空
            //這種情況我們就需要判斷了,因為此時str1 = 0;
double d3 = Double.parseDouble(str2);
            if (op.equals("+")) {  //加
dou = 0 + d3;
            } else if (op.equals("-")) { //減
dou = 0 - d3;
            } else if (op.equals("×")) { //乘
dou = 0;
            } else if (op.equals("÷")) {  //除
                //除數為0不計算
dou = 0;
            }
            //我們之前把他強轉為double類型了,但是如果沒有小數點,我們就是int類型
if (!str2.contains(".")) {
                int i = (int) dou;
                et_showview.setText(i + "");
            } else {  //如果有小數點
et_showview.setText(dou + "");
            }
        } else {  //最后一種情況,他們兩個都是空
            //我們清空掉
et_showview.setText("");
        }
    }
}




評分

參與人數 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏3 分享淘帖 頂 踩
回復

使用道具 舉報

沙發
ID:371895 發表于 2021-1-1 22:16 來自手機 | 只看該作者
此帖僅作者可見

使用道具 舉報

板凳
ID:241249 發表于 2021-2-15 22:01 | 只看該作者
此帖僅作者可見

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 污污免费网站 | 亚洲视频免费在线 | 日韩国产欧美在线观看 | 国产精品18久久久 | 欧美极品在线 | 美女视频黄色片 | 日韩视频精品在线 | 亚洲福利 | 成人精品国产 | 男女污污动态图 | 玖玖综合在线 | 中文字幕久久精品 | 久久精品一区二区三区四区 | 亚洲欧美成人影院 | 国产精品久久久久久久久久久久冷 | 成人视屏在线观看 | caoporn免费| 亚洲欧美激情网 | 久久久国产精品一区 | 中文一区 | 日日干夜夜草 | 亚洲综合中文字幕在线观看 | 欧美综合一区二区三区 | 亚洲一区二区三区视频 | 能看的av | 免费黄色特级片 | www国产成人免费观看视频,深夜成人网 | 日韩在线欧美 | 午夜电影网站 | 黄色欧美在线 | av大全在线观看 | 日韩精品一区二区三区中文字幕 | 国产精品综合色区在线观看 | 男女视频91| 免费a大片 | 国产做爰 | 欧美高清一区 | 国产精品久久在线 | 日本在线一区二区 | 91玖玖| 国内精品伊人久久久久网站 |