안드로이드 개발 질문/답변
(글 수 45,052)
package exam.ButtonEdit;
import android.app.*; import android.os.*; import android.view.*; import android.widget.*; import android.os.Bundle;
public class ButtonEdit extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btn=(Button)findViewById(R.id.btn); btn.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v){ EditText edit=(EditText)findViewById(R.id.edit); String str=edit.getText().toString(); Toast.makeText(ButtonEdit.this,str,Toast.LENGTH_SHORT).show(); } }); } }
다름이 아니라 Toast.makeText(ButtonEdit.this,str,Toast.LENGTH_SHORT).show(); 이분이 궁금한데요
왜 ButtonEdit.this 인지 이유를 모르겠어요 액티비티넣으면 되는지 알고 this를 넣어봤더니 안되고
ButtonEdit.this를 프린트로 찍어봤더니 로그에서는 ButtonEdit.ButtonEdit 이렇게 찍히는데 이유가 궁금합니다
그리고 또하나 LinearLayout linear= new LinearLayout(this); 이렇게 LinearLayout 생성할때 액티비티를 this로 넘겼는데
이 this도 프린터로 찍어보면 액티비티.액티비티 로 찍히는데 이유가 뭐죠? 자바에서는 예를 들면
Child extends Parent 라는 클래스가있으면 차일드 클래스 안에서 this를 찍어보면 Child.Child 가 찍히는게 아니라
Child가 찍히자나여 ㅠㅠ
2010.06.28 12:53:28
Toast.makeText api를 보시면 첫째 인자가 Context 나 Activity 로 되어 있습니다. 그래서 현재 Activity인 ButtonEdit.this를 넣으신거죠.
아마 context를 넣으셔도 동일할 거 같네요.
아마 context를 넣으셔도 동일할 거 같네요.
2010.06.28 13:26:23
w public void onClick(View v){
EditText edit=(EditText)findViewById(R.id.edit);
String str=edit.getText().toString();
System.out.println("[Button]"+this);
Toast.makeText(ButtonEdit.this,str,Toast.LENGTH_SHORT).show();
}
});
제가 그래서 안에다가 this 를 넣어봤는데 ButtonEdit.ButtonEdit 이찍히는데요 ㅡㅡ;
EditText edit=(EditText)findViewById(R.id.edit);
String str=edit.getText().toString();
System.out.println("[Button]"+this);
Toast.makeText(ButtonEdit.this,str,Toast.LENGTH_SHORT).show();
}
});
제가 그래서 안에다가 this 를 넣어봤는데 ButtonEdit.ButtonEdit 이찍히는데요 ㅡㅡ;
2010.06.28 14:52:19
원조안드로이드님 말씀이 맞네요.
this는 Button.OnClickListener 인터페이스를 포인팅하고 있습니다.
아마 위에 코드를 출력할때 [패키지명.클래스명].[클래스명]@[고유번호] 로 나뉠껍니다.
고유번호는 제생각으로는 객체를 가리키는 스택영역에 남아있는 해시코드일듯 싶네요.
자세히보시면 고유번호가 다른걸 알수있습니다.
그러므로 인터페이스가 구현된 영역에서는 인터페이스를 포인팅하고 있기때문에
Intent 클래스의 인자값으로 넘겨줘야할 Context객체는 Activity안에 구현이 되어있기때문에
Context가 구현이된 객체를 넘겨줘야합니다.
Java에 대해서 공부를 좀 더 하시면 이해할 수 있습니다.