[JAVA] Anfänger in der App-Entwicklung haben versucht, eine Android-Rechner-App zu erstellen

Einführung

Ich habe eine Android-Taschenrechner-App mit Java erstellt, indem ich auf die folgende Website verwiesen habe. Ich habe Kotlin auf den folgenden Websites verwendet, aber da dies meine erste Erstellung einer Android-Anwendung ist, handelt es sich bei diesem Artikel um ein in Java geschriebenes Programm. Erstellen Sie eine Taschenrechner-App in Android Studio

Einfallsreichtum

Verwenden Sie den Bigdecimal-Typ anstelle des Double-Typs, um Berechnungsfehler zu vermeiden

Zuerst wurde der Berechnungsprozess mit dem Doppeltyp durchgeführt, aber wie auf der folgenden Seite gezeigt, ist ein Berechnungsfehler aufgetreten, sodass ich den Bigdecimal-Typ verwendet habe, um dies zu verhindern. Über die Ursache des Doppeltypfehlers ~ Ich habe Probleme mit binären Brüchen ~

Referenzseite

Programm

Design

Quellcode

styles.xml


<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

Das übergeordnete Attribut des Stils wurde in NoActionBar geändert. Weil die Rechner-App keine Aktionsleiste benötigt.

color.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- DefaultColorChoice -->
    <!--<color name="colorPrimary">#008577</color>-->
    <!--<color name="colorPrimaryDark">#00574B</color>-->
    <!--<color name="colorAccent">#D81B60</color>-->
    <color name="colorPrimary">#2196F3</color>
    <color name="colorPrimaryDark">#1976D2</color>
    <color name="colorAccent">#EF5350</color>
</resources>

Ändern Sie das Design in Ihre Lieblingsfarbe. Die folgenden Websites sind hilfreich für Farbkombinationen. Musterkollektion für Materialdesign-Farbkombinationen

strings.xml


<resources>
    <string name="app_name">Calculator</string>
    <string name="formula_text" />
</resources>

activity_calc.xml


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context=".CalcActivity">

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.4" />

    <TableLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <TextView
                android:id="@+id/tvFormula"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_span="4"
                android:layout_weight="1"
                android:paddingEnd="8dp"
                android:gravity="end"
                android:text="@string/formula_text"
                android:textSize="30sp" />
        </TableRow>

        <TableRow
            style="?android:attr/buttonBarStyle"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <Button
                android:id="@+id/btClear"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#e9e9e9"
                android:text="C"
                android:textColor="@color/colorPrimaryDark" />

            <Button
                android:id="@+id/btDivide"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#e9e9e9"
                android:text="÷"
                android:textColor="@color/colorPrimaryDark" />

            <Button
                android:id="@+id/btMultiply"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#e9e9e9"
                android:text="×"
                android:textColor="@color/colorPrimaryDark" />

            <Button
                android:id="@+id/btDelete"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#e9e9e9"
                android:text="DEL"
                android:textColor="@color/colorPrimaryDark" />
        </TableRow>

        <TableRow
            style="?android:attr/buttonBarStyle"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <Button
                android:id="@+id/btSeven"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="7"
                android:textColor="@color/colorPrimaryDark" />

            <Button
                android:id="@+id/btEight"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="8"
                android:textColor="@color/colorPrimaryDark" />

            <Button
                android:id="@+id/btNine"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="9"
                android:textColor="@color/colorPrimaryDark" />

            <Button
                android:id="@+id/btPercent"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#e9e9e9"
                android:text="%"
                android:textColor="@color/colorPrimaryDark" />
        </TableRow>

        <TableRow
            style="?android:attr/buttonBarStyle"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <Button
                android:id="@+id/btFour"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="4"
                android:textColor="@color/colorPrimaryDark" />

            <Button
                android:id="@+id/btFive"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="5"
                android:textColor="@color/colorPrimaryDark" />

            <Button
                android:id="@+id/btSix"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="6"
                android:textColor="@color/colorPrimaryDark" />

            <Button
                android:id="@+id/btSubtract"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#e9e9e9"
                android:text="-"
                android:textColor="@color/colorPrimaryDark" />
        </TableRow>

        <TableRow
            style="?android:attr/buttonBarStyle"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <Button
                android:id="@+id/btOne"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="1"
                android:textColor="@color/colorPrimaryDark" />

            <Button
                android:id="@+id/btTwo"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="2"
                android:textColor="@color/colorPrimaryDark" />

            <Button
                android:id="@+id/btThree"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="3"
                android:textColor="@color/colorPrimaryDark" />

            <Button
                android:id="@+id/btAdd"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#e9e9e9"
                android:text="+"
                android:textColor="@color/colorPrimaryDark" />
        </TableRow>

        <TableRow
            style="?android:attr/buttonBarStyle"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <Button
                android:id="@+id/btPlusMinus"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1" />
            <Button
                android:id="@+id/btZero"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="0"
                android:textColor="@color/colorPrimaryDark" />

            <Button
                android:id="@+id/btPoint"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="."
                android:textColor="@color/colorPrimaryDark" />

            <Button
                android:id="@+id/btEqual"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="="
                android:textColor="#ffffff"
                android:background="@color/colorPrimaryDark"/>
        </TableRow>
    </TableLayout>

