要实现按钮长按的功能并不难,最简单的方法就是通过实现setOnTouchListener()方法,然后在里面处理对应的逻辑操作就行了.
这里我是自定义了按钮来专门处理长按功能,直接上代码
MainActivity.java
package huahua.btnlongtouch;
import huahua.btnlongtouch.LongTouchBtn.LongTouchListener;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView Tv1;
private LongTouchBtn Btn1;
private int num=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Tv1 = (TextView)findViewById(R.id.tv1);
Btn1 = (LongTouchBtn)findViewById(R.id.btn2);
Btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Log.i("huahua", "自定义按钮处理单击");
}
});
Btn1.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Log.i("huahua", "自定义按钮处理长按一次相应");
return false;
}
});
/**
* 这是一个自定义的接口 专门负责处理长按逻辑
* @param listener
* 监听器。
* @param time
* 第2个参数传入1000 ,表示1秒处理一次onLongTouch()方法
*/
Btn1.setOnLongTouchListener(new LongTouchListener() {
@Override
public void onLongTouch() {
num++;
Tv1.setText(num+"");
Log.i("huahua", "正在长按");
}
},1000);
}
}
为了使实现方法清晰易懂,主Activity中就只有自定义的LongTouchBtn和TextView2个组件
LongTouchBtn.java
package huahua.btnlongtouch;
import android.content.Context;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Button;
public class LongTouchBtn extends Button{
/**
* 记录当前自定义Btn是否按下
*/
private boolean clickdown = false;
/**
* 长按的回调接口
*/
private LongTouchListener mListener;
/**
* 按钮长按时 间隔多少毫秒来处理 回调方法
*/
private int mtime;
/**
* 构造函数
* @param context
* @param attrs
*/
public LongTouchBtn(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
/**
* 处理touch事件
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
clickdown = true;
new LongTouchTask().execute();
Log.i("huahua", "按下");
}
else if(event.getAction() == MotionEvent.ACTION_UP)
{
clickdown = false;
Log.i("huahua", "弹起");
}
return super.onTouchEvent(event);
}
/**
* 使当前线程睡眠指定的毫秒数。
*
* @param time
* 指定当前线程睡眠多久,以毫秒为单位
*/
private void sleep(int time) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 处理长按的任务
*/
class LongTouchTask extends AsyncTask<Void, Integer, Void>{
@Override
protected Void doInBackground(Void... params) {
while(clickdown)
{
sleep(mtime);
publishProgress(0);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
}
@Override
protected void onProgressUpdate(Integer... values) {
mListener.onLongTouch();
}
}
/**
* 给长按btn控件注册一个监听器。
*
* @param listener
* 监听器的实现。
* @param time
* 多少毫秒时间间隔 来处理一次回调方法
*/
public void setOnLongTouchListener(LongTouchListener listener, int time) {
mListener = listener;
mtime = time;
}
/**
* 长按监听接口,使用按钮长按的地方应该注册此监听器来获取回调。
*/
public interface LongTouchListener {
/**
* 处理长按的回调方法
*/
void onLongTouch();
}
}
自定义的Button,也是这个程序最重要的类
布局文件activity_main.xml
<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"
>
<huahua.btnlongtouch.LongTouchBtn
android:id="@+id/btn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="自定义Btn" />
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
/>
</LinearLayout>
好了,代码不多而且都加入了详细的注释,有兴趣的朋友可以下载源码
最后还是说下为什么要自定义Button的思路来实现
1,为了方便以后扩展,自定义的组件可以实现很多特殊的功能
2,降低耦合性,避免以后每个要实现长按的Button都去处理setOnTouchListener,然后还在要里面判断是否长按的条件