-문제 

 

 

[안드로이드 스튜디오] 기초 계산기 어플리케이션 만들기

기본 사칙연산을 위한 간단한 어플리케이션을 제작 디자인 레이아웃 부분은 res -> layout -> activi...

blog.naver.com

 

<MainActivity.java> 

package com.example.test;

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 {

    Button button1, button2, button3, button4, button5, button6, button7, button8, button9, button0, buttonC, buttonOk;

    EditText edit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = findViewById(R.id.button1);
        button2 = findViewById(R.id.button2);
        button3 = findViewById(R.id.button3);
        button4 = findViewById(R.id.button4);
        button5 = findViewById(R.id.button5);
        button6 = findViewById(R.id.button6);
        button7 = findViewById(R.id.button7);
        button8 = findViewById(R.id.button8);
        button9 = findViewById(R.id.button9);
        button0 = findViewById(R.id.button0);
        buttonC = findViewById(R.id.buttonC);
        buttonOk = findViewById(R.id.buttonOk);
        edit = findViewById(R.id.edit);

        View.OnClickListener cl = new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (v == button1) {
                    edit.setText(edit.getText().toString() + 1);
                } else if (v == button2) {
                    edit.setText(edit.getText().toString() + 2);
                } else if (v == button3) {
                    edit.setText(edit.getText().toString() + 3);
                } else if (v == button4) {
                    edit.setText(edit.getText().toString() + 4);
                } else if (v == button5) {
                    edit.setText(edit.getText().toString() + 5);
                } else if (v == button6) {
                    edit.setText(edit.getText().toString() + 6);
                } else if (v == button7) {
                    edit.setText(edit.getText().toString() + 7);
                } else if (v == button8) {
                    edit.setText(edit.getText().toString() + 8);
                } else if (v == button9) {
                    edit.setText(edit.getText().toString() + 9);
                } else if (v == button0) {
                    edit.setText(edit.getText().toString() + 0);
                } else if (v == buttonC) {
                    edit.setText("");
                } else if (v == buttonOk) {
                    edit.setText("");
                }
            }
        };

        button1.setOnClickListener(cl);
        button2.setOnClickListener(cl);
        button3.setOnClickListener(cl);
        button4.setOnClickListener(cl);
        button5.setOnClickListener(cl);
        button6.setOnClickListener(cl);
        button7.setOnClickListener(cl);
        button8.setOnClickListener(cl);
        button9.setOnClickListener(cl);
        button0.setOnClickListener(cl);
        buttonC.setOnClickListener(cl);
        buttonOk.setOnClickListener(cl);
    }
}

 

 

<activity_main.xml>

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:orientation="vertical"
    tools:context=".MainActivity">


    <TableLayout

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="8dp"
        android:stretchColumns="*">

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

            <EditText
                android:id="@+id/edit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_span="3"
                android:layout_weight="0"
                android:ems="10"
                android:text="" />
        </TableRow>


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

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_margin="5dp"
                android:textSize="40dp"
                android:text="1" />

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_margin="5dp"
                android:textSize="40dp"
                android:text="2" />

            <Button
                android:id="@+id/button3"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_margin="5dp"
                android:textSize="40dp"
                android:text="3" />

        </TableRow>

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

            <Button
                android:id="@+id/button4"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_margin="5dp"
                android:textSize="40dp"
                android:text="4" />

            <Button
                android:id="@+id/button5"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_margin="5dp"
                android:textSize="40dp"
                android:text="5" />

            <Button
                android:id="@+id/button6"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_margin="5dp"
                android:textSize="40dp"
                android:text="6" />

        </TableRow>

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

            <Button
                android:id="@+id/button7"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_margin="5dp"
                android:textSize="40dp"
                android:text="7" />

            <Button
                android:id="@+id/button8"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_margin="5dp"
                android:textSize="40dp"
                android:text="8" />

            <Button
                android:id="@+id/button9"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_margin="5dp"
                android:textSize="40dp"
                android:text="9" />

        </TableRow>

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

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:layout_weight="1" />

            <Button
                android:id="@+id/button0"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_margin="5dp"
                android:textSize="40dp"
                android:text="0" />

            <Button
                android:id="@+id/buttonC"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_margin="5dp"
                android:textSize="15dp"
                android:text="Clear" />

        </TableRow>

        <Button
            android:id="@+id/buttonOk"
            android:textSize="30dp"
            android:text="ok" />


    </TableLayout>

</RelativeLayout>

+ Recent posts