- Anasayfa
- Android Programlama
- Android CheckBox Kullanımı ve Örneği
Android CheckBox Kullanımı ve Örneği

- Yazar: Savaş Dersim Çelik
- Kategori: Android Programlama
- Tarih: Pazar Haziran 11th, 2017
- 1.965 Görüntülenme
- Beğenenler: 0 Like
- Yorum Yok Yorum Yap
Android CheckBox, işaretli veya işaretlenmemiş iki durum düğmesinin bir türüdür.
Onay kutularının çok fazla kullanılması olabilir. Örneğin, kullanıcının hobisini bilmek, belirli eylemi etkinleştirmek / devre dışı bırakmak vb. Için kullanılabilir.
Android CheckBox sınıfı, CompoundButton sınıfının alt sınıfıdır.
Android.widget.CheckBox sınıfı, CheckBox’ları oluşturma olanağı sağlar.
CheckBox sınıfında View, TextView ve Button sınıflarının devralınan yöntemleri vardır. Bazıları şöyledir:
Method | Açıklama |
---|---|
public boolean isChecked() | Aksi takdirde işaretlenirse true, aksi halde false döndürür. |
public void setChecked(boolean status) | CheckBox durumunu değiştirir. |
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="Pizza" /> <CheckBox android:id="@+id/checkBox2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/checkBox1" android:text="Kahve" /> <CheckBox android:id="@+id/checkBox3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/checkBox2" android:text="Hamburger" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/checkBox2" android:layout_marginTop="32dp" android:layout_toLeftOf="@+id/checkBox3" android:text="EKLE" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="125dp" android:text="Webinyo.COM" android:textSize="24sp" /> </RelativeLayout>
MainActivity.java
import android.os.Bundle; import android.app.Activity; import android.view.View; import android.view.View.OnClickListener; import android.widget.*; public class MainActivity extends Activity { CheckBox pizza,coffe,burger; Button buttonOrder; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); addListenerOnButtonClick(); } public void addListenerOnButtonClick(){ //Getting instance of CheckBoxes and Button from the activty_main.xml file pizza=(CheckBox)findViewById(R.id.checkBox1); coffe=(CheckBox)findViewById(R.id.checkBox2); burger=(CheckBox)findViewById(R.id.checkBox3); buttonOrder=(Button)findViewById(R.id.button1); //Applying the Listener on the Button click buttonOrder.setOnClickListener(new OnClickListener(){ @Override public void onClick(View view) { int totalamount=0; StringBuilder result=new StringBuilder(); result.append("Seçilen Ürün"); if(pizza.isChecked()){ result.append("\nPizza 100 TL"); totalamount+=100; } if(coffe.isChecked()){ result.append("\nKahve 50 TL"); totalamount+=50; } if(burger.isChecked()){ result.append("\nHamburger 120 TL"); totalamount+=120; } result.append("\nToplam: "+totalamount+" TL"); //Displaying the message on the toast Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_LONG).show(); } }); } }
Konu İle İlgili Soru, Görüş ve Öneriler