</android.support.constraint.ConstraintLayout>

Die Breite und Höhe sind richtig ausgelegt, um nicht fest codiert zu werden.

CalcActivity.java


package com.yagiyagi21.android.calculator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import static android.webkit.ConsoleMessage.MessageLevel.LOG;

public class CalcActivity extends AppCompatActivity {

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

        List<Button> buttonList = new ArrayList<>();
        //Holen Sie sich die Nummerntaste
        buttonList.add((Button) findViewById(R.id.btZero));
        buttonList.add((Button) findViewById(R.id.btOne));
        buttonList.add((Button) findViewById(R.id.btTwo));
        buttonList.add((Button) findViewById(R.id.btThree));
        buttonList.add((Button) findViewById(R.id.btFour));
        buttonList.add((Button) findViewById(R.id.btFive));
        buttonList.add((Button) findViewById(R.id.btSix));
        buttonList.add((Button) findViewById(R.id.btSeven));
        buttonList.add((Button) findViewById(R.id.btEight));
        buttonList.add((Button) findViewById(R.id.btNine));
        //Holen Sie sich die Symbolschaltfläche
        buttonList.add((Button) findViewById(R.id.btAdd));
        buttonList.add((Button) findViewById(R.id.btSubtract));
        buttonList.add((Button) findViewById(R.id.btMultiply));
        buttonList.add((Button) findViewById(R.id.btDivide));
        buttonList.add((Button) findViewById(R.id.btEqual));
        //Holen Sie sich andere Schaltflächen
        buttonList.add((Button) findViewById(R.id.btClear));
        buttonList.add((Button) findViewById(R.id.btDelete));
        buttonList.add((Button) findViewById(R.id.btPoint));
        buttonList.add((Button) findViewById(R.id.btPlusMinus));
        buttonList.add((Button) findViewById(R.id.btPercent));

        ButtonListener listener = new ButtonListener();

