먼저 이 소스와 연습은 유투버 '동빈나'님과 안드로이드스튜디오 책을 인용한 사실을 알립니다.
Mainactivity.java 설정
package com.cookandroid.calculator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void addClick(View v) {
EditText Number1 = (EditText) findViewById(R.id.Number1);
EditText Number2 = (EditText) findViewById(R.id.Number2);
TextView result = (TextView) findViewById(R.id.result);
int n1 = Integer.parseInt(Number1.getText().toString());
int n2 = Integer.parseInt(Number2.getText().toString()); //정수형으로 저장 , 더하기 버튼에 대한 실행
result.setText(Integer.toString(n1 + n2));
}
public void subtractClick(View v) {
EditText Number1 = (EditText) findViewById(R.id.Number1);
EditText Number2 = (EditText) findViewById(R.id.Number2);
TextView result = (TextView) findViewById(R.id.result);
int n1 = Integer.parseInt(Number1.getText().toString());
int n2 = Integer.parseInt(Number2.getText().toString()); //정수형으로 저장, 빼기 버튼에 대한 실행
result.setText(Integer.toString(n1 - n2));
}
public void multiplyClick(View v) {
EditText Number1 = (EditText) findViewById(R.id.Number1);
EditText Number2 = (EditText) findViewById(R.id.Number2);
TextView result = (TextView) findViewById(R.id.result);
int n1 = Integer.parseInt(Number1.getText().toString());
int n2 = Integer.parseInt(Number2.getText().toString()); //정수형으로 저장, 곱하기 버튼에 대한 실행
result.setText(Integer.toString(n1 * n2));
}
public void divideClick(View v) {
EditText Number1 = (EditText) findViewById(R.id.Number1);
EditText Number2 = (EditText) findViewById(R.id.Number2);
TextView result = (TextView) findViewById(R.id.result);
int n1 = Integer.parseInt(Number1.getText().toString());
int n2 = Integer.parseInt(Number2.getText().toString()); //정수형으로 저장, 나누기 버튼에 대한 실행
result.setText(Integer.toString(n1 / n2));
}
}
-----------------------------------------------------------------------------------------------------------------------------------
activity_main.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"
tools:context=".MainActivity"
android:orientation="vertical">
<EditText
android:id="@+id/Number1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="첫 번째 숫자"
android:inputType="textPersonName"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="16dp" />
<EditText
android:id="@+id/Number2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="두번 째 숫자"
android:inputType="textPersonName"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="80dp" />
<Button
android:id="@+id/addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Number2"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:onClick="addClick"
android:text="+" />
<Button
android:id="@+id/subtractButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Number2"
android:layout_toEndOf="@+id/addButton"
android:layout_toRightOf="@+id/addButton"
android:onClick="subtractClick"
android:text="-" />
<Button
android:id="@+id/multiplyButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="multiplyClick"
android:text="x"
android:layout_below="@+id/Number2"
android:layout_toRightOf="@+id/subtractButton"
android:layout_toEndOf="@+id/subtractButton" />
<Button
android:id="@+id/divideButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="divideClick"
android:text="÷"
android:layout_below="@+id/Number2"
android:layout_toRightOf="@+id/multiplyButton"
android:layout_toEndOf="@+id/multiplyButton" />
<TextView
android:id="@+id/result"
android:layout_width="350dp"
android:layout_height="21dp"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="198dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
</LinearLayout>
'Programming > 안드로이드스튜디오' 카테고리의 다른 글
앱만들기 [ 계산기 ] (0) | 2020.04.17 |
---|---|
안드로이드 앱 만들기 : 가위바위보게임 (1) | 2020.03.11 |
안드로이드 앱만들기_2STEP : 계산기 2탄 (0) | 2020.03.05 |