我们在Android编程中经常会看到为控件设置监听器。今天在工作中发现有好多自定义的监听器,便写了一遍博客,让我们来讲一讲如何自定义一个监听器;
首先让我们先分析一下如果自定义一个监听器需要哪些条件:
(1)定义一个接口(监听器)
(2)在什么条件下调用我们的接口(更多的情况下,我们是重写父类的方法,在指定的条件下来调用我们的接口。有时我们也可以设置一个定时器在指定的时间间隔调用)
##实例一(通过重写父类方法,调用自定义接口)
1:自定义MyView类
public class MyView extends View {
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public MyView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public interface OnMyClickListener{
public void onClick();
}
private OnMyClickListener myListener;
public void setOnMyClickListener(OnMyClickListener onMyClickListener) {
// TODO Auto-generated method stub
myListener = onMyClickListener;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if(MotionEvent.ACTION_DOWN == event.getAction() && null != myListener){
myListener.onClick();
return true;
}
return super.onTouchEvent(event);
}
}
2:布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<com.example.customlistener.MyView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/myview"
/>
</LinearLayout>
3:Activity
import com.example.customlistener.MyView.OnMyClickListener;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Toast;
public class MainActivity extends Activity {
private MyView myView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myView = (MyView) this.findViewById(R.id.myview);
myView.setOnMyClickListener(new OnMyClickListener() {
@Override
public void onClick() {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "自定义监听器!!!", Toast.LENGTH_LONG).show();
}
});
}
}
##实例二(通过Handler来实时的调用我们的监控器)
思路:
我们通过Timer创建一个每隔10秒通过Handler发送一个消息。然后在Handler中调用我们的接口方法,就OK了;
1:自定义Test类:
package com.example.customlistenerhandler;
import java.util.Timer;
import java.util.TimerTask;
import android.os.Handler;
import android.os.Message;
public class Test {
private OnMyClickListener myClickListener = null;
private int TAG = 0x001;
private Handler handler = null;
private Timer timer = null;
public Test(){
timer = new Timer();
handler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0x001:
if(null != myClickListener){
myClickListener.onClick();
}
break;
}
}
};
}
public void setOnMyClickListener(OnMyClickListener listener){
this.myClickListener = listener;
}
public void run(){
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(TAG);
}
}, 0, 10000);
}
public void shutDownListener(){
if(null != timer){
timer.cancel();
}
}
}
2:自定义接口
package com.example.customlistenerhandler;
public interface OnMyClickListener {
public void onClick();
}
3:布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="关闭当前的监听器"
android:id="@+id/mybutton"/>
</LinearLayout>
4:Activity
package com.example.customlistenerhandler;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private Test test;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) this.findViewById(R.id.mybutton);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
test.shutDownListener();
}
});
test = new Test();
test.setOnMyClickListener(new OnMyClickListener() {
@Override
public void onClick() {
Toast.makeText(MainActivity.this, "自定义检测器每10秒被调用一次!!!", Toast.LENGTH_SHORT).show();
}
});
test.run();
}
}
当Activity启动时会自动开启监听器,每隔10秒钟调用一次接口;当我们点击按钮后,会把监听器关闭!!!