CheckBox Örneği

CheckBox Örneği

 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 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:layout_width="match_parent"
   android:layout_height="match_parent"
   android:layout_margin="20dp"
   android:orientation="vertical"
   tools:context=".MainActivity">


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

        <Switch
           android:id="@+id/switch_KonumServisi"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:text="Konum Servisleri" />
    </LinearLayout>

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

        <Switch
           android:id="@+id/switch_KonumAl"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:paddingLeft="20dp"
           android:text="Konum Bilgilerini Al" />
    </LinearLayout>

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

        <Switch
           android:id="@+id/switch_KonumGonder"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:paddingLeft="20dp"
           android:text="Konum Bilgilerini Gönder" />


    </LinearLayout>

    <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_marginTop="100dp"
       android:gravity="center"
       android:orientation="horizontal">

        <TextView
           android:id="@+id/Mesaj"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           />

    </LinearLayout>

</androidx.appcompat.widget.LinearLayoutCompat>

 

MainActivity.java

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    Switch konumServisleri, konumGonder, konumAl;
    TextView Mesaj;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        konumServisleri = findViewById(R.id.switch_KonumServisi);
        konumAl = findViewById(R.id.switch_KonumAl);
        konumGonder = findViewById(R.id.switch_KonumGonder);
        Mesaj = findViewById(R.id.Mesaj);

        /*if(konumServisleri.isChecked()) {
            konumAl.setVisibility(View.VISIBLE);
            konumGonder.setVisibility(View.VISIBLE);
        }
        else {*/

            konumAl.setVisibility(View.GONE);
            konumGonder.setVisibility(View.GONE);
        //}
    }

    @Override
    protected void onResume(){
        super.onResume();

        konumServisleri.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(konumServisleri.isChecked()) {
                    konumAl.setVisibility(View.VISIBLE);
                    konumGonder.setVisibility(View.VISIBLE);
                }
                else {
                    konumAl.setVisibility(View.GONE);
                    konumGonder.setVisibility(View.GONE);
                }

                Onayla();
            }
        });

        konumAl.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                Onayla();
            }
        });

        konumGonder.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                Onayla();
            }
        });
    }

    public void Onayla() {

        String Mesajim="";

        if(!konumServisleri.isChecked()) {
            Mesajim ="Konum Servisleri Kapalı";
        }
        else {
            if(konumAl.isChecked()==true && konumGonder.isChecked()==true) {
                Mesajim ="Konum Al ve Konum Gönder Açık";
            }
            else if(konumAl.isChecked()==false && konumGonder.isChecked()==false) {
                Mesajim ="Konum Al ve Konum Gönder Kapalı";
            }
            else if(konumAl.isChecked()==true && konumGonder.isChecked()==false) {
                Mesajim ="Konum Al Açık ve Konum Gönder Kapalı";
            }
            else {
                Mesajim ="Konum Al Kapalı ve Konum Gönder Açık";
            }
        }

        Mesaj.setText(Mesajim);
    }
}

 

 

Yorumunuzu Ekleyin

Yükleniyor...