Kotlin ile Button Event’ları
Merhaba Arkadaşlar,
Bu dersimizde kotlin ile button ‘a tıklanma olaylarını öğreneceğiz.
1 -> Burada basit olarak bir projeyi sıfırdan açabildiğinizi varsayıyorum. (İlk kez android kurulumu yapanlar için bakması gereken kaynak aşağıdadır. )
//codelabs.developers.google.com/codelabs/kotlin-android-training-get-started/#0
//codelabs.developers.google.com/codelabs/kotlin-android-training-app-anatomy/#10
Projeyi oluşturdunuz ve artık Mainactivity layout dosyasını açın.
Öncelikle aşağıdaki şekilde bir xml yapısı oluşturuyoruz.
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="//schemas.android.com/apk/res/android" xmlns:app="//schemas.android.com/apk/res-auto" xmlns:tools="//schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:text="Button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn_tiklama" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp" android:layout_marginRight="8dp" app:layout_constraintStart_toStartOf="parent" android:layout_marginLeft="8dp" android:layout_marginStart="8dp" android:onClick="onButtonClicked" app:layout_constraintHorizontal_bias="0.498" tools:layout_editor_absoluteY="283dp" tools:ignore="MissingConstraints"/> <Button android:text="Button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn_tiklama1" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp" android:layout_marginRight="8dp" app:layout_constraintStart_toStartOf="parent" android:layout_marginLeft="8dp" android:layout_marginStart="8dp" android:layout_marginTop="80dp" app:layout_constraintTop_toTopOf="parent" android:onClick="onButtonClicked" app:layout_constraintHorizontal_bias="0.498"/> <TextView android:text="TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv_gosterim" android:layout_marginTop="8dp" app:layout_constraintTop_toBottomOf="@+id/btn_tiklama" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp" android:layout_marginRight="8dp" app:layout_constraintStart_toStartOf="parent" android:layout_marginLeft="8dp" android:layout_marginStart="8dp"/> <Button android:text="Button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn_tiklama2" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp" android:layout_marginRight="8dp" app:layout_constraintStart_toStartOf="parent" android:layout_marginLeft="8dp" android:layout_marginStart="8dp" android:layout_marginTop="192dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintHorizontal_bias="0.498"/> </androidx.constraintlayout.widget.ConstraintLayout> |
Yukardaki xml ‘de 3 tane button ve 1 tane text view olan bir yapı göreceksiniz.



Şimdi sıra geldi button event ‘larına. 2 tane işlem ile button ‘lara tıklama özelliği verebiliriz.
1–> setOnclickListener özelliği ile çağırmak. Örnek;
1 2 3 4 5 6 7 | tv_gosterim = findViewById(R.id.<em>tv_gosterim</em>) btn_tiklama2.setOnClickListener <strong>{ </strong>Toast.makeText(this,"Button 2 'ye basıldı",Toast.<em>LENGTH_LONG</em>).show() tv_gosterim.setText("Btn 2 'ye tıklandı") <strong>} </strong>Log.i(TAG,"Main Activity Log") } |
2 –> OnClickListener interface ‘ini implemente ederek.
1 2 | class MainActivity : AppCompatActivity(), View.OnClickListener { Yukarıdaki şekilde ilgili class 'ınız View.OnClickListener 'ı implemente etmeniz gerekiyor. Sonrasında sizlere yeni bir metod verecektir. orada ilgili tanımlamaları yapabilirsiniz. |
1 2 3 4 5 6 7 | override fun onClick(view: View?) { if (view != null) { when(view.<em>id</em>){ //kodlar } } |
ve Evet artık işlem tamamdır. Kotlin ‘de button tıklama işlemlerini yapabilirsiniz.
MainActivity ‘nin tüm kod hali. (ilgili class ‘ın ismini güncellemeyi unutmayın. )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | package com.umiitkose.androidexampleswithkotlin.example.onclick import android.os.Bundle import android.util.Log import android.view.View import android.widget.TextView import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import com.umiitkose.androidexampleswithkotlin.R import kotlinx.android.synthetic.main.ui_onclick_example.* class OnClickedActivity : AppCompatActivity(), View.OnClickListener { companion object{ val TAG: String = "MainActivity" } private lateinit var tv_gosterim:TextView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.<em>ui_onclick_example</em>) tv_gosterim = findViewById(R.id.<em>tv_gosterim</em>) btn_tiklama2.setOnClickListener <strong>{ </strong>Toast.makeText(this,"Button 2 'ye basıldı",Toast.<em>LENGTH_LONG</em>).show() tv_gosterim.setText("Btn 2 'ye tıklandı") <strong>} </strong>Log.i(TAG,"Main Activity Log") } fun onButtonClicked(view: View){ when(view.<em>id</em>){ R.id.<em>btn_tiklama </em>->tv_gosterim.<em>text </em>= ("Buttona Tıklandı") R.id.<em>btn_tiklama1 </em>->tv_gosterim.<em>text </em>= ("Button1 Tıklandı") } } override fun onClick(view: View?) { if (view != null) { when(view.<em>id</em>){ //kodlar } } } } |
Projenin kaynak kodları //github.com/umiitkose/KotlinAndroidExamples.git adresinden erişebilirsiniz.
İlgili uygulama onclick package ‘ı içerisindedir.













