<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/button1"
android:layout_width="80px"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:text="누르세요" />
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="누르세요"/>
</LinearLayout>
package com.gusfree;
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 EventListenerActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);//main.xml을 화면으로!
//1번 버튼을 누르면 a 실행해라 버튼을 찾자
//2번 버튼을 누르면 b 실행해라
//UpCasting 발생
View view =this.findViewById(R.id.button1);
//다운 캐스팅 시키자
//xxx를 찾아라. xxx를 찾았다. 남자xxx;
Button btn1 =(Button)view;
final TextView tv=//메서드 안에 있는 지역 변수
(TextView) this.findViewById(R.id.textView1);
//버튼 누르면 실행될 메서드 연결
//set + OnClickListener
btn1.setText("버튼1");
btn1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
//버튼1을 클릭하면 이쪽으로 제어 이동
tv.setText("버튼1를 눌렀다.");
}
});
//2번 버튼 누르면 "2번 버튼 눌렀다." 표시하기
final Button btn2 =
(Button) this.findViewById(R.id.button2);
btn2.setText("버튼2");
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tv.setText("버튼2를 눌렀다.");
}
});
}
}
- findViewById (btn1)
찾은결과 : View view; //btn1에 해당하는 뷰;
Button button = (Button)view;
neo : 트리니티(딱 1명 존재) 를 찾아달라고 요청
어떤사람 사람1호 = 찾은 결과;
어떤여자 여자1호 = 사람1호;
생명체(Object)
|
생물(View)
| |
인간 동물
| |
여자 남자 (Button)
'Android > 2012.04월 강좌' 카테고리의 다른 글
| Android review (0) | 2012.04.30 |
|---|---|
| 2일차 이벤트2 (0) | 2012.04.27 |
| 2일차 Layout2 (0) | 2012.04.27 |
| 2일차 Layout (0) | 2012.04.27 |
| 2일차 기본3 (0) | 2012.04.27 |
googleKeyBoard.apk