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

【Android】控件與布局入門 - 簡(jiǎn)易計(jì)算器

這篇具有很好參考價(jià)值的文章主要介紹了【Android】控件與布局入門 - 簡(jiǎn)易計(jì)算器。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

目錄

1. 基礎(chǔ)開(kāi)發(fā)環(huán)境

2. 計(jì)算器的布局和相關(guān)按鈕

3. 計(jì)算器的主要運(yùn)算邏輯

4. APK 文件

5. 項(xiàng)目源碼


1. 基礎(chǔ)開(kāi)發(fā)環(huán)境

JDK:JDK17

Android Studio:Android Studio Giraffe | 2022.3.1

Android SDK:Android API 34

Gradle: gradle-8.0-bin.zip

2. 計(jì)算器的布局和相關(guān)按鈕

使用 LinearLayout 和 GridLayout 實(shí)現(xiàn)計(jì)算器的交互前端。

layout 文件如下

<?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:background="#EEEEEE"
    android:orientation="vertical"
    android:padding="5dp">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="@string/simple_calculator"
                android:textColor="@color/black"
                android:textSize="20sp"/>

            <TextView
                android:id="@+id/tv_result"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/white"
                android:lines="3"
                android:text="@string/filler"
                android:gravity="end|bottom"
                android:textColor="@color/black"
                android:textSize="25sp"/>

            <GridLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:columnCount="4"
                android:rowCount="5">

                <Button
                    android:id="@+id/btn_clear_entry"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/button_height"
                    android:layout_columnWeight="1"
                    android:gravity="center"
                    android:text="@string/cancel"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size"/>

                <Button
                    android:id="@+id/btn_divide"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/button_height"
                    android:layout_columnWeight="1"
                    android:gravity="center"
                    android:text="@string/divide"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size"/>

                <Button
                    android:id="@+id/btn_multiply"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/button_height"
                    android:layout_columnWeight="1"
                    android:gravity="center"
                    android:text="@string/multiply"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size"/>

                <Button
                    android:id="@+id/btn_clear"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/button_height"
                    android:layout_columnWeight="1"
                    android:gravity="center"
                    android:text="@string/delete"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size"/>

                <Button
                    android:id="@+id/btn_seven"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/button_height"
                    android:layout_columnWeight="1"
                    android:gravity="center"
                    android:text="@string/seven"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size"/>

                <Button
                    android:id="@+id/btn_eight"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/button_height"
                    android:layout_columnWeight="1"
                    android:gravity="center"
                    android:text="@string/eight"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size"/>

                <Button
                    android:id="@+id/btn_nine"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/button_height"
                    android:layout_columnWeight="1"
                    android:gravity="center"
                    android:text="@string/nine"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size"/>

                <Button
                    android:id="@+id/btn_plus"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/button_height"
                    android:layout_columnWeight="1"
                    android:gravity="center"
                    android:text="@string/plus"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size"/>

                <Button
                    android:id="@+id/btn_four"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/button_height"
                    android:layout_columnWeight="1"
                    android:gravity="center"
                    android:text="@string/four"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size"/>

                <Button
                    android:id="@+id/btn_five"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/button_height"
                    android:layout_columnWeight="1"
                    android:gravity="center"
                    android:text="@string/five"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size"/>

                <Button
                    android:id="@+id/btn_six"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/button_height"
                    android:layout_columnWeight="1"
                    android:gravity="center"
                    android:text="@string/six"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size"/>

                <Button
                    android:id="@+id/btn_minus"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/button_height"
                    android:layout_columnWeight="1"
                    android:gravity="center"
                    android:text="@string/minus"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size"/>

                <Button
                    android:id="@+id/btn_one"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/button_height"
                    android:layout_columnWeight="1"
                    android:gravity="center"
                    android:text="@string/one"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size"/>

                <Button
                    android:id="@+id/btn_two"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/button_height"
                    android:layout_columnWeight="1"
                    android:gravity="center"
                    android:text="@string/two"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size"/>

                <Button
                    android:id="@+id/btn_three"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/button_height"
                    android:layout_columnWeight="1"
                    android:gravity="center"
                    android:text="@string/three"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size"/>

                <Button
                    android:id="@+id/btn_sqrt"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/button_height"
                    android:layout_columnWeight="1"
                    android:gravity="center"
                    android:text="@string/sqrt"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size"/>

                <Button
                    android:id="@+id/btn_reciprocal"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/button_height"
                    android:layout_columnWeight="1"
                    android:gravity="center"
                    android:text="@string/reciprocal"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size"/>

                <Button
                    android:id="@+id/btn_zero"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/button_height"
                    android:layout_columnWeight="1"
                    android:gravity="center"
                    android:text="@string/zero"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size"/>

                <Button
                    android:id="@+id/btn_dot"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/button_height"
                    android:layout_columnWeight="1"
                    android:gravity="center"
                    android:text="@string/dot"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size"/>

                <Button
                    android:id="@+id/btn_equal"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/button_height"
                    android:layout_columnWeight="1"
                    android:gravity="center"
                    android:text="@string/equal"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size"/>

            </GridLayout>

        </LinearLayout>

    </ScrollView>


