[Android] kotlin 버튼클릭 이벤트 처리(Button onclick event)

2022. 12. 1. 09:31Programming/Android

android studio logo

kotlin 버튼클릭 이벤트 처리(Button onclick event)

안드로이드 간단한 버튼 클릭 이벤(setonclick event)트를 만들어봅시다.

 

MainActivity

package com.fflask

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
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 counter_num = findViewById<TextView>(R.id.counter_num)
        val btn_event = findViewById<Button>(R.id.e_btn)
        var counter = 0

        btn_event.setOnClickListener {
            counter++//클릭할때마다 카운터 상승
            counter_num.text = counter.toString()
            //카운터를 텍스트뷰로
        }

    }
}

activity_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/main_background_colors"
        tools:context=".MainActivity">

    <TextView
            android:id="@+id/counter_num"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/start_main_text"
            android:textSize="80sp"
            android:textColor="@android:color/background_light"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
    <Button
            android:text="@string/reset_btn_text"
            android:textColor="@android:color/background_light"
            android:layout_width="wrap_content"
            android:textStyle="bold"
            android:layout_height="wrap_content"
            android:id="@+id/e_btn"
            android:background="#00000000"
            android:layout_marginTop="8dp"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintVertical_bias="0.988"
            android:layout_marginEnd="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_marginLeft="8dp"
            app:layout_constraintHorizontal_bias="0.498"/>

</android.support.constraint.ConstraintLayout>

xml은 바로 사용하시면 에러가 있을 꺼에요 필요없으신 부분은 삭제하고 사용해주세요.

Main구문의 주석을 참조해주세요.

 

다양한 인벤트를 다음에 다양한 이벤트를 추가해봐야 겠어요.

App playing vedio link: https://www.youtube.com/watch?v=xI_7s9oV3MA 

 

반응형