일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 자바 오류
- DoitSQL입문
- 자바 예외
- dbms
- 페이지분석
- SQL
- R1C3
- 크롤링 오류
- 예외
- DoIt
- HTML역사
- DoitSQL
- 웹브라우저 수용도
- Doit입문SQL
- 데이터베이스
- html
- 배열 예제
- 예제
- 함수 선언
- 우아한테크
- 숫자형식오류
- 생성자
- 자바
- 배열 3요소
- 웹 브라우저 전쟁
- 키-값 데이터베이스
- SQL입문
- 함수
- 크롤링
- 숫자 형식
Archives
- Today
- Total
반응형
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 자바 오류
- DoitSQL입문
- 자바 예외
- dbms
- 페이지분석
- SQL
- R1C3
- 크롤링 오류
- 예외
- DoIt
- HTML역사
- DoitSQL
- 웹브라우저 수용도
- Doit입문SQL
- 데이터베이스
- html
- 배열 예제
- 예제
- 함수 선언
- 우아한테크
- 숫자형식오류
- 생성자
- 자바
- 배열 3요소
- 웹 브라우저 전쟁
- 키-값 데이터베이스
- SQL입문
- 함수
- 크롤링
- 숫자 형식
Archives
- Today
- Total
프로그래밍
[안드로이드 자바] 기본 계산기 본문
728x90
반응형
디자인
프로젝트 설명
EditText로 숫자를 입력받아 연산 후(덧셈, 뺄셈, 곱셈, 나누기, 나머지) TextView로 결과 출력
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"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Edit1"
android:hint="숫자1"
android:layout_margin="10dp"
></EditText>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Edit2"
android:hint="숫자2"
android:layout_margin="10dp"
></EditText>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/BtnAdd1"
android:text="덧 셈"
android:layout_margin="10dp"
></Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/BtnAdd2"
android:text="뺄 셈"
android:layout_margin="10dp"
></Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/BtnAdd3"
android:text="곱 셈"
android:layout_margin="10dp"
></Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/BtnAdd4"
android:text="나 누 기"
android:layout_margin="10dp"
></Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/BtnAdd5"
android:text="나 머 지"
android:layout_margin="10dp"
></Button>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ResultText"
android:text="계산결과 : "
android:layout_margin="10dp"
android:textSize="30dp"
android:textColor="#0000ff"
></TextView>
</LinearLayout>
Mainactivity.java
package com.example.calcfloat;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
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 btnAdd1;
Button btnAdd2;
Button btnAdd3;
Button btnAdd4;
Button btnAdd5;
TextView resultText;
String num1, num2;
Integer res;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit1 = (EditText) findViewById(R.id.Edit1);
edit2 = (EditText) findViewById(R.id.Edit2);
btnAdd1 = (Button) findViewById(R.id.BtnAdd1);
btnAdd2 = (Button) findViewById(R.id.BtnAdd2);
btnAdd3 = (Button) findViewById(R.id.BtnAdd3);
btnAdd4 = (Button) findViewById(R.id.BtnAdd4);
btnAdd5 = (Button) findViewById(R.id.BtnAdd5);
resultText = (TextView) findViewById(R.id.ResultText);
btnAdd1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
num1 = edit1.getText().toString();
num2 = edit2.getText().toString();
if (num1.trim().equals("") || num2.trim().equals("")) {
Toast.makeText(getApplicationContext(), "값을 입력하세요", Toast.LENGTH_SHORT).show();
} else {
res = Integer.parseInt(num1) + Integer.parseInt(num2);
resultText.setText("계산결과 : " + res.toString());
}
return false;
}
});
btnAdd2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
num1 = edit1.getText().toString();
num2 = edit2.getText().toString();
if (num1.trim().equals("") || num2.trim().equals("")) {
Toast.makeText(getApplicationContext(), "값을 입력하세요", Toast.LENGTH_SHORT).show();
} else {
res = Integer.parseInt(num1) - Integer.parseInt(num2);
resultText.setText("계산결과 : " + res.toString());
}
return false;
}
});
btnAdd3.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
num1 = edit1.getText().toString();
num2 = edit2.getText().toString();
if (num1.trim().equals("") || num2.trim().equals("")) {
Toast.makeText(getApplicationContext(), "값을 입력하세요", Toast.LENGTH_SHORT).show();
} else {
res = Integer.parseInt(num1) * Integer.parseInt(num2);
resultText.setText("계산결과 : " + res.toString());
}
return false;
}
});
btnAdd4.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
num1 = edit1.getText().toString();
num2 = edit2.getText().toString();
if (num1.trim().equals("") || num2.trim().equals("")) {
Toast.makeText(getApplicationContext(), "값을 입력하세요", Toast.LENGTH_SHORT).show();
} else {
if (num2.trim().equals("0")) {
Toast.makeText(getApplicationContext(), "0으로 나눌 수 없음", Toast.LENGTH_SHORT).show();
} else {
res = Integer.parseInt(num1) / Integer.parseInt(num2);
resultText.setText("계산결과 : " + res.toString());
}
}
return false;
}
});
btnAdd5.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
num1 = edit1.getText().toString();
num2 = edit2.getText().toString();
if (num1.trim().equals("") || num2.trim().equals("")) {
Toast.makeText(getApplicationContext(), "값을 입력하세요", Toast.LENGTH_SHORT).show();
} else {
if (num2.trim().equals("0")) {
Toast.makeText(getApplicationContext(), "0으로 나눌 수 없음", Toast.LENGTH_SHORT).show();
} else {
res = Integer.parseInt(num1) % Integer.parseInt(num2);
resultText.setText("계산결과 : " + res.toString());
}
}
return false;
}
});
}
}
728x90
반응형
'안드로이드 > 안드로이드 스튜디오' 카테고리의 다른 글
[안드로이드 자바] 예약 어플 만들기(Chronometer, TimePicker, DatePicker, CalendarView) (0) | 2022.05.17 |
---|---|
[안드로이드 자바] 타임피커, 캘린더뷰를 이용한 예약 시스템 만들기(Chronometer, Timpicker, CalendarView) (0) | 2022.05.17 |
[안드로이드 자바] 그리드 계산기(GridLayout) (0) | 2021.12.09 |
[안드로이드 자바] 라디오버튼으로 이미지 선택 출력(visible, invisible) (0) | 2021.12.09 |
[안드로이드 자바] xml 요소 배치하기(텍스트뷰, 버튼, 스위치, 레이팅바) (0) | 2021.12.03 |
Comments