刚接触Android和Java感觉最让我头晕的就是那种Free Style的书写格式,太让人崩溃了,就好比事件的Listener那样,比如onClickListener事件就有好几种书写格式,
下边就总结一下吧。
1、从控件的接口中直接继承并实现监听的方法,例如:
//直接继承控件的接口
public
class
MyFirstActivity
extends
Activity
implements
Button.OnClickListener
//在onCreate函数中指定控件的监听方法
m_Btn_test1 = (Button)findViewById(R.id.btn_TEST1);
m_Btn_test1.setOnClickListener(this);
//实现Listener的回调函数
public void onClick(View v)
2、从View类的接口中继承并实现监听的方法,例如:
//直接继承View的接口
public
class
MyFirstActivity
extends
Activity
implements
OnClickListener
//在onCreate函数中指定控件的监听方法与在控件中的书写基本一致。
3、直接在类的内部实现监听
//在onCreate函数中指定控件的监听方法
m_Btn_test1 = (Button)findViewById(R.id.btn_TEST1);
m_Btn_test1.setOnClickListener(
new
Button.OnClickListener(){
public void onClick(view v){
//test
}
});