Mehmet Ethem SULAN :: Android :: Radio Button Group and Button setOnClickListener() Android Example

Android ile Radio Button Group nasıl oluşturulacağını ve button tıklanıldığında radio button idsi ve kaçıncı radio button olduğunu texte yazdırıyorum. Android 3.0 ile gruplandırılmış bir palette gelmiş ve kullanımı basit. Eğer Android kurmasını bilmiyorsanız install google android örneğine bakabilirsiniz.Java addActionListener örneğinde olduğu gibi componentIsmi.eklenecekOlay (bt.setOnClickListener gibi) şeklinde bileşenlere event listener ekleniyor.
Kodu çalıştırdığınızda aşağıdaki resimde görünen kilide iki sefer tıkladıktan sonra uygulamanızı seçip deneyebilirsiniz.

start


Drag drop ekranı.

palette


Son olarak bir radio button seçili geliyor ve button tıklanıldığında alttaki texte değer yazdırma.

çıktı


main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content">
        <RadioButton android:text="RadioButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/radio0" android:checked="true"></RadioButton>
        <RadioButton android:text="RadioButton" android:id="@+id/radio1" android:layout_width="wrap_content" android:layout_height="wrap_content"></RadioButton>
        <RadioButton android:text="RadioButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/radio2"></RadioButton>
        <RadioButton android:text="RadioButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/radio3"></RadioButton>
    </RadioGroup>
    <EditText
    android:layout_height="wrap_content"
    android:id="@+id/editText1"
     android:text="EditText"
     android:layout_width="match_parent">
     </EditText>
    <Button android:text="Button"
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    </Button>
</LinearLayout>

Ozellik.java

package com.ethem.attribute;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class Ozellik extends Activity {
    private EditText text;
    private RadioButton rb1;
    private RadioButton rb2;
    private RadioButton rb3;
    private RadioButton rb4;
    private Button bt;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
//Temel mantik main.xml de tanimlanan componentlere .java sinifindan
//id ile erismek.
        rb1=(RadioButton)findViewById(R.id.radio0);
        rb2=(RadioButton)findViewById(R.id.radio1);
        rb3=(RadioButton)findViewById(R.id.radio2);
        rb4=(RadioButton)findViewById(R.id.radio3);
        //text baglama
        text=(EditText)findViewById(R.id.editText1);
        //on click property
        bt=(Button)findViewById(R.id.button1);
        bt.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				if(rb1.isChecked()){
					text.setText("Birinci radio button Secildi. Id:"+rb1.getId());
				}else if(rb2.isChecked()){
					text.setText("Ikinci radio button secildi.Id:"+rb2.getId());
				}else if(rb3.isChecked()){
					text.setText("Ucuncu radio button secildi.Id:"+rb3.getId());
				}else if(rb4.isChecked()){
					text.setText("Dorduncu radio button secildi.Id:"+rb4.getId());
			    }else{
			    	text.setText("Radio buton secilmedi!");
			    }

			}
		});

    }
}

Uygulamayı indirmek için tıklayın.

Comments

There is one comment for this post.

  1. Göksel GÜREN on Nisan 11, 2011 3:51 pm

    Güzel bir anlatım olmuş Ethem.

Write a Comment

Let me know what you think?