        for(Button button : buttonList){
            button.setOnClickListener(listener);
        }
    }

    private class ButtonListener implements View.OnClickListener {

        private List<BigDecimal> _numList = new ArrayList<>();
        private List<Character> _opeList = new ArrayList<>();
        private String _inputValue = "";

        @Override
        public void onClick(View view) {
            TextView tvFormula = findViewById(R.id.tvFormula);

            //Definieren Sie die Verarbeitung für jede Schaltfläche
            int btId = view.getId();
            char inputChar;
            switch (btId) {
                //Für Zifferntasten
                case R.id.btZero:
                    inputChar = '0';
                    addTextView(tvFormula, inputChar);
                    _inputValue += inputChar;
                    break;
                case R.id.btOne:
                    inputChar = '1';
                    addTextView(tvFormula, inputChar);
                    _inputValue += inputChar;
                    break;
                case R.id.btTwo:
                    inputChar = '2';
                    addTextView(tvFormula, inputChar);
                    _inputValue += inputChar;
                    break;
                case R.id.btThree:
                    inputChar = '3';
                    addTextView(tvFormula, inputChar);
                    _inputValue += inputChar;
                    break;
                case R.id.btFour:
                    inputChar = '4';
                    addTextView(tvFormula, inputChar);
                    _inputValue += inputChar;
                    break;
                case R.id.btFive:
                    inputChar = '5';
                    addTextView(tvFormula, inputChar);
                    _inputValue += inputChar;
                    break;
                case R.id.btSix:
                    inputChar = '6';
                    addTextView(tvFormula, inputChar);
                    _inputValue += inputChar;
                    break;
                case R.id.btSeven:
                    inputChar = '7';
                    addTextView(tvFormula, inputChar);
                    _inputValue += inputChar;
                    break;
                case R.id.btEight:
                    inputChar = '8';
                    addTextView(tvFormula, inputChar);
                    _inputValue += inputChar;
                    break;
                case R.id.btNine:
                    inputChar = '9';
                    addTextView(tvFormula, inputChar);
                    _inputValue += inputChar;
                    break;
                //Für Symbolschaltflächen
                case R.id.btAdd:
                    inputChar = '+';
                    if(!(_inputValue.equals(""))) {
                        addList(tvFormula, _inputValue, inputChar);
                    }
                    break;
                case R.id.btSubtract:
                    inputChar = '-';
                    if(!(_inputValue.equals(""))) {
                        addList(tvFormula, _inputValue, inputChar);
                    }
                    break;
                case R.id.btMultiply:
                    inputChar = '×';
                    if(!(_inputValue.equals(""))) {
                        addList(tvFormula, _inputValue, inputChar);
                    }
                    break;
                case R.id.btDivide:
                    inputChar = '÷';
                    if(!(_inputValue.equals(""))) {
                        addList(tvFormula, _inputValue, inputChar);
                    }
                    break;
                case R.id.btEqual:
                    inputChar = '=';
                    if(!(_inputValue.equals(""))) {
                        addList(tvFormula, _inputValue, inputChar);
                    }
                    String result = calculate().toString();
                    tvFormula.setText(result);
                    _inputValue = result;

                    _numList.clear();
                    _opeList.clear();
                    break;
                //Verarbeitung für andere Schaltflächen
                case R.id.btClear:
                    tvFormula.setText("");
                    _numList.clear();
                    _opeList.clear();
                    _inputValue= "";
                    break;
                case R.id.btDelete:
                    String formulaStr = tvFormula.getText().toString();
                    char formulaStrLastChar = formulaStr.charAt(formulaStr.length() - 1);

                    if (isFourArithmeticOpe(formulaStrLastChar)) {
                        _opeList.remove(_opeList.size() - 1);
                    }
                    if (!formulaStr.isEmpty()) {
                        tvFormula.setText(formulaStr.subSequence(0, formulaStr.length() - 1));
                    }
                    if (!_inputValue.isEmpty()) {
                        _inputValue = _inputValue.substring(0, _inputValue.length() - 1);
                    }
                    break;
                case R.id.btPoint:
                    inputChar = '.';
                    addTextView(tvFormula, inputChar);
                    _inputValue += inputChar;
                    break;
                case R.id.btPlusMinus:
                    break;
                case R.id.btPercent:
                    break;
            }
        }

        private void addList(TextView tvFormula, String inputValue, char ope) {
            addTextView(tvFormula, ope);
            _numList.add(new BigDecimal(inputValue));
            _opeList.add(ope);
            _inputValue = "";
        }

        private void addTextView(TextView textView, char addStr) {
            textView.setText(textView.getText().toString() + addStr);
        }

        private BigDecimal calculate() {
            int i = 0;

            while(i < _opeList.size()) {
                if(_opeList.get(i) == '×' | _opeList.get(i) == '÷') {
                    BigDecimal resultMultiDiv = _opeList.get(i) == '×' ? _numList.get(i).multiply(_numList.get(i+1)) : _numList.get(i).divide(_numList.get(i+1));

                    _numList.set(i, resultMultiDiv);
                    _numList.remove(i+1);
                    _opeList.remove(i);
                    i--;
                }
                else if(_opeList.get(i) == '-') {
                    _opeList.set(i, '+');
                    _numList.set(i+1, _numList.get(i+1).negate());
                }
                i++;
            }

            BigDecimal result = new BigDecimal("0");
            for(BigDecimal j : _numList) {
                result = result.add(j);
            }

            return result;
        }

        private boolean isFourArithmeticOpe(char c) {
            if(c == '+' | c == '-' | c == '*' | c == '/') return true;
            return false;
        }
    }
}

