国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Android Studio制作一個簡單的計算器APP

這篇具有很好參考價值的文章主要介紹了Android Studio制作一個簡單的計算器APP。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

雖然現(xiàn)在我們?nèi)粘I钪泻苌儆玫接嬎闫?,但是第一次嘗試在Android Studio上做一個計算器

程序設(shè)計步驟: (1)在布局文件中聲明編輯文件框EditText,按鈕Button等組件。 (2)在MainActivity中獲取組件實例。 (3)通過swtich函數(shù),判斷輸入的內(nèi)容,并進行相應操作,通過getText()獲取文本內(nèi)容,setText()顯示。

程序代碼設(shè)計: 布局實現(xiàn):在activi_main.xml中設(shè)置。使用線性布局(LinearLayout)與網(wǎng)格布局(GridLayout)來設(shè)置界面。在設(shè)計區(qū)域設(shè)置一個4行4列的網(wǎng)格布局,每行劃分為均等的16個按鈕,分別代表數(shù)字0-9,小數(shù)點,和運算符加減乘除以及等于號。 ?

最終效果圖如下:

Android Studio制作一個簡單的計算器APP文章來源地址http://www.zghlxwxcb.cn/news/detail-508874.html

xml文件:

<?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_marginLeft="30dp"
    android:layout_marginTop="16dp"
    android:layout_marginRight="30dp"
    android:layout_marginBottom="20dp"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/Answer"
        android:layout_width="match_parent"
        android:layout_height="189dp"
        android:layout_weight="0.33"
        android:hint="Result"
        android:textSize="34sp" />

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

        <Button
            android:id="@+id/btn10"
            android:layout_width="wrap_content"
            android:layout_height="76dp"
            android:background="@color/purple_200"
            android:text="C"
            android:textColor="#F30C4D"
            android:textSize="20sp" />

        <Button
            android:id="@+id/btn11"
            android:layout_width="wrap_content"
            android:layout_height="76dp"
            android:background="@color/purple_200"
            android:text="%"
            android:textColor="#FF5722"
            android:textSize="20sp" />

        <Button
            android:id="@+id/btn12"
            android:layout_width="175dp"
            android:layout_height="76dp"
            android:background="@color/purple_200"
            android:text="Del"
            android:textColor="#F30C4D"
            android:textSize="20sp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="76dp"
            android:background="@color/purple_200"
            android:text="1"
            android:textSize="26dp" />

        <Button
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="76dp"
            android:background="@color/teal_200"
            android:text="2"
            android:textSize="26dp" />

        <Button
            android:id="@+id/btn3"
            android:layout_width="wrap_content"
            android:layout_height="76dp"
            android:background="@color/purple_200"
            android:text="3"
            android:textSize="26dp" />

        <Button
            android:id="@+id/btnadd"
            android:layout_width="wrap_content"
            android:layout_height="77dp"
            android:background="#00BCD4"
            android:text="+"
            android:textColor="#FFEB3B"
            android:textSize="26dp" />
    </LinearLayout>

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

        <Button
            android:id="@+id/btn4"
            android:layout_width="wrap_content"
            android:layout_height="76dp"
            android:background="@color/purple_200"
            android:text="4"
            android:textSize="26dp" />

        <Button
            android:id="@+id/btn5"
            android:layout_width="wrap_content"
            android:layout_height="76dp"
            android:background="@color/purple_200"
            android:text="5"
            android:textSize="26dp" />

        <Button
            android:id="@+id/btn6"
            android:layout_width="wrap_content"
            android:layout_height="76dp"
            android:background="@color/purple_200"
            android:text="6"
            android:textSize="26dp" />

        <Button
            android:id="@+id/btnsub"
            android:layout_width="wrap_content"
            android:layout_height="76dp"
            android:background="@color/purple_200"
            android:text="-"
            android:textColor="#FFEB3B"
            android:textSize="26dp" />
    </LinearLayout>

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

        <Button
            android:id="@+id/btn7"
            android:layout_width="wrap_content"
            android:layout_height="76dp"
            android:background="@color/purple_200"
            android:text="7"
            android:textSize="26dp" />

        <Button
            android:id="@+id/btn8"
            android:layout_width="wrap_content"
            android:layout_height="76dp"
            android:background="@color/purple_200"
            android:text="8"
            android:textSize="26dp" />

        <Button
            android:id="@+id/btn9"
            android:layout_width="wrap_content"
            android:layout_height="76dp"
            android:background="@color/purple_200"
            android:text="9"
            android:textSize="26dp" />

        <Button
            android:id="@+id/btnmul"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@color/purple_200"
            android:text="×"
            android:textColor="#FFEB3B"
            android:textSize="26dp" />
    </LinearLayout>

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

        <Button
            android:id="@+id/btn0"
            android:layout_width="wrap_content"
            android:layout_height="76dp"
            android:background="@color/purple_200"
            android:text="0"
            android:textColor="#E91E63"
            android:textSize="26dp" />

        <Button
            android:id="@+id/btndot"
            android:layout_width="wrap_content"
            android:layout_height="76dp"
            android:background="@color/purple_200"
            android:text="."
            android:textSize="26dp" />

        <Button
            android:id="@+id/btnequel"
            android:layout_width="wrap_content"
            android:layout_height="76dp"
            android:background="@color/purple_200"
            android:text="="
            android:textColor="#FF5722"
            android:textSize="26dp" />

        <Button
            android:id="@+id/btndiv"
            android:layout_width="wrap_content"
            android:layout_height="76dp"
            android:background="@color/purple_200"
            android:text="÷"
            android:textColor="#FFEB3B"
            android:textSize="26dp" />
    </LinearLayout>


