activity_main.xml

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="tr.com.emrahkahraman.checkbox.ActivityMain" >
	   		    
	 <LinearLayout android:id="@+id/LinearLayout01" 
	    android:layout_width="fill_parent" 
	    android:layout_height="wrap_content">
	    
	    <CheckBox
        android:id="@+id/chkTurkey"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/turkey"
        android:checked="true" />
	     	   
	</LinearLayout>
	
	<LinearLayout android:id="@+id/LinearLayout02" 
	    android:layout_below="@id/LinearLayout01" 
	    android:layout_width="fill_parent" 
	    android:layout_height="wrap_content">
	    
	    <CheckBox
        android:id="@+id/chkSpain"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/spain" />
	    	   	
	</LinearLayout>

    <LinearLayout android:id="@+id/LinearLayout03" 
	    android:layout_below="@id/LinearLayout02"  
	    android:layout_width="fill_parent" 
	    android:layout_height="wrap_content">
	   
        <CheckBox
        android:id="@+id/chkAzerbaijan"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/azerbaijan" />
        	   	
	</LinearLayout>
	
        <LinearLayout android:id="@+id/LinearLayout04" 
	    android:layout_below="@id/LinearLayout03"  
	    android:layout_width="fill_parent" 
	    android:layout_height="wrap_content">
	   
        <Button android:id="@+id/btnDisplay"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="@string/btnDisplay"
        />  	
	</LinearLayout>

</RelativeLayout>

ActivityMain sınıfı kodları

package tr.com.emrahkahraman.checkbox;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class ActivityMain extends ActionBarActivity {

	private CheckBox chkTurkey;
	private CheckBox chkSpain;
	private CheckBox chkAzerbaijan;
	
	private Button btnDisplay;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		chkTurkey=(CheckBox) findViewById(R.id.chkTurkey);
		chkSpain=(CheckBox) findViewById(R.id.chkSpain);
		chkAzerbaijan=(CheckBox) findViewById(R.id.chkAzerbaijan);
		
		btnDisplay=(Button) findViewById(R.id.btnDisplay);
		btnDisplay.setOnClickListener(clickListener);	
		
	}

	 private OnClickListener clickListener = new OnClickListener() 
	   {
		    public void onClick(View v) 
		    {
		    	switch (v.getId())
		    	{
		    	case R.id.btnDisplay:
			   
		    		StringBuffer result = new StringBuffer();
		    		result.append("Türkiye seçili : ").append(chkTurkey.isChecked());
		    		result.append("\nİspanya seçili : ").append(chkSpain.isChecked());
		    		result.append("\nAzerbaycan seçili :").append(chkAzerbaijan.isChecked());
		     
		    		Toast.makeText(ActivityMain.this, result.toString(),
		    				Toast.LENGTH_LONG).show();
		    		
		    		break;
		    	}
		   }
	};
	@Override
	public boolean onCreateOptionsMenu(Menu menu) 
	{
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) 
	{
		int id = item.getItemId();
		if (id == R.id.action_settings) 
		{
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}

Ekran Görüntüsü
Screenshot_2015-05-06-22-53-44[1]

Proje Kodunu İndirmek İçin Tıklayınız

Etiketler: ,

Yazar : Emrah Kahraman

Bilgisayar Mühendisi

Android Checkbox Kullanımı Yazısı için Yorum Yapabilirsiniz

Sponsor
Alexa
Hakkımda
Bağlantılar