Recommended Posts

Anfänger in der App-Entwicklung haben versucht, eine Android-Rechner-App zu erstellen
Erstelle eine Android App. (Tag 5)
Erstelle eine Android App. (Erster Tag)
So erstellen Sie eine App mit Tensorflow mit Android Studio
Ich möchte eine ios.android App machen
Ich habe versucht, eine Android-Anwendung mit MVC zu erstellen (Java)
Wie man einen imposanten Android-Musikplayer macht
[Einführung in die Android App-Entwicklung] Machen wir einen Zähler
Erstellen Sie eine Instagram-Klon-App ④
Startschuss für die persönliche Entwicklung der Android-App
Einführung in die Android App-Entwicklung
Erstellen Sie eine Instagram-Klon-App ②
Erstellen Sie eine Instagram-Klon-App ③
Erstellen Sie eine Instagram-Klon-App ①
Ich habe versucht, innerhalb von 3 Monaten einen Antrag von unerfahren zu stellen
Ich habe versucht, Ben zu einer leicht verständlichen GIF-Animation zu machen
Ich habe versucht, eine App zu erstellen, mit der Sie nach Genre posten und chatten können ~ Übersicht über die App ~
Was Anfänger in der Android-Entwicklung vor der Veröffentlichung der App in 2 Wochen getan haben
Ich habe versucht, eine einfache Karten-App in Android Studio zu erstellen
Ich habe versucht, mit Eclipse + Tomcat eine http2-Entwicklungsumgebung zu erstellen
Hinweise für Anfänger in der Android-Anwendungsentwicklung
Ich habe versucht, mithilfe von Routing-Verschachtelung eine beliebige URL zu erstellen
Einführung in die Android App-Entwicklung 1 Installieren von JDK und Android Studio für Mac
[Android] So erstellen Sie ein Dialogfragment
Ich habe versucht, eine einfache Gesichtserkennungs-Android-Anwendung mit OpenCV zu erstellen
Versuch und Irrtum, um nationale Feiertage in der Android-App-Entwicklung anzuzeigen. Teil 2
So erstellen Sie eine App mit einem Plug-In-Mechanismus [C # und Java]
Erstellen Sie mit Android Studio eine ausführbare JAR-Datei
Ein Memorandum für Anfänger der Android-Anwendungsentwicklung
Einführung von Vue.js in eine vorhandene Rails-App
[Java] Anfänger wollen Dating machen. 1
Ich habe eine Taschenrechner-App für Android erstellt
Einführung von Vuetify in vorhandene Rails-Apps
Schnelle Anfänger versuchten, Mikrowellenlogik zu implementieren!
[Android] Ich habe mit ListView + Bottom Sheet einen Materiallistenbildschirm erstellt
Ich mache eine Android-App und sie steckt voller Fehler und wie man sie löst
[3.] Mahjong App Entwicklung von 0 mit zwei Anfängern
Riot (Chat App) Entwicklung (Einstellungen) in Android Studio
Ich habe versucht, eine Standardauthentifizierung mit Java durchzuführen
Android App: Versuchen Sie, Ereignisse und Hörer zusammenzufassen
[1.] Mahjong App Entwicklung von 0 mit zwei Anfängern
Ich habe eine Android-App für den MiRm-Dienst erstellt
Erstellen Sie eine Software, die den Android-Bildschirm auf einen PC 1 spiegelt
Ich habe versucht, eine LINE-Klon-App zu erstellen
Android Studio-Entwicklung zum ersten Mal (für Anfänger)
Wie erstelle ich Unity Native Plugin (Android-Version)
[2.] Mahjong App Entwicklung von 0 mit zwei Anfängern
Rails6.0 ~ So erstellen Sie eine umweltfreundliche Entwicklungsumgebung
Schnelle Anfänger haben versucht, die Automatenlogik zu implementieren!
Rails-Anfänger haben versucht, mit RSpec zu beginnen
Ich habe versucht, eine Anwendung in 2 Sprachen zu entwickeln
Vom Aufbau einer AWS-Cloud-Umgebung bis zur Bereitstellung einer Spring Boot-App (für Anfänger)
Ich habe die Android-Entwicklung, RGB, Hexadezimalzahl und Farbimplementierung aus der Ressourcendatei zusammengefasst