相信很多像我一样的新手学习ANDROID开发会遇到这个问题,通过这几天的归类和总结,将我的理解写在下面,欢迎大家一起前来讨论:
以按钮BUTTON的监听事件为例,以下的监听实现都是等价的:
1.使用接口继承按钮监听方法:
-
package dickren123.hui.say_hello_to_world;
-
-
import android.app.Activity;
-
import android.os.Bundle;
-
import android.view.View;
-
import android.widget.Button;
-
import android.widget.TextView;
-
-
public class Hello_to_worldActivity extends Activity implements Button.OnClickListener{
-
-
private Button btn_say_hello;
-
private TextView hello_world;
-
@Override
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.main);
-
btn_say_hello = (Button)findViewById(R.id.bnt_SAY_HELLO_TO_WORLD);
-
hello_world = (TextView)findViewById(R.id.textView1);
-
btn_say_hello.setOnClickListener(this) ;
-
}
-
public void onClick(View v) {
-
-
hello_world.setText("dickren123!");
-
}
-
}
2.使用接口继承view类的监听方法
-
package dickren123.hui.say_hello_to_world;
-
-
-
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 Hello_to_worldActivity extends Activity implements OnClickListener{
-
-
private Button btn_say_hello;
-
private TextView hello_world;
-
@Override
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.main);
-
btn_say_hello = (Button)findViewById(R.id.bnt_SAY_HELLO_TO_WORLD);
-
hello_world = (TextView)findViewById(R.id.textView1);
-
btn_say_hello.setOnClickListener(this) ;
-
}
-
public void onClick(View v) {
-
-
hello_world.setText("dickren123!");
-
}
-
}
3.不用接口,在类内部直接实现监听
-
package dickren123.hui.say_hello_to_world;
-
-
import android.app.Activity;
-
import android.os.Bundle;
-
import android.view.View;
-
import android.widget.Button;
-
import android.widget.TextView;
-
-
public class Hello_to_worldActivity extends Activity {
-
-
private Button btn_say_hello;
-
private TextView hello_world;
-
@Override
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.main);
-
btn_say_hello = (Button)findViewById(R.id.bnt_SAY_HELLO_TO_WORLD);
-
hello_world = (TextView)findViewById(R.id.textView1);
-
btn_say_hello.setOnClickListener(new Button.OnClickListener(){
-
public void onClick(View v) {
-
-
hello_world.setText("dickren123!");
-
}
-
}) ;
-
}
-
}
如果不使用匿名实例,也可以定义一个具体的实例,如下:
-
package dickren123.hui.say_hello_to_world;
-
-
import android.app.Activity;
-
import android.os.Bundle;
-
import android.view.View;
-
import android.widget.Button;
-
-
public class Hello_to_worldActivity extends Activity {
-
-
private Button btn_say_hello;
-
-
@Override
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.main);
-
btn_say_hello = (Button)findViewById(R.id.bnt_SAY_HELLO_TO_WORLD);
-
btn_listener bl = new btn_listener();
-
btn_say_hello.setOnClickListener(bl);
-
}
-
}
-
class btn_listener implements Button.OnClickListener
-
{
-
public void onClick(View v) {
-
-
-
}
-
}
阅读(1599) | 评论(0) | 转发(0) |