2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Android Studio 電卓アプリ制作(動画有り)

Last updated at Posted at 2021-03-03

出来るだけコードが少なくなるように電卓アプリを作成しました。
改善点などご教授いただけますと幸いです。
YouTubeに作っている動画をアップしています。
https://www.youtube.com/watch?v=1c0LvmCW5EU&list=PLhg2PHSq8bjjjBpOPll39ZAUgtbnlDOvU

strings.xml
<resources>
    <string name="app_name">CalculateApp</string>
    <string name="btn_zero">0</string>
    <string name="btn_one">1</string>
    <string name="btn_two">2</string>
    <string name="btn_three">3</string>
    <string name="btn_four">4</string>
    <string name="btn_five">5</string>
    <string name="btn_six">6</string>
    <string name="btn_seven">7</string>
    <string name="btn_eight">8</string>
    <string name="btn_nine">9</string>
    <string name="btn_plus">+</string>
    <string name="btn_minus">-</string>
    <string name="btn_times">×</string>
    <string name="btn_divide">÷</string>
    <string name="btn_clear">C</string>
    <string name="btn_point">.</string>
    <string name="btn_equal">=</string>
</resources>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="7"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text_formula"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="right"
        android:textSize="36sp"></TextView>

    <TextView
        android:id="@+id/text_result"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="right"
        android:textSize="36sp"
        android:textStyle="bold"></TextView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="4">

        <Space
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"></Space>

        <Space
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"></Space>

        <Space
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"></Space>

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:text="@string/btn_clear"
            android:onClick="tapClear"
            android:textSize="36sp"></Button>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="4">

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:text="@string/btn_seven"
            android:onClick="tapSeven"
            android:textSize="36sp"></Button>

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:text="@string/btn_eight"
            android:onClick="tapEight"
            android:textSize="36sp"></Button>

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:text="@string/btn_nine"
            android:onClick="tapNine"
            android:textSize="36sp"></Button>

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:text="@string/btn_divide"
            android:onClick="tapDivide"
            android:textSize="36sp"></Button>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="4">

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:text="@string/btn_four"
            android:onClick="tapFour"
            android:textSize="36sp"></Button>

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:text="@string/btn_five"
            android:onClick="tapFive"
            android:textSize="36sp"></Button>

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:text="@string/btn_six"
            android:onClick="tapSix"
            android:textSize="36sp"></Button>

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:text="@string/btn_times"
            android:onClick="tapTimes"
            android:textSize="36sp"></Button>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="4">

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:text="@string/btn_one"
            android:onClick="tapOne"
            android:textSize="36sp"></Button>

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:text="@string/btn_two"
            android:onClick="tapTwo"
            android:textSize="36sp"></Button>

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:text="@string/btn_three"
            android:onClick="tapThree"
            android:textSize="36sp"></Button>

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:text="@string/btn_minus"
            android:onClick="tapMinus"
            android:textSize="36sp"></Button>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="4">

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:text="@string/btn_zero"
            android:onClick="tapZero"
            android:textSize="36sp"></Button>

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:text="@string/btn_point"
            android:onClick="tapPoint"
            android:textSize="36sp"></Button>

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:text="@string/btn_equal"
            android:onClick="tapEqual"
            android:textSize="36sp"></Button>

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:text="@string/btn_plus"
            android:onClick="tapPlus"
            android:textSize="36sp"></Button>
    </LinearLayout>

</LinearLayout>
MainActivity.java
package com.example.calculateapp;

import androidx.appcompat.app.AppCompatActivity;

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

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

public class MainActivity extends AppCompatActivity {

    TextView formula, result;
    List<String> symbols;

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

        formula = findViewById(R.id.text_formula);
        result = findViewById(R.id.text_result);
        symbols = new ArrayList<String>();
    }

    public void tapZero(View view) {
        formula.setText(formula.getText().toString() + "0");
    }

    public void tapOne(View view) {
        formula.setText(formula.getText().toString() + "1");
    }

    public void tapTwo(View view) {
        formula.setText(formula.getText().toString() + "2");
    }

    public void tapThree(View view) {
        formula.setText(formula.getText().toString() + "3");
    }

    public void tapFour(View view) {
        formula.setText(formula.getText().toString() + "4");
    }

    public void tapFive(View view) {
        formula.setText(formula.getText().toString() + "5");
    }

    public void tapSix(View view) {
        formula.setText(formula.getText().toString() + "6");
    }

    public void tapSeven(View view) {
        formula.setText(formula.getText().toString() + "7");
    }

    public void tapEight(View view) {
        formula.setText(formula.getText().toString() + "8");
    }

    public void tapNine(View view) {
        formula.setText(formula.getText().toString() + "9");
    }

    public void tapPoint(View view) {
        formula.setText(formula.getText().toString() + ".");
    }

    public void tapPlus(View view) {
        formula.setText(formula.getText().toString() + "+");
        symbols.add("+");
    }

    public void tapMinus(View view) {
        formula.setText(formula.getText().toString() + "-");
        symbols.add("-");
    }

    public void tapTimes(View view) {
        formula.setText(formula.getText().toString() + "×");
        symbols.add("×");
    }

    public void tapDivide(View view) {
        formula.setText(formula.getText().toString() + "÷");
        symbols.add("÷");
    }

    public void tapClear(View view) {
        formula.setText("");
        result.setText("");
        symbols.clear();
    }

    public void tapEqual(View view) {
        BigDecimal tmp;
        BigDecimal numResult = new BigDecimal(0);
        String TextFormula = formula.getText().toString();
        List<String> nums = Arrays.asList(TextFormula.split("[\\+\\-×÷]", -1));
        List<BigDecimal> numbers = new ArrayList<BigDecimal>();
        try {
            if ((nums.get(0).length() == 0) && (symbols.get(0) == "-")) {
                nums.set(0, "0");
            }
            for (String num : nums){
                numbers.add(new BigDecimal(num));
            }
            for (int i = 0; i < symbols.size(); i++) {
                switch (symbols.get(i)) {
                    case "×":
                        tmp = numbers.get(i).multiply(numbers.get(i+1));
                        numbers.set(i,new BigDecimal(0));
                        numbers.set(i + 1, tmp);
                        break;
                    case "÷":
                        tmp = numbers.get(i).divide(numbers.get(i+1),2,BigDecimal.ROUND_HALF_UP);
                        numbers.set(i,new BigDecimal(0));
                        numbers.set(i + 1, tmp);
                        break;
                    case "-":
                        numbers.set(i+1,numbers.get(i+1).multiply(new BigDecimal(-1)));
                        break;
                }
            }
            for (BigDecimal num : numbers) {
                numResult = numResult.add(num);
            }
            result.setText(numResult.toString());
        } catch (Exception e) {
            Log.e("エラー", e.getMessage());
        }
    }
}

縦画面に固定したいので、マニフェストに追記

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.calculateapp">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.CalculateApp">
        <activity
            android:name=".MainActivity"
            android:screenOrientation="portrait">//縦画面固定
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

YouTubeに作っている動画をアップしています。
https://www.youtube.com/watch?v=kvz7gLP3SHI&list=PLhg2PHSq8bjisIZGg-cLe4TegqJTCBXhS
以上。

2
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?