[안드로이드 스튜디오/코틀린] 리스너, 메소드, 클릭 이벤트
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()
}
}
실습
참고
반응형
'개발(Android) > android studio' 카테고리의 다른 글
[Android Studio/Kotlin] Singleton Pattern (0) | 2022.04.20 |
---|---|
[Android Studio/Kotlin] Custom Dialog, Interface (0) | 2022.04.20 |
[Android Studio/Kotlin] Linear Layout, Relative Layout, Constraint Layout (0) | 2022.04.17 |
[Android Studio/Kotlin] Life Cycle of Activity (0) | 2022.04.17 |
[Android Studio/Kotlin] Gradient View (0) | 2022.04.16 |