본문 바로가기

카테고리 없음

패스트 캠퍼스 안드로이드 공부

뭔가 알고리즘을 공부해도 너무 실력이 늘지 않는 것 같다.

그래서 클론 코딩을 하면서 모르는 용어를 공부하고 흐름을 파악하는 방법으로 공부를 해야 될 것 같다.

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat
    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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <!--orinentatin=vertical : 세로로 쌓겠다-->
    <TextView
        android:id="@+id/numberTextView"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:text="0"
        android:textSize="100sp"
        android:textColor="#5E35B1"
        android:gravity="center"/>
    <!--layout 중첩-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/resetButton"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="초기화"
            android:layout_weight="1"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"/>

        <Button
            android:id="@+id/plusButton"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="+"
            android:layout_weight="1"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"/>

    </LinearLayout>

</androidx.appcompat.widget.LinearLayoutCompat>

위의 xml 코드를 작성하면 바로 이런 이미지가 나타난다.설명은 주석을 달면서 공부를 하였다.

 

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        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
            // number를 numberTextView에 할당하겠다.
            // numberTextView.text : numberTextView를 text형으로 변환
            numberTextView.text = number.toString()
            Log.d("onClick", "리셋 버튼이 클릭되었습니다")
        }

        plusButton.setOnClickListener {
            number += 1
            numberTextView.text = number.toString()
        }
    }
}

코틀린 코드를 작성하면 1씩 올라가고 초기화를 누르면 0이되는 코드이다. 언뜻보면 정말 단순한 코드일 수 있지만 혼자서 처음부터 작성하라고 하면 어려울 수도 있는 코드인 것 같다.