본문 바로가기
옛날

버튼 이벤트 처리 [Android]

by 차가운게 조아 2015. 12. 11.

 

버튼은 단순한 차일드 위젯이고 버튼클래스를 바로 사용하는것이 보통이라 상속받지않고 이벤트 처리가능해야함

 

그래서 클릭 이벤트에 대해선 콜백 메서드가 정의되어 있지 않으며 반드시 리스너로 이벤트를 받어야한다.

 

 

첫번째방법


 

<?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"
android:gravity="center_horizontal"
>

<TextView
android:id="@+id/fruit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffff00"
android:textSize="20pt"
android:text="과일"
/>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
>

<Button
android:id="@+id/apple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Apple"
/>

<Button
android:id="@+id/orange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Orange"
/>
< /LinearLayout>
< /LinearLayout>


 

package test.Layout;

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;

public class Layout extends Activity 
{ 
    public void onCreate(Bundle savedInstanceState) 
    {
     super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  findViewById(R.id.apple).setOnClickListener(mClickListener);
  findViewById(R.id.orange).setOnClickListener(mClickListener);
    }

 Button.OnClickListener mClickListener = new View.OnClickListener() {
  public void onClick(View v) {
   TextView textFruit=(TextView)findViewById(R.id.fruit);
   switch (v.getId()) {
   case R.id.apple:
    textFruit.setText("Apple");
    break;
   case R.id.orange:
    textFruit.setText("Orange");
    break;
   }
  }
 };
}

 


 

방법 2 - 리스너 1개로 여러뷰에대해 등록

 

package test.Layout;

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;

public class Layout extends Activity implements View.OnClickListener 
{ 
    public void onCreate(Bundle savedInstanceState) 
    {
     super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  Button btnApple=(Button)findViewById(R.id.apple);
  btnApple.setOnClickListener(this);
  Button btnOrange=(Button)findViewById(R.id.orange);
  btnOrange.setOnClickListener(this);
    }

 public void onClick(View v) {
  TextView textFruit=(TextView)findViewById(R.id.fruit);
  switch (v.getId()) {
  case R.id.apple:
   textFruit.setText("Apple");
   break;
  case R.id.orange:
   textFruit.setText("Orange");
   break;
  }
 }
}

- OnClickListener 인터페이스르 ㄹ직접구현했으므로 리스너는 액티비티 자신인 this다.

 

- 리스너를 칭할수있는 방법이 생겨서 리스너 등록 메소드로 this를 전달함

 

- onClick메서드는 누구를 클릭했는지 View의 v를 전달받으며 v의 getId를 통해 클릭된 버튼을 알아낸다.

 

 

 

 

 

 


 

 

 

 

 

방법 3 - 최상위 액티비티는 두고, 별도 리스너 객체를 멤버 선언후 이걸 리스너로 사용 하는 방법

 

package test.Layout;

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;

public class Layout extends Activity
{ 
    public void onCreate(Bundle savedInstanceState) 
    {
     super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  Button btnApple=(Button)findViewById(R.id.apple);
  btnApple.setOnClickListener(new Button.OnClickListener() {
   public void onClick(View v) {
    TextView textFruit=(TextView)findViewById(R.id.fruit);
    textFruit.setText("Apple");
   }
  });

  Button btnOrange=(Button)findViewById(R.id.orange);
  btnOrange.setOnClickListener(new Button.OnClickListener() {
   public void onClick(View v) {
    TextView textFruit=(TextView)findViewById(R.id.fruit);
    textFruit.setText("Orange");
   }
  });
    }
}


'옛날' 카테고리의 다른 글

버튼 링크 [Android]  (0) 2015.12.11
안드로이드 웹뷰 [Android]  (0) 2015.12.11
Layout xml 속성 [Android]  (0) 2015.12.10
소프트웨어 버전 정보 이해하기  (0) 2015.12.10
UIWebview [IOS][Object-C]  (0) 2015.12.10