</LinearLayout>

相關(guān) values 如下:

  • strings.xml
<resources>
    <string name="app_name">calculator</string>
    <string name="simple_calculator">計(jì)算器</string>
    <string name="filler">0</string>
    <string name="cancel">CE</string>
    <string name="divide">÷</string>
    <string name="multiply">×</string>
    <string name="plus">+</string>
    <string name="minus">-</string>
    <string name="delete">C</string>
    <string name="zero">0</string>
    <string name="one">1</string>
    <string name="two">2</string>
    <string name="three">3</string>
    <string name="four">4</string>
    <string name="five">5</string>
    <string name="six">6</string>
    <string name="seven">7</string>
    <string name="eight">8</string>
    <string name="nine">9</string>
    <string name="sqrt">√</string>
    <string name="dot">.</string>
    <string name="equal">=</string>
    <string name="reciprocal">1/X</string>
</resources>
  • dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="button_font_size">30sp</dimen>
    <dimen name="button_height">75dp</dimen>
</resources>

實(shí)際效果:

【Android】控件與布局入門 - 簡(jiǎn)易計(jì)算器,Android,android

?

3. 計(jì)算器的主要運(yùn)算邏輯

package com.example.calculator;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tvResult;

    private String firstNum = "";
    private String secondNum = "";
    private String operator = "";
    private String result = "";
    private String showText = "";

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

        // 從布局文件中獲取文本視圖
        tvResult = findViewById(R.id.tv_result);

        // 為所有按鈕注冊(cè)點(diǎn)擊監(jiān)聽(tīng)器
        findViewById(R.id.btn_clear_entry).setOnClickListener(this);
        findViewById(R.id.btn_divide).setOnClickListener(this);
        findViewById(R.id.btn_multiply).setOnClickListener(this);
        findViewById(R.id.btn_clear).setOnClickListener(this);
        findViewById(R.id.btn_seven).setOnClickListener(this);
        findViewById(R.id.btn_eight).setOnClickListener(this);
        findViewById(R.id.btn_nine).setOnClickListener(this);
        findViewById(R.id.btn_plus).setOnClickListener(this);
        findViewById(R.id.btn_four).setOnClickListener(this);
        findViewById(R.id.btn_five).setOnClickListener(this);
        findViewById(R.id.btn_six).setOnClickListener(this);
        findViewById(R.id.btn_minus).setOnClickListener(this);
        findViewById(R.id.btn_one).setOnClickListener(this);
        findViewById(R.id.btn_two).setOnClickListener(this);
        findViewById(R.id.btn_three).setOnClickListener(this);
        findViewById(R.id.btn_sqrt).setOnClickListener(this);
        findViewById(R.id.btn_reciprocal).setOnClickListener(this);
        findViewById(R.id.btn_zero).setOnClickListener(this);
        findViewById(R.id.btn_dot).setOnClickListener(this);
        findViewById(R.id.btn_equal).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        String inputText = ((TextView) view).getText().toString();

        List<Integer> fourOperations = Arrays.asList(R.id.btn_plus, R.id.btn_minus, R.id.btn_multiply, R.id.btn_divide);
        int id = view.getId();

        if (id == R.id.btn_clear) {
            // 清除
            clear();
        } else if (id == R.id.btn_clear_entry) {
            // 清除輸入
            clearEntryOperate();
        } else if (fourOperations.contains(id)) {
            operator = inputText;
            refreshShowText(showText + operator);
            // 四則運(yùn)算
        } else if (id == R.id.btn_equal) {
            // 等號(hào)
            if (!secondNum.equals("")) {
                double calculateResult = calculateFour();
                refreshOperateResult(String.valueOf(calculateResult));
                refreshShowText(result);
            }
        } else if (id == R.id.btn_sqrt) {
            // 根號(hào)
            double sqrtResult = Math.sqrt(Double.parseDouble(firstNum));
            refreshOperateResult(String.valueOf(sqrtResult));
            refreshShowText(result);
        } else if (id == R.id.btn_reciprocal) {
            // 倒數(shù)
            double reciprocalResult = 1.0 / Double.parseDouble(firstNum);
            refreshOperateResult(String.valueOf(reciprocalResult));
            refreshShowText(result);
        } else {
            // 其它
            if (result.length() > 0 && operator.equals("")) {
                clear();
            }

            if (operator.equals("")) {
                firstNum += inputText;
            } else {
                secondNum += inputText;
            }
            if (showText.equals("0") && !inputText.equals(".")) {
                refreshShowText(inputText);
            } else {
                refreshShowText(showText + inputText);
            }

        }
    }

    private double calculateFour() {
        switch (operator) {
            case "+":
                return Double.parseDouble(firstNum) + Double.parseDouble(secondNum);
            case "-":
                return Double.parseDouble(firstNum) - Double.parseDouble(secondNum);
            case "×":
                return Double.parseDouble(firstNum) * Double.parseDouble(secondNum);
            default:
                return Double.parseDouble(firstNum) / Double.parseDouble(secondNum);
        }
    }

    private void clear() {
        refreshOperateResult("");
        refreshShowText("0");
    }

    private void refreshOperateResult(String newResult) {
        result = newResult;
        firstNum = result;
        secondNum = "";
        operator = "";

    }

    private void clearEntryOperate() {
        String tmp = showText.substring(0, showText.length() - 1);
        if (tmp.equals("")) {
            tmp = "0";
        }
        refreshShowText(tmp);
    }

    // 刷新顯示文本
    private void refreshShowText(String text) {
        showText = text;
        integerRemoveDot();
        tvResult.setText(showText);
    }

    private void integerRemoveDot() {
        int start = showText.indexOf(".");
        if (start != -1) {
            int end = showText.length();
            String value = showText.substring(start + 1, end);
            if (Double.parseDouble(value) == 0.0) {
                showText = showText.substring(0, start);
            }
        }
    }
}