</LinearLayout>

java文件:?

package com.example.mycalculater;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {
    Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,badd,bsub,bmul,bdiv,bdot,bequal,b10,b12;
    TextView ans;
    double var1,var2;
    boolean add,sub,mul,div,n10;
    boolean sq = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        b1= (Button) findViewById(R.id.btn1);
        b2= (Button) findViewById(R.id.btn2);
        b3= (Button) findViewById(R.id.btn3);
        b4= (Button) findViewById(R.id.btn4);
        b5= (Button) findViewById(R.id.btn5);
        b6= (Button) findViewById(R.id.btn6);
        b7= (Button) findViewById(R.id.btn7);
        b8= (Button) findViewById(R.id.btn8);
        b9= (Button) findViewById(R.id.btn9);
        b0= (Button) findViewById(R.id.btn0);
        badd= (Button) findViewById(R.id.btnadd);
        bsub= (Button) findViewById(R.id.btnsub);
        bmul= (Button) findViewById(R.id.btnmul);
        bdiv= (Button) findViewById(R.id.btndiv);
        bdot= (Button) findViewById(R.id.btndot);
        bequal= (Button) findViewById(R.id.btnequel);
        b10= (Button) findViewById(R.id.btn10);
        b12= (Button) findViewById(R.id.btn12);
        ans = (TextView) findViewById(R.id.Answer);


        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(sq==true){
                    ans.setText(null);
                    ans.setText(ans.getText()+"1");
                    sq=false;
                }else
                    ans.setText(ans.getText()+"1");

            }
        });
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(sq==true){
                    ans.setText(null);
                    ans.setText(ans.getText()+"2");
                    sq=false;
                }else
                     ans.setText(ans.getText()+"2");
            }
        });
        b3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(sq==true){
                    ans.setText(null);
                    ans.setText(ans.getText()+"3");
                    sq=false;
                }else
                    ans.setText(ans.getText()+"3");
            }
        });
        b4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(sq==true){
                    ans.setText(null);
                    ans.setText(ans.getText()+"4");
                    sq=false;
            }else
                ans.setText(ans.getText()+"4");
            }
        });
        b5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(sq==true){
                    ans.setText(null);
                    ans.setText(ans.getText()+"5");
                    sq=false;
                }else
                ans.setText(ans.getText()+"5");
            }
        });
        b6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(sq==true){
                    ans.setText(null);
                    ans.setText(ans.getText()+"6");
                    sq=false;
                }else
                ans.setText(ans.getText()+"6");
            }
        });
        b7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(sq==true){
                    ans.setText(null);
                    ans.setText(ans.getText()+"7");
                    sq=false;
                }else
                ans.setText(ans.getText()+"7");
            }
        });
        b8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(sq==true){
                    ans.setText(null);
                    ans.setText(ans.getText()+"8");
                    sq=false;
                }else
                ans.setText(ans.getText()+"8");
            }
        });
        b9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(sq==true) {
                    ans.setText(null);
                    ans.setText(ans.getText()+"9");
                    sq=false;
                }else
                ans.setText(ans.getText()+"9");
            }
        });
        b0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(sq==true){
                    ans.setText(null);
                    ans.setText(ans.getText()+"0");
                    sq=false;
                }else
                ans.setText(ans.getText()+"0");
            }
        });
        bdot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    if(ans.getText().toString().contains("."))
                        ans.setText(ans.getText()+"");
                    else
                        ans.setText(ans.getText()+".");
                } catch (Exception e) {
                    ans.setText("出錯");
                }
            }
        });
        badd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    var1 = Double.parseDouble(ans.getText()+"");
                    add=true;
                    ans.setText(null);
                }catch (RuntimeException a){
                    ans.setText("錯誤");
                    sq=true;

                }
            }
        });
        bsub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    var1 = Double.parseDouble(ans.getText()+"");
                    sub=true;
                    ans.setText(null);
                }catch(RuntimeException a){
                    ans.setText("錯誤");
                    sq=true;
                }
            }
        });
        bmul.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    var1 = Double.parseDouble(ans.getText()+"");
                    mul=true;
                    ans.setText(null);
                }catch(RuntimeException a){
                    ans.setText("錯誤");
                    sq=true;
                }
            }
        });
        bdiv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*var1 = Double.parseDouble(ans.getText()+"");
                div=true;
                ans.setText(null);*/
                try {
                    var1 = Double.parseDouble(ans.getText()+"");
                    div=true;
                    ans.setText(null);
                }catch(RuntimeException a){
                    ans.setText("錯誤");
                    sq=true;
                }
            }
        });
        b10.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ans.setText(null);
                add=false;sub=false;mul=false;div=false;
            }
        });
        b12.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    String temp = "";
                    temp = ans.getText().toString().substring(0, ans.length() - 1);
                    ans.setText(temp + "");
                }catch(RuntimeException a){
                    ans.setText("錯誤");
                }
            }
        });
        bequal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    var2 = Double.parseDouble(ans.getText() + "");
                    if (add == true) {
                        ans.setText(var1 + var2 + "");
                        add = false;
                        sq = true;
                    }
                    if (sub == true) {
                        ans.setText(var1 - var2 + "");
                        sub = false;
                        sq = true;
                    }
                    if (mul == true) {
                        ans.setText(var1 * var2 + "");
                        mul = false;
                        sq = true;
                    }
                    if (div == true) {
                        ans.setText(var1 / var2 + "");
                        div = false;
                        sq = true;
                    }
                    if (n10 == true) {
                        ans.setText(0 + "");
                        n10 = false;
                        sq = true;
                    }
                }catch(RuntimeException a){
                }
                /*String temp="";
                temp=ans.getText().toString().substring(0,ans.length()-1);
                ans.setText(temp+"");*/
            }
        });

