민서네집

[android] 이벤트 핸들링 방법 본문

andorid

[android] 이벤트 핸들링 방법

브라이언7 2010. 9. 14. 15:04


package my.andr.event;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class UIEvent extends Activity implements OnClickListener {

 public void onClick(View v) {
  TextView tv = (TextView) findViewById(R.id.text);
  tv.setText("Background Color : YELLOW");
  tv.setBackgroundColor(0xFFFFFF00);
 }
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  Button b = (Button) this.findViewById(R.id.button);
  b.setOnClickListener(this);
 }
}

/* 주로 3번,4번 방법 특히 4번 방법을 더 많이 사용함 */

/* 1번 방법
public class UIEvent extends Activity {
 class ClickHandler implements OnClickListener {
  public void onClick(View v) {
   TextView tv = (TextView) findViewById(R.id.text);
   tv.setText("Background Color : YELLOW");
   tv.setBackgroundColor(0xFFFFFF00);
  }
 }

 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  Button b = (Button) this.findViewById(R.id.button);
  OnClickListener ocl = new ClickHandler();
  b.setOnClickListener(ocl);
 }
}
*/

/* 2번 방법
public class UIEvent extends Activity implements OnClickListener {

 public void onClick(View v) {
  TextView tv = (TextView) findViewById(R.id.text);
  tv.setText("Background Color : YELLOW");
  tv.setBackgroundColor(0xFFFFFF00);
 }
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  Button b = (Button) this.findViewById(R.id.button);
  b.setOnClickListener(this);
 }
}
*/

/* 3번 방법
public class UIEvent extends Activity {

 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  Button b = (Button) this.findViewById(R.id.button);
  OnClickListener ocl = new OnClickListener() {
   @Override
   public void onClick(View v) {
    TextView tv = (TextView) findViewById(R.id.text);
    tv.setText("Background Color : YELLOW");
    tv.setBackgroundColor(0xFFFFFF00);
   }
  };
  b.setOnClickListener(ocl);
 }
}
*/

/* 4번 방법
public class UIEvent extends Activity {

 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  Button b = (Button) this.findViewById(R.id.button);
  b.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    TextView tv = (TextView) findViewById(R.id.text);
    tv.setText("Background Color : YELLOW");
    tv.setBackgroundColor(0xFFFFFF00);
   }
  });
 }
}
*/

'andorid ' 카테고리의 다른 글

[android] intent를 설정하는 여러가지 방법  (0) 2010.09.14
Comments