▣ Android Widget[Component] - Button
1. Project 생성
2. main.xml widget 추가
<?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">
<Button
android:id="@+id/b01"
android:layout_width="100px"
android:layout_height="400px"
android:text="버튼"/>
</LinearLayout>
3. ButtonTestActivity.java 버튼 이벤트 추가
package soo.ui.simple;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class ButtonTestActivity extends Activity {
/** Called when the activity is first created. */
Button b; //전역에서 사용하기위해 선언
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b = (Button)findViewById(R.id.b01); //main.xml에 추가한 버튼 id
1. 내부Class이용(inner Class)
3. 자신 클래스 호출
}
2. 제3클래스
※ Event Handling : Type 4
1. 내부Class이용(inner Class) : 외부클래스 자원(멤버변수/메소드) 을 사용
※delegate 이벤트처리 모델(위임형 이벤트처리모델)
- 익명(무명)
//1. 내부Class 사용방법 - 익명클래스
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(ButtonTestActivity.this, "익명테스트", Toast.LENGTH_SHORT);
b.setText("재발제어");
}
});
- 유명
b.setOnClickListener(new InnerHandler());
//유명클래스
class InnerHandler implements onClickListener {
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(ButtonTestActivity.this, "유명테스트", Toast.LENGTH_SHORT);
b.setText("유명처리제어");
}
}
※delegate 이벤트처리 모델(위임형 이벤트처리모델)
2. 제 3 Class
※delegate 이벤트처리 모델(위임형 이벤트처리모델)
b.setOnClickListener(new ThirdHandler(this));
class ThirdHandler implements onClickListener {
ButtonTestActivity bta;
ThirdHandler(ButtonTestActivity bta){
this.bta = bta;
}
public void onClick(View v){
// TODO Auto-generated method stub
Toast.makeText(bta, "제3클래스", Toast.LENGTH_SHORT).show();
//bta.b.setText("제3클래스방식");
//bta.getB().setText("제3클래스방식"); //정보은닉성 구현
Button bb = (Button)v;
bb.setText("명시적");
}
3. 자기 자신 처리(Self Event)
public class ButtonTestActivity extends Activity implements View.OnClickListener
b.setOnClickListener(this);
public void onClick(View v){
// TODO Auto-generated method stub
Toast.makeText(ButtonTestActivity.this, "자신의 클래스처리", Toast.LENGTH_SHORT).show();
b.setText("자신의 클래스처리");
}
JAVA Tip
Interface : 객체를 생성하기 위한 틀
멤버변수는 전부 -> 상수
메소드는 전부 -> 추상메소드
public class View {
public static abstract interface onClickListener { //abstract 생략가능
public abstract void onClick(View v); //abstract 생략가능
final int I = 100; //final 생략가능
}
}
접근제한자 : public
* class A() - 11개의 method를 가지고 있다. ( class A extends Object() )
* class A()
class B()
* public class A()
class B()
기타버튼
main.xml
<?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" >
<Button
android:id="@+id/b01"
android:layout_width="100px"
android:layout_height="100px"
android:text="버튼"/>
<ImageButton
android:id="@+id/ib01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ib"/>
<ToggleButton
android:id="@+id/tb01"
android:layout_width="100px"
android:layout_height="wrap_content"
android:src="@drawable/ib"/>
</LinearLayout>
'나 어제 배웠다 > Android' 카테고리의 다른 글
개발관련 참고 사이트 (0) | 2010.03.16 |
---|---|
Android Widget[Component]-ImageView (0) | 2010.03.16 |
Android 모양구성-FrameLayout (0) | 2010.03.16 |
Android 모양구성-RelativeLayout (0) | 2010.03.15 |
Android 모양구성-LinearLayout (0) | 2010.03.15 |