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

 找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 4685|回復(fù): 2
打印 上一主題 下一主題
收起左側(cè)

Android計(jì)算器源碼

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主


jisuanqi.rar (8.85 MB, 下載次數(shù): 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;// 小數(shù)點(diǎn)
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;  //接收結(jié)果
    /**
     * 加一個(gè)標(biāo)識(shí)
     * 需求就是我們運(yùn)算出結(jié)果的時(shí)候,再次點(diǎn)擊數(shù)字及你選哪個(gè)下一次運(yùn)算,這個(gè)時(shí)候,輸入框應(yīng)該自動(dòng)為空
     */
private boolean flag;  //清空標(biāo)識(shí)
@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);// 小數(shù)點(diǎn)
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) {
        //我們?cè)谶@里實(shí)現(xiàn)業(yè)務(wù)邏輯
String str = et_showview.getText().toString();
        switch (v.getId()) {
            //我們點(diǎn)擊數(shù)字鍵,輸入框就收到我們的計(jì)算數(shù)字
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:
                //再次輸入的時(shí)候判斷,把前面的清空,再把點(diǎn)擊的按鈕放在輸入框上
if (flag) {
                    flag = false;
                    str = "";
                    et_showview.setText("");
                }
                //我們只要點(diǎn)擊鍵盤,相應(yīng)的數(shù)字添加在EditText上
et_showview.setText(str + ((Button) v).getText());
                break;
            //運(yùn)算符也是一樣需要添加的
case R.id.btn_pluse://+
case R.id.btn_minus://-
case R.id.btn_multiply://*
case R.id.btn_divide:///
                //再次輸入的時(shí)候判斷,把前面的清空,再把點(diǎn)擊的按鈕放在輸入框上
if (flag) {
                    flag = false;
                    str = "";
                    et_showview.setText("");
                }
                //為了計(jì)算方便,我們可以在前后添加空格
et_showview.setText(str + " " + ((Button) v).getText() + " ");
                break;
            case R.id.btn_del:
                //再次輸入的時(shí)候判斷,把前面的清空,再把點(diǎn)擊的按鈕放在輸入框上
if (flag) {
                    flag = false;
                    str = "";
                    et_showview.setText("");
                } else if (str != null && !str.equals(" ")) {//判斷,因?yàn)槲覀冃枰粋(gè)個(gè)刪,首先確定是不是空或者""
                    //用substring()截取字符長(zhǎng)度-1 再設(shè)置回去 形成刪一個(gè)的效果
et_showview.setText(str.substring(0, str.length() - 1));
                }
                break;
            //清除按鈕,直接把輸入框設(shè)置成空
case R.id.btn_clear:
                flag = false;
                str = "";
                et_showview.setText(" ");
                break;
            //計(jì)算結(jié)果
case R.id.btn_equal:
                getResult();
                break;
        }
    }

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




評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

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

使用道具 舉報(bào)

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

使用道具 舉報(bào)

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

使用道具 舉報(bào)

本版積分規(guī)則

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

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 嫩草视频在线 | 羞羞视频免费观看入口 | 神马久久久久久久久久 | 日韩成人av在线 | 亚洲一区国产 | 一区二区三区四区不卡视频 | 一区二区三区网站 | 一级a爱片久久毛片 | 色婷婷av一区二区三区软件 | 在线一区观看 | 国产成人精品免费视频大全最热 | 天天操网| 夜夜爽99久久国产综合精品女不卡 | 亚洲国产一区在线 | 在线成人免费视频 | 久久免费国产视频 | 久久久精品久久 | 欧美国产免费 | 日韩在线观看 | 成人免费观看男女羞羞视频 | 国产成人叼嘿视频在线观看 | 国产欧美在线视频 | 国产精品成人一区二区三区 | 精品久久久久久国产 | 亚洲天堂男人的天堂 | 久久精品成人一区 | 黄色精品 | 亚洲精品九九 | 在线免费观看黄a | 国产极品车模吞精高潮呻吟 | 久久精品视频一区二区 | 国产精品永久免费视频 | 羞羞视频在线观免费观看 | 久久久久亚洲视频 | 亚洲电影免费 | av日韩一区| 91大神在线资源观看无广告 | 日本不卡免费新一二三区 | 99久久久久久99国产精品免 | 性高湖久久久久久久久 | 国产精品国产自产拍高清 |