반응형
Image : Marshmallow, API Level 23, Android 6.0, ABI x86
AVD Hardware : Nexus 4(768x1280, xhdpi)
language : java
res/layout/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"
android:orientation="vertical"
android:weightSum="100"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="20"
android:orientation="vertical">
<TextView
android:id="@+id/result_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:gravity="center_vertical|right"
android:padding="5dp"
android:text="0"
android:textSize="50sp"
android:textColor="#666666"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000000" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="80"
android:orientation="vertical"
android:padding="10dp"
android:weightSum="100">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="20"
android:orientation="horizontal"
android:weightSum="100">
<Button
android:id="@+id/all_clear_but"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="25"
android:onClick="buttonClick"
android:text="AC"
android:textSize="30sp" />
<Button
android:id="@+id/clear_entry_but"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="25"
android:onClick="buttonClick"
android:text="CE"
android:textSize="30sp" />
<Button
android:id="@+id/back_space_but"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="25"
android:onClick="buttonClick"
android:text="BS"
android:textSize="30sp" />
<Button
android:id="@+id/division_but"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="25"
android:backgroundTint="#929292"
android:onClick="operatorClick"
android:text="/"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="20"
android:orientation="horizontal"
android:weightSum="100">
<Button
android:id="@+id/num7_but"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="25"
android:onClick="numButtonClick"
android:text="7"
android:textSize="30sp" />
<Button
android:id="@+id/num8_but"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="25"
android:onClick="numButtonClick"
android:text="8"
android:textSize="30sp" />
<Button
android:id="@+id/num9_but"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="25"
android:onClick="numButtonClick"
android:text="9"
android:textSize="30sp" />
<Button
android:id="@+id/multi_but"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="25"
android:backgroundTint="#929292"
android:onClick="operatorClick"
android:text="*"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="20"
android:orientation="horizontal"
android:weightSum="100">
<Button
android:id="@+id/num4_but"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="25"
android:onClick="numButtonClick"
android:text="4"
android:textSize="30sp" />
<Button
android:id="@+id/num5_but"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="25"
android:onClick="numButtonClick"
android:text="5"
android:textSize="30sp" />
<Button
android:id="@+id/num6_but"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="25"
android:onClick="numButtonClick"
android:text="6"
android:textSize="30sp" />
<Button
android:id="@+id/sub_but"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="25"
android:backgroundTint="#929292"
android:onClick="operatorClick"
android:text="-"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="20"
android:orientation="horizontal"
android:weightSum="100">
<Button
android:id="@+id/num1_but"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="25"
android:onClick="numButtonClick"
android:text="1"
android:textSize="30sp" />
<Button
android:id="@+id/num2_but"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="25"
android:onClick="numButtonClick"
android:text="2"
android:textSize="30sp" />
<Button
android:id="@+id/num3_but"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="25"
android:onClick="numButtonClick"
android:text="3"
android:textSize="30sp" />
<Button
android:id="@+id/add_but"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="25"
android:backgroundTint="#929292"
android:onClick="operatorClick"
android:text="+"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="20"
android:orientation="horizontal"
android:weightSum="100">
<Button
android:id="@+id/num0_but"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="75"
android:onClick="numButtonClick"
android:text="0"
android:textSize="30sp" />
<Button
android:id="@+id/res_but"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="25"
android:onClick="operatorClick"
android:text="="
android:textSize="30sp"
android:background="#929292"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.cac.calc;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
boolean isFirstInput = true; // 입력 값이 처음 입력되는가를 체크
int resultNumber = 0; // 계산 값 저장
char operator = '+'; // 연산자 저장
final String CLEAR_INPUT_TEXT = "0";
TextView resultText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultText = findViewById(R.id.result_text);
}
// AC, CE, BS 클릭
public void buttonClick(View view) {
switch ( view.getId() )
{
case R.id.all_clear_but:
resultNumber = 0;
operator = '+';
setClearText(CLEAR_INPUT_TEXT);
break;
case R.id.clear_entry_but:
setClearText(CLEAR_INPUT_TEXT);
break;
case R.id.back_space_but:
if (resultText.getText().toString().length() > 1 )
{
String getResultText = resultText.getText().toString();
String subString = getResultText.substring(0,getResultText.length()-1);
resultText.setText(subString);
} else {
resultText.setTextColor(0xFF666666);
resultText.setText(CLEAR_INPUT_TEXT);
isFirstInput = true;
}
break;
default:
break;
}
}
// 입력된 숫자를 초기화
public void setClearText(String clearText)
{
isFirstInput = true;
resultText.setTextColor(0xFF666666);
resultText.setText(clearText);
}
// 0~9 버튼 클릭
public void numButtonClick(View view)
{
Button getButton = findViewById(view.getId());
if(isFirstInput){
resultText.setTextColor(0xFF000000);
resultText.setText(getButton.getText().toString());
isFirstInput = false;
} else {
if (resultText.getText().toString().equals("0"))
{
Toast.makeText(getApplicationContext(), "초기 입력값이 0입니다.",Toast.LENGTH_LONG).show();
setClearText(CLEAR_INPUT_TEXT);
return;
}
resultText.append(getButton.getText().toString());
}
}
// 사칙연산 값을 반환
public int intCal(int result, int lastNum, char operator)
{
if ( operator == '+'){
result += lastNum;
} else if ( operator == '-') {
result -= lastNum;
} else if ( operator == '/') {
result /= lastNum;
} else if ( operator == '*') {
result *= lastNum;
}
return result;
}
// 연산자 클릭
public void operatorClick(View view)
{
Button getButton = findViewById(view.getId());
if(view.getId() == R.id.res_but) {
if(isFirstInput){
resultNumber = 0;
operator = '+';
setClearText(CLEAR_INPUT_TEXT);
// = 두번 이상 누를 때 처리
} else {
resultNumber = intCal(resultNumber, Integer.parseInt(resultText.getText().toString()), operator);
resultText.setText(String.valueOf(resultNumber));
isFirstInput = true;
}
} else {
if(isFirstInput){
operator = getButton.getText().toString().charAt(0);
} else {
int lastNum = Integer.parseInt(resultText.getText().toString());
resultNumber = intCal(resultNumber, lastNum, operator);
operator = getButton.getText().toString().charAt(0);
resultText.setText(String.valueOf(resultNumber));
isFirstInput = true;
}
}
}
}
app/res/values/strings.xml
<resources>
<string name="app_name">계산기</string>
</resources>
안드로이드 APK 배포
> New Key Store 항목 입력 > release 선택, V1-V2 모두 체크 > 프로젝트 폴더(app/release) apk 배포 완료
Playstore 배포
새 개발자 계정 만들기(25$ 결제필요) > (모든 앱 > 첫 번째 앱 만들기) >
(대시보드 > 스토어 등록정보 설정 > 할 일 보기에 나온 항목 모두 설정) > (대시보드 > Google Play에 앱 게시) >
프로덕션 트랙으로 출시 시작
* apk파일 업로드 시 com.example이 제한된 상태이므로 다른 패키지 이름을 사용해야 합니다. 해결 방법
안드로이드 스튜디오 > 패키지명 바꿀 폴더 오른쪽 상단의 톱니바퀴 클릭 > Compact Middle Packages 선택 해제
> app/src/main/java/"example" 패키지 우클릭 후 Refector, Rename > Rename Packages > 변경할 패키지명 >
build.gradle 스크립트에서 applicationId를 변경한 패키지명으로 수정 > (Build > Build Bundle(s) / Build APK(s) > Build APK(s)) >
안드로이드 APK 배포
계산기 앱 제작 강의 :
https://www.youtube.com/watch?v=cZXMzTL17es&list=PLMocbRXgGcjZwmzqb0meFSeH9uh_FC-dM&index=2
반응형
'프로그래밍' 카테고리의 다른 글
이더리움 dapp 메타마스크 연동하기 (0) | 2022.02.15 |
---|---|
python exe 파일 만들기 (0) | 2022.01.16 |
한국어 문장 생성 AI - KoGPT2 (0) | 2021.06.19 |
이클립스 디버깅 (0) | 2021.01.10 |
Python 문법 정리 (0) | 2020.11.10 |