android onClick方法改造实现双击事件
2014-06-07 22:59
615 查看
import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private static final String TAG = "MainActivity"; // 是否第二次点击标志 private boolean flag = true; // 第一次点击时间 private long one = 0; // 第二次点击时间 private long two = 0; // 算为双击点击时间间隔 private int interval = 500;</span> @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void twoTouch(View v) { // Log.i(TAG, "进入 twoTouch"); // 判断是否第二次点击 if (!flag) { // 取第二次时间时超时,重置标志之类 if ((System.currentTimeMillis() - one) > interval) { flag = true; one = 0; two = 0; } } // 判断标志给 one two 赋值,初始 true 给 one 赋值 if (flag) { Log.i(TAG, "one 赋值"); one = System.currentTimeMillis(); flag = false; Toast.makeText(getApplicationContext(), "第一次点击!", Toast.LENGTH_SHORT).show(); } else { Log.i(TAG, "two 赋值"); two = System.currentTimeMillis(); if ((two - one) > interval) { // 取第二次时间时超时 flag = true; one = 0; two = 0; } } // 计算两次点击间隔 long t_o = two - one; // 判断间隔是否在双击范围中 if (t_o > 0 && t_o < interval) { Log.i(TAG, "双击发生"); // 双击发生则设置标志之类复位 flag = true; one = 0; two = 0; // 对话框构建器 AlertDialog.Builder dialog = new AlertDialog.Builder(this); // 设置标题、内容 dialog.setTitle("提示"); dialog.setMessage("你双击了哈!!!"); // 设置不能使用 back 键关闭 dialog.setCancelable(false); // 设置左边按钮 dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // Toast.makeText(getApplicationContext(), // "点击了对话框确定钮!", Toast.LENGTH_SHORT).show(); } }); dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // Toast.makeText(getApplicationContext(), // "点击了对话框取消钮!", Toast.LENGTH_SHORT).show(); } }); // 创建对话框 AlertDialog alert = dialog.create(); // 显示对话框 alert.show(); } else if (two > 0 && !flag) { Log.i(TAG, "双击没有发生"); // 双击没有发生设置标志之类复位 flag = true; one = 0; two = 0; } } }
标红为主要代码,全局变量中定义:是否第二次点击标志,第一次点击时间,第二次点击时间
首先判断是否第二次点击防止了,先点击一次,等会连续点击两次的问题。
欢迎交流哈
学习笔记:
布局文件中使用TextView控件 声明点击事件,必须声明 android:clickable="true"
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0f0"
android:clickable="true"
android:gravity="center"
android:onClick="twoTouch"
android:text="@string/hello_world"
android:textSize="30sp" />
相关文章推荐
- 实现Android监控任意控件或按键双击事件方法
- Android onClick事件三种实现方法
- Android 自定义View实现单击和双击事件的方法
- Android开发之布局文件中实现OnClick事件关联处理方法
- Android onClick事件三种实现方法
- Android响应事件onClick方法的五种实现方式小结
- Android开发之布局文件里实现OnClick事件关联处理方法
- 三种Android单击事件onclick的实现方法
- Android onClick事件三种实现方法
- android 双击屏幕触发事件,放大图片,不用接入借口api,不用OnGestureListener,不造成代码累赘方法
- Android:实现手势滑动的事件处理方法
- android 实现button双击事件
- Android:实现手势滑动的事件处理方法
- Android知识整理(2)【转】android中三种onClick事件的实现方式与对比
- Android中三种onClick事件的实现与对比
- Android 实现事件监听的两个方法
- Android开发之Button事件实现方法的总结
- Android开发中4种Onclick事件的实现
- 利用dispatchSetPressed方法实现自定义监听onClick事件
- Android开发中onClick事件的几种实现,分析,对比