android 实现button单击双击事件同时监控
2015-10-27 19:03
901 查看
java代码部分
xml布局文件代码
<pre name="code" class="java"><span style="font-size:18px;">package com.xpc.doubleclick; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class DoubleClickTest extends Activity { private boolean waitDouble = true; private static final int DOUBLE_CLICK_TIME = 350; //两次单击的时间间隔 @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); // 设置布局文件,其中只有一个Button setContentView(R.layout.activity_main); Button button = (Button)findViewById(R.id.button1); button.setOnClickListener(listener1); } private OnClickListener listener1 = new OnClickListener() { @Override public void onClick(View v) { <span style="white-space:pre"> </span>if ( waitDouble == true ) //第一次击按钮,等待双击 { waitDouble = false; //准备接受双击 Thread thread = new Thread() { //开启线程</span>
<span style="font-size:18px;"> @Override public void run() { try { sleep(DOUBLE_CLICK_TIME); //线程睡眠 if ( waitDouble == false ) { //如果在睡眠时间中任未进行第二次点击,</span>
<span style="font-size:18px;"><span style="white-space:pre"> </span>//如果进行了第二次点击则第一次点击未产生任何效果 waitDouble = true; singleClick(); <span style="white-space:pre"> </span>//判断该次操作为单击操作,等待下一次点击 } } catch (InterruptedException e) { e.printStackTrace(); } } }; thread.start(); } else { <span style="white-space:pre"> </span>//在第一次点击正在睡眠时,进行了第二次点击 waitDouble = true; doubleClick(); <span style="white-space:pre"> </span>//判断出为双击,等待单击 } } }; // 单击响应事件 private void singleClick(){ System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<singleClick"); } // 双击响应事件 private void doubleClick(){ System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<doubleClick"); } }</span>
xml布局文件代码
<span style="font-size:18px;"><RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="Button" /> </RelativeLayout></span>demo下载地址
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析