philaphillip

[안드로이드, Kotlin] 숫자세기(계수기) 앱 만들기 - 개인 프로젝트 1 본문

안드로이드/코틀린

[안드로이드, Kotlin] 숫자세기(계수기) 앱 만들기 - 개인 프로젝트 1

잡식블로그 2024. 1. 4. 15:49
반응형

개요

  • 버튼을 누르면 숫자가 1씩 늘어남
  • 초기화 기능이 있음
  • 9,999까지 셀 수 있음

 

 

UI

xml

<pre id="code_1637046272621" class="xml" data-ke-laguage="xml"><code>

<?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">

    <TextView
        android:id="@+id/numberTextView"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:text="100"
        android:textSize="100sp"
        android:textColor="@color/blue"
        android:textStyle="bold|italic"
        android:gravity="center"/>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <Button
            android:id="@+id/resetButton"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:layout_marginTop="16dp"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:text="초기화"/>

        <Button
            android:id="@+id/plusButton"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:layout_margin="16dp"
            android:text="+"/>

    </LinearLayout>





</LinearLayout>

 

 

 

 

 

Main Activity

package com.exemple.countnumber

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.TextView

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val numberTextView = findViewById<TextView>(R.id.numberTextView)
        val resetButton = findViewById<Button>(R.id.resetButton)
        val plusButton = findViewById<Button>(R.id.plusButton)

        var number = 0


        resetButton.setOnClickListener {
            number = 0
            numberTextView.text = number.toString()
            //Log.d("onClick", "리셋된 숫자 ${number}")
        }

        plusButton.setOnClickListener {
            number+=1
            numberTextView.text = number.toString()
            //Log.i("onClick", "플러스된 숫자 ${number}")
        }
    }
}

 

Main Activity의 패키지 부분은 각자 하고있는 파일의 위치로 바꿔주세요!

반응형
Comments