ToggleButton ve Switch Kontrolü
Basılı veya çekili durabilen özel bir butondur. ON/OFF (Wifi/Bluetooth) gibi durumları kontrol etmek için kullanırız.
Android 4.0’dan sonra ToggleButton
benzeri olan Switch
kontrolü eklenmiştir.Bu iki kontrol CompoundButton klasının alt sınıfıdır.
Önemli ToggleButton ve Switch Attribute’leri
ToggleButton ve Switch özellikleri tamamen aynıdır, her ikisinde de kullanılabilir.
android:textOff: Kapalı durumda iken görünecek olan yazı
android:textOn: Açık durumda iken görünecek yazı
androd:checked: Görüneceği ilk durumu ayarlamak için kullanılır, true veya false değerini alır
Örnek
Switch: Açık kapalı durumuna göre Wifi durumunu textView’e yazıcak
ToggleButton: Açık kapalı durumuna göre Constrait Layout’un rengini değiştiricek
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 | <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/consLayout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Switch android:id="@+id/switchButton" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_marginTop="32dp" android:checked="true" android:text="WİFİ" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/txtWifiDurum" /> <ToggleButton android:id="@+id/toggleButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="6dp" android:layout_marginTop="32dp" android:checked="true" android:text="ToggleButton" android:textOff="Kapalı" android:textOn="Açık" app:layout_constraintStart_toStartOf="@+id/switchButton" app:layout_constraintTop_toBottomOf="@+id/switchButton" /> <TextView android:id="@+id/txtWifiDurum" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="64dp" android:layout_marginEnd="8dp" android:textSize="18sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> |
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 | package com.example.androidkontrolleri; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.widget.CompoundButton; import android.widget.Switch; import android.widget.TextView; import android.widget.ToggleButton; import androidx.appcompat.app.AppCompatActivity; import androidx.constraintlayout.widget.ConstraintLayout; public class MainActivity extends AppCompatActivity { private ConstraintLayout consLayout; private Switch switchButton; private ToggleButton toggleButton; private TextView txtWifiDurum; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); consLayout = findViewById(R.id.consLayout); switchButton = findViewById(R.id.switchButton); toggleButton = findViewById(R.id.toggleButton); txtWifiDurum = findViewById(R.id.txtWifiDurum); //setOnClickListener'de kullanabilirdik ama switch için genellikle bu method kullanılır switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { //Switch Button işaretli ise true döner if (isChecked) { txtWifiDurum.setText("Wifi Açık"); } else { txtWifiDurum.setText("Wifi Kapalı"); } } }); toggleButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //ToggleButton işaretli ise true döner if (toggleButton.isChecked()) { consLayout.setBackgroundColor(Color.GREEN); } else { consLayout.setBackgroundColor(Color.YELLOW); } } }); } } |
