Android

Android – CheckBox Kullanımı

Android’de Görsel bileşenleri anlatmaya kaldığımız yerden devam ediyoruz. Sıradaki bileşenimiz, CheckBox. Türkçeye işaret kutucuğu olarak çevrilen bu bileşen, genellikle yanda gördüğünüz gibi kullanılır. Şimdi bu bileşenin Android‘de kullanımı inceleyelim.

 

Bilmemiz Gereken Public Metodları

[blockquote ] Örneklerde, checkBox ismindeki değişkenin oluşturulduğunu varsaydım. Örnek Uygulama kısmında bu değişkenin nasıl oluşturduğunu görebilirsiniz. [/blockquote]

1- boolean isChecked()

Bu metod CheckBox‘ın işaretli olup olmadığını boolean olarak bize döner. Kullanımı şu şekildedir;

[syntax type=”html|php|js|css”]checkBox.isChecked();[/syntax]

2- void setChecked(boolean)

Bu metodla da CheckBox’ın o anki durumunu ayarlayabilirsiniz. Yani işaretli olsun ya da olmasın gibi. Kullanımı şu şekilde;

[syntax type=”html|php|js|css”]checkBox.setChecked(true); | checkBox.setChecked(false);[/syntax]

3-  boolean getChecked()

Bu metodla da CheckBox’ın o anki durumunu kontrol edebilirsiniz. Yani CheckBox‘ın o anda işaretli olup olmadığını kotnrol edebilirsiniz. Kullanımı şu şekilde;

[syntax type=”html|php|js|css”]checkBox.getChecked(); [/syntax]

4- void setOnCheckedChangeListener(OnCheckedChangeListener)

Bu metod, bileşenimizi dinlememizi sağlar. Eğer kullanıcı bileşenin durumunu değiştirirse yani CheckBox‘a tıklarsa, bunu yakalamak için bu metodu kullanmamız gerekir. Kullanımını aşağıdaki örnek uygulamada görebilirsiniz.

Örnek Uygulama

Şimdi de örnek bir uygulama ile bu bileşenin kullanımını iyice anlayalım.

Öncelikle her zamanki gibi bir proje oluşturalım ve uygulamamıza başlayalım.

Uygulama İçeriği

Uygulamamızda, bir anket sorusu hazırlayacağız. Kullanıcıya hangi işletim sistemlerini kullandığını soracağız. Herhangi bir seçeneğe tıklandığında ekrana Toast kullanarak o seçeneği ekrana basacağız. Kullanıcıda kullandığı işletim sistemlerini işaretledikten sonra gönder butonuna basınca işaretli tüm şıkları ekrana basacağız.

Layout’un Hazırlanması
Ekran Resmi 2014-10-22 13.04.55

activity_main.xml dosyamızı açıyoruz ve tasarım kısmına (Graphical Layout) geçiyoruz. Burada Form Widgets sekmesi altından bir tane TextView, bir tane Button ve altı tane de CheckBox‘ı sürükle bırak mantığıyla Linear Layout‘umuzun içerisine alıyoruz.

Şimdi, XML kısmını aşağıdaki gibi düzenliyoruz.

 

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_margin="10dp"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Aşağıdaki işletim sistemlerinden hangisini/hangilerini kullandınız?"
        android:textSize="16sp"
        android:textStyle="bold" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical" >

            <CheckBox
                android:id="@+id/checkBox1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Android" />

            <CheckBox
                android:id="@+id/checkBox2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="iOS" />

            <CheckBox
                android:id="@+id/checkBox3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Windows Phone" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical" >

            <CheckBox
                android:id="@+id/checkBox4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Windows" />

            <CheckBox
                android:id="@+id/checkBox5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Mac OS X" />

            <CheckBox
                android:id="@+id/checkBox6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Linux" />
        </LinearLayout>
    </LinearLayout>

    <Button
        android:id="@+id/sendButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Gönder"
        android:layout_marginTop="10dp"
        android:layout_gravity="center_horizontal" />

</LinearLayout>

Tasarım kısmında yapacaklarımız bu kadar. Şimdi MainActivity’yi düzenlemeye geçelim.

Kodlama (MainActivity)

MainActivity sınıfını da aşağıdaki gibi güncelleyelim.

public class MainActivity extends Activity {

	private CheckBox cb1;
	private CheckBox cb2;
	private CheckBox cb3;
	private CheckBox cb4;
	private CheckBox cb5;
	private CheckBox cb6;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//Tasarımdaki Checkbox'ları çekiyoruz.
		cb1 = (CheckBox)findViewById(R.id.checkBox1);
		cb2 = (CheckBox)findViewById(R.id.checkBox2);
		cb3 = (CheckBox)findViewById(R.id.checkBox3);
		cb4 = (CheckBox)findViewById(R.id.checkBox4);
		cb5 = (CheckBox)findViewById(R.id.checkBox5);
		cb6 = (CheckBox)findViewById(R.id.checkBox6);
		
		//CheckBox'ların Listener'larını tanımlıyoruz. 
		cb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(cb1.isChecked())
					Toast.makeText(getApplicationContext(), buttonView.getText(), Toast.LENGTH_SHORT).show();
			}
		});
		cb2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(cb2.isChecked())
					Toast.makeText(getApplicationContext(), buttonView.getText(), Toast.LENGTH_SHORT).show();
			}
		});
		cb3.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(cb3.isChecked())
					Toast.makeText(getApplicationContext(), buttonView.getText(), Toast.LENGTH_SHORT).show();
			}
		});
		cb4.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(cb4.isChecked())
					Toast.makeText(getApplicationContext(), buttonView.getText(), Toast.LENGTH_SHORT).show();
			}
		});
		cb5.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(cb5.isChecked())
					Toast.makeText(getApplicationContext(), buttonView.getText(), Toast.LENGTH_SHORT).show();
			}
		});
		cb6.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(cb6.isChecked())
					Toast.makeText(getApplicationContext(), buttonView.getText(), Toast.LENGTH_SHORT).show();
			}
		});
		
		// Butonu tanımlıyoruz ve tıklandığında işaretli şıkları ekrana basıyoruz. 
		Button show = (Button) findViewById(R.id.showButton);
		show.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String cevaplar="Cevaplar: n";
				if(cb1.isChecked())
					cevaplar += " "+cb1.getText();
				if(cb2.isChecked())
					cevaplar += " "+cb2.getText();
				if(cb3.isChecked())
					cevaplar += " "+cb3.getText();
				if(cb4.isChecked())
					cevaplar += " "+cb4.getText();
				if(cb5.isChecked())
					cevaplar += " "+cb5.getText();
				if(cb6.isChecked())
					cevaplar += " "+cb6.getText();
				
				Toast.makeText(getApplicationContext(), cevaplar, Toast.LENGTH_LONG).show();
			}
		});
	}
}

Ekran Görüntüleri

CheckBox

Kaynak Kodlar

Uygulamanın kaynak kodları : https://github.com/mursidyazar/Android-CheckBox.git

Benzer Yazılar

Android Görsel Bileşenler;

  1. TextView ve EditText
  2. ImageView
  3. Button ve StateListDrawable
  4. Toast
  5. ListView
  6. WebView
  7. Spinner
  8. CheckBox
  9. ProgressBar
  10. SeekBar

6 Yorum

Android – ImageView Kullanımı için bir yanıt yazın X