Genel

Android Studio Listener ile Button Kontrolü

Herkese merhaba, Bu seferki dersimizin amacı switch case yapısı kullanarak birden fazla butonu tek bir listener ile kontrol edicez. Yani biz her button için tek tek tıklama olayı tanımlamıştık şimdi ise tek bir tıklamada hepsini kontrol ediceğiz. Mesela 1 button 2 sayıyı toplasın. 1 butonumuz text View’e Ali yazdırsın. 1 Buton’da Toast Mesaj göstererk Button1 ‘e tıkladınız desin.

Tasarım Kısmı:

2015-07-25_00h08_062015-07-25_00h08_12

Tasarımın Kodları :

<RelativeLayout xmlns:android="//schemas.android.com/apk/res/android"
    xmlns:tools="//schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_below="@+id/button2"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button2"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="101dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button3"
        android:layout_below="@+id/button"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/textView"
        android:layout_below="@+id/button3"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="84dp" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText2"
        android:layout_below="@+id/editText"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

Java Kodumuz : View.OnClickListener’dan ‘dan kalıtım implement alıyoruz. Ve butonlarımızı tanıtıyoruz. ve hepsini tek bir noktada tıklama veriyoruz. Altta ise aldığımız getId’leri ile her buton’un idsi için işlem yaptırıyoruz.

2015-07-25_00h07_58

2015-07-25_00h07_48

Java Kodumuz :

package com.umiitkose.myapplication;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity implements View.OnClickListener{
    TextView tv;
    Button b1,b2,b3;
    EditText a1,a2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b1=(Button)findViewById(R.id.button);
        b2=(Button)findViewById(R.id.button2);
        b3=(Button)findViewById(R.id.button3);
        b1.setOnClickListener(this);
        b2.setOnClickListener(this);
        b3.setOnClickListener(this);
        tv=(TextView)findViewById(R.id.textView);
        a1=(EditText)findViewById(R.id.editText);
        a2=(EditText)findViewById(R.id.editText2);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button:
                Toast.makeText(getApplicationContext(), "Buton 1'e tıklandı.", Toast.LENGTH_SHORT).show();
                break;
            case R.id.button2:
                tv.setText("Ali");
                break;

            case R.id.button3:
                float sayi1float=Float.parseFloat(a1.getText().toString());

                float sayi2float=Float.parseFloat(a2.getText().toString());

                tv.setText(String.valueOf(sayi1float+sayi2float));

                break;
        }
    }
    }

Uygulamanın Çalışması.

2015-07-25_00h07_15 2015-07-25_00h07_30 2015-07-25_00h07_36

 

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir