Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 | 31 |
Tags
- MySQL 세팅
- OpenCV
- bootstrap
- 콘솔 가상환경 # 콘솔 #가상환경
- bootstrap4 패키지
- 성능지표
- #undef
- 실시간 시계
- DB 데이터
- javascript
- jinja2
- href
- 튜토리얼
- Action
- PyQt5
- openweathermap
- 사이트 도메인
- 환경변수 설정
- heroku
- VS Code
- OpenCV + Flask
- #endif
- #ifdef
- 명령어
- DB 데이터 저장
- #else
- #ifndef
- Django
- flask
- #if
Archives
- Today
- Total
PROGRAMMING
안드로이드 실습 - 연산 어플 본문
1. MainActivity.java
package com.example.tutorial1;
import androidx.appcompat.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 multipleClick(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));
}
}
2. activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<EditText
android:id="@+id/number1"
android:layout_width="409dp" <!-- match_parent, wrap_content 사용하지 않고 수동 설정 -->
android:layout_height="48dp"
android:ems="10"
android:hint="첫 번째 숫자"
android:inputType="textPersonName"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="70dp" />
<EditText
android:id="@+id/number2"
android:layout_width="411dp"
android:layout_height="46dp"
android:ems="10"
android:hint="두 번째 숫자"
android:inputType="textPersonName"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="122dp" />
<Button
android:id="@+id/subtractButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="빼기"
tools:layout_editor_absoluteX="109dp"
tools:layout_editor_absoluteY="168dp"
android:onClick="subtractClick"/>
<Button
android:id="@+id/addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="더하기"
tools:layout_editor_absoluteX="7dp"
tools:layout_editor_absoluteY="168dp"
android:onClick="addClick"/>
<Button
android:id="@+id/multiplyButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="곱하기"
tools:layout_editor_absoluteX="209dp"
tools:layout_editor_absoluteY="168dp"
android:onClick="multipleClick"/>
<Button
android:id="@+id/divideButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="나누기"
tools:layout_editor_absoluteX="309dp"
tools:layout_editor_absoluteY="168dp"
android:onClick="divideClick"/>
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
tools:layout_editor_absoluteX="9dp"
tools:layout_editor_absoluteY="219dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
'Android' 카테고리의 다른 글
Button, TextView, Toast (0) | 2020.10.27 |
---|---|
안드로이드 실습 - 리스트, 종료 with AlertDialog (0) | 2020.10.27 |
JVM, JRE, JDK, Gradle, Apache Groovy, Compile process (0) | 2020.10.26 |
VSCode, eclipse, Intellij (+ Java) 환경 설정 (0) | 2020.10.26 |
Java, Android Studio 설치(+D drive), 휴대폰 연동 (0) | 2020.10.26 |
Comments