[Android Studio] 간단한 계산기
2022. 5. 5. 13:43
더하기, 빼기, 나누기, 곱하기, 나머지를 구하는 계산기이다.
try ~ catch 문을 사용하고 계산값이 비어있거나 계산이 되지않을 때에 토스트를 출력한다.
- 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"
tools:context=".MainActivity"
android:orientation="vertical">
<EditText
android:id="@+id/Edit1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="숫자1"
/>
<EditText
android:id="@+id/Edit2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="숫자2"
android:layout_margin="10dp"
/>
<Button
android:id="@+id/BtnAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="더하기" />
<Button
android:id="@+id/BtnSub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="빼기" />
<Button
android:id="@+id/BtnMul"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="곱하기" />
<Button
android:id="@+id/BtnDiv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="나누기"
/>
<Button
android:id="@+id/BtnNa"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="나머지"
/>
<TextView
android:id="@+id/TextResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:textColor="#00BCD4"
android:layout_margin="10dp"
android:text="결과 :" />
</LinearLayout>
- mainActivity.java
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 edit1, edit2;
Button btnAdd, btnSub, btnMul, btnDiv, btnNa;
TextView textResult;
String num1, num2;
Integer result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("초간단 계산기");
edit1 = (EditText) findViewById(R.id.Edit1);
edit2 = (EditText) findViewById(R.id.Edit2);
btnAdd = (Button) findViewById(R.id.BtnAdd);
btnSub = (Button) findViewById(R.id.BtnSub);
btnMul = (Button) findViewById(R.id.BtnMul);
btnDiv = (Button) findViewById(R.id.BtnDiv);
btnNa = (Button) findViewById(R.id.BtnNa);
textResult = (TextView) findViewById(R.id.TextResult);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
num1 = edit1.getText().toString();
num2 = edit2.getText().toString();
if(num1.isEmpty() || num2.isEmpty()) {
Toast.makeText(getApplicationContext(), "숫자를 입력하세요", Toast.LENGTH_SHORT).show();
}
else {
result = Integer.parseInt(num1) + Integer.parseInt(num2);
textResult.setText("계산 결과 : " + result.toString());
}
}
});
btnSub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
num1 = edit1.getText().toString();
num2 = edit2.getText().toString();
if (num1.isEmpty() || num2.isEmpty()) {
Toast.makeText(getApplicationContext(), "숫자를 입력하세요", Toast.LENGTH_SHORT).show();
} else {
result = Integer.parseInt(num1) - Integer.parseInt(num2);
textResult.setText("계산 결과 : " + result.toString());
}
}
});
btnMul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
num1 = edit1.getText().toString();
num2 = edit2.getText().toString();
if(num1.isEmpty() || num2.isEmpty()) {
Toast.makeText(getApplicationContext(), "숫자를 입력하세요", Toast.LENGTH_SHORT).show();
}
else {
result = Integer.parseInt(num1) * Integer.parseInt(num2);
textResult.setText("계산 결과 : " + result.toString());
}
}
});
btnDiv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
num1 = edit1.getText().toString();
num2 = edit2.getText().toString();
if (num1.isEmpty() || num2.isEmpty()) {
Toast.makeText(getApplicationContext(), "숫자를 입력하세요", Toast.LENGTH_SHORT).show();
} else {
result = Integer.parseInt(num1) / Integer.parseInt(num2);
textResult.setText("계산 결과 : " + result.toString());
}
}
});
btnNa.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
num1 = edit1.getText().toString();
num2 = edit2.getText().toString();
String error = "숫자를 입력하세요";
if (num1.trim().equals("") || num2.trim().equals("")) {
Toast.makeText(getApplicationContext(), error, Toast.LENGTH_SHORT).show();
}
else{
try {
result = Integer.parseInt(num1) % Integer.parseInt(num2);
textResult.setText("계산 결과 : " + result.toString());
} catch (java.lang.ArithmeticException e) {
Toast.makeText(getApplicationContext(), "계산이 되지않습니다", Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
728x90
'Study > Android' 카테고리의 다른 글
[Android] 강의 내용 정리(220322 ~220329) (0) | 2022.05.28 |
---|---|
[Android Studio] Android Studio를 시작하기전에 필요한 Java 기본 문법 (1) (0) | 2022.05.05 |