//        public void opratorCalc(String operatorNumber,String currentOprator)
//        {
//            if(TextUtils.isEmpty(lastOperators))
//            {
//                firstNumber = Double.parseDouble(operatorNumber);
//                return;
//            }






    }
}

到了這里,關(guān)于Android Studio制作一個簡單的計算器APP的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務器費用

相關(guān)文章

  • 初學編程 第一個小程序Android studio實現(xiàn)計算器功能

    源代碼下載:https://gitee.com/zha-yingying/calculator.git 1.建立一個新的Layout,我這里使用的是GridLayout(網(wǎng)格布局),提取屏幕寬度(方便后面設(shè)置子控件的寬度)GridLayout的特點是自定義網(wǎng)格布局有幾行幾列,我們可以將自控件自定義放在第幾行第幾列。 2.建立一個新的textview(文本

    2023年04月14日
    瀏覽(23)
  • 移動開發(fā)作業(yè)三:使用Android studio來實現(xiàn)簡單的計算器

    移動開發(fā)作業(yè)三:使用Android studio來實現(xiàn)簡單的計算器

    一.實驗要求 結(jié)合所學內(nèi)容利用Android studio來設(shè)計一個開發(fā)實例,這里去我選擇做一個簡易的計算器,可以初步實現(xiàn)加減乘除。 二.實驗功能 該計算器與我們平常手機上的計算器一樣,可以進行加減乘除操作。 三.實驗過程 1.首先是關(guān)于計算器的布局 在layout文件下的drawable文件

    2024年02月02日
    瀏覽(93)
  • 用Java制作簡單的計算器

    用Java制作簡單的計算器

    本篇文章主要是提供思路,當然文章末尾也又提供了源代碼。 代碼也是寫了幾天,重要的理解,不是直接復制粘貼就交作業(yè)了。 轉(zhuǎn)載請注明出處,尊重作者勞動成果。 目錄 界面的設(shè)計: 事件的響應: 計算: 詳細代碼如下: ?總結(jié): 要制作一個簡單的計算器,首先就是對

    2024年02月08日
    瀏覽(12)
  • 用python制作一個簡易計算器

    這是一個用Python制作簡單計算器的教程。你可以根據(jù)需要進行更多的改進,例如添加其他運算符或功能。 首先,我們需要創(chuàng)建一個簡單的用戶界面,用于顯示計算器的按鍵和結(jié)果。在Python中,我們可以使用 tkinter 庫來創(chuàng)建圖形用戶界面。創(chuàng)建一個新的Python文件,并將其命名為

    2024年02月11日
    瀏覽(20)
  • 制作一個簡易的計算器app

    制作一個簡易的計算器app

    github項目地址:https://github.com/13008451162/AndroidMoblieCalculator 筆者的Ui制作的制作的比較麻煩僅供參考,在這里使用了多個LinearLayout對屏幕進行了劃分。不建議大家這樣做最好使用GridLayout會更加快捷簡單 筆者大致劃分是這樣的: 使用了四個大框,在第四個大框里面有多個小框

    2024年02月15日
    瀏覽(31)
  • Qt 制作一個簡易的計算器

    Qt 制作一個簡易的計算器

    1.通過UI界面封裝出計算器的大致模型 進入設(shè)計頁面后,左側(cè)會有各種控件,可以將他們拖拽到你想編輯的窗口中,我們要做的是計算器,所以只用到很少幾個控件,我們最主要用到Push Button這個控件來做我們計算器的按鈕,lineEdit顯示數(shù)字,我們可以將它拖拽到窗口,然后就

    2024年02月05日
    瀏覽(22)
  • Android Studio簡易計算器

    Android Studio簡易計算器

    目錄 第一步,創(chuàng)建新項目 第二步,設(shè)計UI 第三步,實現(xiàn)計算邏輯 第四步,測試應用程序 隨著移動互聯(lián)網(wǎng)的普及,手機應用程序已經(jīng)成為人們生活中不可或缺的一部分。計算器是一類被廣泛使用的應用程序之一,因此學習如何開發(fā)一款簡易的計算器應用程序是學習Android Stu

    2024年02月08日
    瀏覽(21)
  • 【Android Studio】簡易計算器

    【Android Studio】簡易計算器

    簡易計算器要求: 1,操作簡單,易于掌握,界面簡單。 2.方便進行加,減,乘,除等操作。數(shù)字保留小數(shù)點后兩位。 3.包含小數(shù)點運算和輸入回退功能。 4.能夠進行多次疊加運算。 5.系統(tǒng)能夠進行多次疊加運算。 6.系統(tǒng)可以穩(wěn)定運行。 功能圖如下: 邏輯流程圖如下: 項目建

    2024年02月08日
    瀏覽(21)
  • python界面開發(fā)案例:制作一個計算器軟件

    python界面開發(fā)案例:制作一個計算器軟件

    前言 大家早好、午好、晚好吖 ? ~歡迎光臨本文章 在我們手機上大家都有一個計算器,對吧 那它這功能是怎么實現(xiàn)的呢? 今天我們在電腦上來實現(xiàn)一個電腦端計算器界面~ 開發(fā)環(huán)境: Python 3.8 / 編譯器 Pycharm 2021.2版本 / 編輯器 本文所有模塊環(huán)境源碼教程皆可點擊文章下方

    2023年04月16日
    瀏覽(100)
  • Java中規(guī)模軟件開發(fā)實訓——簡單計算器制作

    Java中規(guī)模軟件開發(fā)實訓——簡單計算器制作

    ? 博主: 命運之光 ?? 專欄: Python星辰秘典 ?? 專欄: web開發(fā)(html css js) ?? 專欄: Java經(jīng)典程序設(shè)計 ?? 博主的其他文章: 點擊進入博主的主頁 前言: 在現(xiàn)代社會中,計算器是我們生活中不可或缺的工具之一。它們可以輕松地進行各種數(shù)值計算,從簡單的加減乘除

    2024年02月12日
    瀏覽(31)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包