4. APK 文件

構(gòu)建好的 APK 文件見(jiàn)文件開(kāi)頭,可直接下載安裝。

5. 項(xiàng)目源碼

https://gitee.com/hl0929/calculator文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-623231.html

到了這里,關(guān)于【Android】控件與布局入門 - 簡(jiǎn)易計(jì)算器的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

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

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 學(xué)會(huì)使用Android Studio網(wǎng)格布局制作計(jì)算器界面

    學(xué)會(huì)使用Android Studio網(wǎng)格布局制作計(jì)算器界面

    1.1GridLayout布局使用虛細(xì)線將布局劃分為行、列和單元格,也支持一個(gè)控件在行、列上都有交錯(cuò)排列。 1.2可以自己設(shè)置布局中組件的排列方式 1.3可以自定義網(wǎng)格布局有多少行、多少列 1.4可以直接設(shè)置組件位于某行某列 1.5可以設(shè)置組件橫跨幾行或者幾列 基于-? Empty Activity 模板

    2024年02月08日
    瀏覽(25)
  • android計(jì)算器界面布局線性布局跨2行,使用Kotlin高效地開(kāi)發(fā)Android App(一,GitHub標(biāo)星3.2K

    android計(jì)算器界面布局線性布局跨2行,使用Kotlin高效地開(kāi)發(fā)Android App(一,GitHub標(biāo)星3.2K

    get(url).placeholder(R.drawable.shape_default_round_bg) .error(R.drawable.shape_default_round_bg) // .apply(RequestOptions.bitmapTransform(RoundedCornersTransformation(DisplayUtil.dp2px(context, 6f), 0))) .transform(RoundedCornersTransformation(DisplayUtil.dp2px(context, 6f), 0)) .into(this) } /** 占位符圓形 */ fun ImageView.loadCircle(url: Drawable) {

    2024年04月11日
    瀏覽(25)
  • Android Studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器(帶橫豎屏,深色淺色模式,更該按鈕顏色,selector,style的使用)

    Android Studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器(帶橫豎屏,深色淺色模式,更該按鈕顏色,selector,style的使用)

    目錄 前言 運(yùn)行結(jié)果: 運(yùn)行截屏(p50e) ?apk文件 源碼文件 ?項(xiàng)目結(jié)構(gòu) 總覽 MainActivity.java drawable 更改圖標(biāo)的方法: blackbutton.xml bluebuttons.xml greybutton.xml orangebuttons.xml whitebutton.xml layout 布局文件 ?豎屏: 橫屏: values ? ? ? ? colors.xml strings.xml styles 淺色模式 深色模式 themes.xml

    2024年02月06日
    瀏覽(29)
  • JAVA制作的簡(jiǎn)易計(jì)算器——傻瓜計(jì)算器

    JAVA制作的簡(jiǎn)易計(jì)算器——傻瓜計(jì)算器

    用JAVA編寫的傻瓜計(jì)算器 作用: 1.可以實(shí)現(xiàn)加法、減法、乘法、除法簡(jiǎn)單運(yùn)算且是單一運(yùn)算,不可混合使用。 2.CE為清除鍵 3.沒(méi)有小數(shù)點(diǎn)O(∩_∩)O 思路: 創(chuàng)建JFrame窗口,設(shè)置標(biāo)題,創(chuàng)建JButton,創(chuàng)建文本框JTextField用作顯示。 先定義各種按鈕類型,用作成員。定義窗口方法對(duì)窗口

    2024年02月11日
    瀏覽(18)
  • java 簡(jiǎn)易計(jì)算器

    java 簡(jiǎn)易計(jì)算器

    1.使用Java圖形界面組件設(shè)計(jì)軟件,界面如圖所示。 2.軟件能夠滿足基本的“加、減、乘、除”等運(yùn)算要求。 3.程序代碼清晰,語(yǔ)法規(guī)范,結(jié)構(gòu)合理,邏輯正確。 先分析,計(jì)算器大概是由三個(gè)大部分組成的:菜單欄,顯示框,按鈕。 所以定義一個(gè)類cal繼承JFrame。 我們定義完后

    2024年02月01日
    瀏覽(26)
  • QT 簡(jiǎn)易計(jì)算器

    QT 簡(jiǎn)易計(jì)算器

    2024年02月09日
    瀏覽(24)
  • JAVA簡(jiǎn)易計(jì)算器

    JAVA簡(jiǎn)易計(jì)算器

    1.C是清除鍵,功能是將之前所輸入的數(shù)字、計(jì)算結(jié)果等信息全部歸零 2.CE,清除當(dāng)前輸入的數(shù)據(jù)或符號(hào) 3.單擊MS存儲(chǔ)當(dāng)前顯示值,可以理解為放到存儲(chǔ)區(qū) 4.單擊MC清除存儲(chǔ)區(qū)數(shù)值 5.單擊MR將存儲(chǔ)區(qū)數(shù)據(jù)顯示到屏幕上 6.M+:當(dāng)前顯示的數(shù)與存儲(chǔ)區(qū)的數(shù)相加 7.M-:當(dāng)前顯示的數(shù)與存儲(chǔ)

    2024年02月09日
    瀏覽(26)
  • Java計(jì)算器簡(jiǎn)易代碼

    我寫的計(jì)算器 網(wǎng)上搜的進(jìn)階版本 拿走不謝!

    2024年02月11日
    瀏覽(23)
  • C# 制作簡(jiǎn)易計(jì)算器

    C# 制作簡(jiǎn)易計(jì)算器

    前言:環(huán)境是vs 2022 1、打開(kāi)vs2022后,右邊導(dǎo)航欄選擇創(chuàng)建新項(xiàng)目。 ?2、選擇Windows窗體應(yīng)用(.net? Framework) ?3、進(jìn)入配置新項(xiàng)目界面(項(xiàng)目名稱和位置可自行修改)點(diǎn)擊創(chuàng)建 ?4、窗體From1即為我們要要編輯的位置 ?5、在窗體中添加對(duì)應(yīng)的工具 6、并在對(duì)應(yīng)的屬性窗口為其修改

    2024年02月08日
    瀏覽(30)
  • MFC基于對(duì)話框——仿照Windows計(jì)算器制作C++簡(jiǎn)易計(jì)算器

    MFC基于對(duì)話框——仿照Windows計(jì)算器制作C++簡(jiǎn)易計(jì)算器

    目錄 一、界面設(shè)計(jì) 二、設(shè)置成員變量 三、初始化成員變量? 四、初始化對(duì)話框 ?五、添加控件代碼 1.各個(gè)數(shù)字的代碼(0~9) 2.清除功能的代碼 3.退格功能的代碼 4.加減乘除功能的代碼 5.小數(shù)點(diǎn)功能的代碼 6.正負(fù)號(hào)功能的代碼 7.等于功能的代碼 六、源碼領(lǐng)取方式 制作好之后

    2024年02月05日
    瀏覽(97)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

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

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包