개발(Android)/android studio

[Android Studio/Kotlin] Listener, Method, Click Event

shinyelee 2022. 4. 18. 22:10

[안드로이드 스튜디오/코틀린] 리스너, 메소드, 클릭 이벤트

activiti_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"
    tools:context=".LinearLayout"
    android:orientation="vertical"
    android:background="@drawable/bg"
    android:paddingHorizontal="20dp"
    android:weightSum="1">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="70dp"
        android:gravity="center_horizontal"
        android:text="Get the life you want!\n Save more money."
        android:textColor="@color/white"
        android:textSize="25dp"
        android:textStyle="bold" />

    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="0.9"/>

    <Button
        android:text="Create free account"
        android:textColor="@color/white"
        android:textSize="20dp"
        android:background="@drawable/rounded_corner_green"
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:textAllCaps="false"/>

    <Button
        android:text="Continue with facebook"
        android:textColor="@color/white"
        android:textSize="20dp"
        android:background="@drawable/rounded_corner_blue"
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:layout_marginTop="15dp"
        android:textAllCaps="false"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:gravity="center_horizontal"
        android:text="Already have an account?"
        android:textColor="@color/white"
        android:textSize="18dp"/>

    <Button
        android:id="@+id/login_btn"
        android:text="Login"
        android:textColor="@color/white"
        android:textSize="20dp"
        android:background="@drawable/rounded_corner_opacity"
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:layout_marginTop="15dp"
        android:textAllCaps="false"/>

</LinearLayout>

MainActivity.kt

package com.shinyelee.android_study2

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

// 메인화면
class MainActivity : AppCompatActivity() {

    val TAG: String = "로그"

    // 뷰 생성
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // 그릴 xml 뷰 파일을 연결(설정) 시켜줌
        setContentView(R.layout.activity_main)

        // 강의(2020년)에서는 이 방법을 사용했으나, 현재(2022년) 지원하지 않는 방법임
        // gradle에 apply plugin: 'kotlin-android-extensions' 삽입 후
//        login_btn.setOnClickListener(View.onClickListener {
//            onLoginButtonClicked()
//        })

        // -> findViewById나 ViewBinding 사용을 권장함
        // 로그인 버튼에 클릭 리스너 설정
        findViewById<Button>(R.id.login_btn).setOnClickListener {
            // 버튼을 누르면 SecondActivity 화면으로 이동
            val intent = Intent(this, SecondActivity::class.java)
            startActivity(intent)
        }

    }
    
}

activity_second.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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=".SecondActivity">

    <Button
        android:onClick="onBackBtnClicked"
        android:text="뒤로가기"
        android:textSize="50dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"/>

</RelativeLayout>

SecondActivity.kt

package com.shinyelee.android_study2

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View

class SecondActivity : AppCompatActivity() {

    val TAG: String = "로그"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)
    }

    fun onBackBtnClicked(view: View) {
        Log.d(TAG, "SecondActivity - onBackBtnClicked() called")
        finish()
    }

}

실습

Login 버튼을 클릭하면 SecondActivity로 이동하고
뒤로가기 버튼을 클릭하면 다시 MainActivity로 이동함


참고

 

Android Studio 4.1에서 제거된 Kotlin Android Extensions을 알아보자. |

I’m an Android Developer.

thdev.tech

 

반응형