IT/Android

android - EditText에 값 입력받아서 사칙연산 결과 보여주기

노마드오브 2018. 9. 5. 21:31

파일명 : calc.xml

<?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:orientation="vertical">

<EditText
android:id="@+id/Edit1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="숫자1"/>

<EditText
android:id="@+id/Edit2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="숫자2"/>

<Button
android:id="@+id/BtnAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="더하기"/>

<Button
android:id="@+id/BtnSub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="빼기" />

<Button
android:id="@+id/BtnMul"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="곱하기"/>

<Button
android:id="@+id/BtnDiv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="나누기"/>

<Button
android:id="@+id/BtnKkk"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="나머지"/>

<TextView
android:id="@+id/TextResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="계산 결과:"
android:textColor="#FF0000"
android:textSize="30dp"/>

</LinearLayout>


파일명 : MainActivity.java

package com.example.it.myapplication;

import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

EditText e1;
EditText e2;

Button b1;
Button b2;
Button b3;
Button b4;
Button b5;

TextView t1;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.calc); //전체뷰(xml파일) 들고오기

e1 = (EditText) findViewById(R.id.Edit1);
e2 = (EditText) findViewById(R.id.Edit2);

b1 = (Button) findViewById(R.id.BtnAdd); //더하기 버튼(개별뷰) 연결
b2 = (Button) findViewById(R.id.BtnSub); //빼기 버튼(개별뷰) 연결
b3 = (Button) findViewById(R.id.BtnMul); //곱하기 버튼(개별뷰) 연결
b4 = (Button) findViewById(R.id.BtnDiv); //나누기 버튼(개별뷰) 연결
b5 = (Button) findViewById(R.id.BtnKkk); //나머지 버튼(개별뷰) 연결

t1 = (TextView) findViewById(R.id.TextResult);

//1. 버튼을 눌렀을 때(버튼 클릭 이벤트가 발생했을때),
//2. 2개의 xml의 EditText에서 각각 값을 java쪽으로 가져와서, 더한다.
//3. 더한 값을 xml의 TextView 에 출력한다.

//버튼.1( 리스너 )
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

if(e1.getText().toString().equals("") ||
e2.getText().toString().equals("")) {
Toast.makeText(MainActivity.this,
"값이 없습니다.",
Toast.LENGTH_LONG).show();
} else { //둘 다 공백이 아닐때
String s1 = e1.getText().toString();//1번 값 가져오기(xml->java)
String s2 = e2.getText().toString();//2번 값 가져오기(xml->java)
int result = Integer.parseInt(s1) + Integer.parseInt(s2);
t1.setText("계산 결과:"+result);
}

               //t1.setText(String.valueOf(result));
}
});

//버튼.2( 리스너 )
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

if(e1.getText().toString().equals("") ||
e2.getText().toString().equals("")) {
Toast.makeText(MainActivity.this,
"값이 없습니다.",
Toast.LENGTH_LONG).show();
} else { //둘 다 공백이 아닐때
String s1 = e1.getText().toString();//1번 값 가져오기(xml->java)
String s2 = e2.getText().toString();//2번 값 가져오기(xml->java)
int result = Integer.parseInt(s1) - Integer.parseInt(s2);
t1.setText("계산 결과:"+result);
}

}
});

//버튼.3( 리스너 )
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

if(e1.getText().toString().equals("") ||
e2.getText().toString().equals("")) {
Toast.makeText(MainActivity.this,
"값이 없습니다.",
Toast.LENGTH_LONG).show();
} else { //둘 다 공백이 아닐때
String s1 = e1.getText().toString();//1번 값 가져오기(xml->java)
String s2 = e2.getText().toString();//2번 값 가져오기(xml->java)
int result = Integer.parseInt(s1) * Integer.parseInt(s2);
t1.setText("계산 결과:"+result);
}

}
});

//버튼.4( 리스너 )
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

if(e1.getText().toString().equals("") ||
e2.getText().toString().equals("")) {
Toast.makeText(MainActivity.this,
"값이 없습니다.",
Toast.LENGTH_LONG).show();
} else { //둘 다 공백이 아닐때
if(e2.getText().toString().equals("0")) {
Toast.makeText(MainActivity.this,
"0으로 나눌 수 없습니다~!",
Toast.LENGTH_LONG).show();
} else {
String s1 = e1.getText().toString();//1번 값 가져오기(xml->java)
String s2 = e2.getText().toString();//2번 값 가져오기(xml->java)
Double d1 = Double.parseDouble(s1);//문자 -> 숫자(더블형)
Double d2 = Double.parseDouble(s2);//문자 -> 숫자(더블형)
Double d3 = d1 / d2;
t1.setText("계산 결과:"+d3);
}
}

}
});

//버튼.5( 리스너 )
b5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

if(e1.getText().toString().equals("") ||
e2.getText().toString().equals("")) {
Toast.makeText(MainActivity.this,
"값이 없습니다.",
Toast.LENGTH_LONG).show();
} else { //둘 다 공백이 아닐때
String s1 = e1.getText().toString();//1번 값 가져오기(xml->java)
String s2 = e2.getText().toString();//2번 값 가져오기(xml->java)
int result = Integer.parseInt(s1) % Integer.parseInt(s2);
t1.setText("계산 결과:"+result);
}

}
});


}
}