<?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">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2" />
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3" />
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button4" />
<Button
android:id="@+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button5" />
<Button
android:id="@+id/btn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button6" />
<Button
android:id="@+id/btn7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button7" />
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</LinearLayout>
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var listener1 = btnListener1();
btn1.setOnClickListener(listener1)
// 람다식!
btn2.setOnClickListener { tv1.text = "두 번째 버튼" }
// 클래스 아이디
btn3.setOnClickListener(btnListener34())
// 람다식.
btn5.setOnClickListener { tv1.text = "다섯 번째 버튼 눌림" }
// 람다식
var listener4 = View.OnClickListener { view ->
when(view.id){
R.id.btn6 ->
tv1.text ="6번째 버튼"
R.id.btn7->
tv1.text="7번째 버튼"
}
}
btn6.setOnClickListener(listener4)
btn7.setOnClickListener(listener4)
}
inner class btnListener1 : View.OnClickListener {
override fun onClick(p0: View?) {
tv1.text = "첫 번째 버튼을 눌렀습니다."
}
}
inner class btnListener34 : View.OnClickListener {
override fun onClick(p0: View?) {
when (p0?.id) {
R.id.btn3 -> tv1.text = "세 번째 버튼"
R.id.btn4 -> tv1.text = "네 번쨰 버튼"
}
}
}
}
첫 번째로 btn1 을 보면, 자바와 비슷하게 작업을 하고 있다. 자바와 다른점을 찾아보자면 클래스 안에 inner 클래스를 넣을 때, class 앞에 inner을 붙이는 걸 볼 수 있다. 직관성이 좋아졌다고 생각한다. 또한 switch가 아닌 when 을 쓰고 있는데 언어적으로도 가독성이 좋아진 것 같다. (코틀린 뽕) switch 문을 써봤다면 바로 적응이 가능하다.
두 번째로 btn2 를 보면, 람다식이라는 것을 사용하고 있다. 람다식은 안에 정의를 해놓으면 나중에 클래스 파일로 변환된다고 한다. 자바로 작성한 코드랑 비교하면 터무늬 없이 짧고 간결하다. 자바로 할때는 findViewById로 View를 가져오고 버튼에 익명클래스 onClick 오버라이딩하고.. 코틀린에서는 간결하게 가능해진다.
나머지는 동일하게 inner class와 람다식을 사용했는데 when을 이용해 넘어오는 View를 통해 id를 찾아서 해당하는 id일 때, 해당 작업을 하는 분기를 나눌 수도 있다.
이 중에서 사용하고 싶은 것을 사용하면 된다. 또는 상황에 따라서 더 보기 좋은 것을 선택해도 될 것 같다.
궁금하신 점은 덧글로 적어주세요.
'안드로이드' 카테고리의 다른 글
[Android Kotlin] 안드로이드 코틀린 Seek Bar (0) | 2020.03.21 |
---|---|
[Android Kotlin] 안드로이드 코틀린 Progress Bar (0) | 2020.03.21 |
[Android Kotlin] 안드로이드 코틀린 Radio Button (0) | 2020.03.21 |
[Android Kotlin] 안드로이드 코틀린 Check Box (0) | 2020.03.21 |
[Android Kotlin] 안드로이드 코틀린 TextView (0) | 2020.03.21 |