2011.09.01(2)——— android 处理双击事件
参考:http://lipeng88213.iteye.com/blog/838741

需求:一些透明的button 用来处理软件环境的改变

xml:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout android:layout_width="fill_parent"xmlns:android="http://schemas.android.com/apk/res/android"android:layout_height="fill_parent"  android:background="@drawable/splashview"><Button android:background="@android:color/transparent" android:layout_height="30dip"android:layout_width="50dip"android:id="@+id/leftUp"android:layout_alignParentTop="true"android:layout_alignParentLeft="true"android:layout_margin="0px"android:padding="0px"/><Button android:background="@android:color/transparent" android:layout_height="30dip"android:layout_width="50dip"android:id="@+id/rightUp"android:layout_alignParentTop="true"android:layout_alignParentRight="true"android:layout_margin="0px"android:padding="0px"/><Button android:background="@android:color/transparent" android:layout_height="30dip"android:layout_width="50dip"android:id="@+id/leftDown"android:layout_alignParentBottom="true"android:layout_alignParentLeft="true"android:layout_margin="0px"android:padding="0px"/><Button android:background="@android:color/transparent" android:layout_height="30dip"android:layout_width="50dip"android:id="@+id/rightDown"android:layout_alignParentBottom="true"android:layout_alignParentRight="true"android:layout_margin="0px"android:padding="0px"/></RelativeLayout>



activity:



import android.content.Context;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.widget.Button;import android.widget.Toast;public class AboutActivity extends Activity {private Button leftUp;private Button rightUp;private Button leftDown;private Button rightDown;private ButtonOnTouchListener listener = new ButtonOnTouchListener();private Context mContext;//计算点击的次数private int count;//第一次点击的时间 long型private long firstClick;//最后一次点击的时间private long lastClick;//第一次点击的button的idprivate int firstId;//调试环境是否开启private boolean isDebugOpen = false;//日志环境是否开启private boolean isLogOpen = false;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.about);mContext = this;leftUp = (Button)findViewById(R.id.leftUp);rightUp = (Button)findViewById(R.id.rightUp);leftDown = (Button)findViewById(R.id.leftDown);rightDown = (Button)findViewById(R.id.rightDown);leftUp.setOnTouchListener(listener);rightUp.setOnTouchListener(listener);leftDown.setOnTouchListener(listener);rightDown.setOnTouchListener(listener);}private class ButtonOnTouchListener implements OnTouchListener{@Overridepublic boolean onTouch(View v, MotionEvent event) {if(event.getAction()==MotionEvent.ACTION_DOWN){//如果第二次点击 距离第一次点击时间过长   那么将第二次点击看为第一次点击if(firstClick!=0 && firstId!=0 && System.currentTimeMillis()-firstClick>300){count = 0;firstId = 0;}count++;if(count==1){firstClick = System.currentTimeMillis();//记录第一次点得按钮的idfirstId = v.getId();}else if(count==2){lastClick = System.currentTimeMillis();//两次点击小于300ms 也就是连续点击if(lastClick-firstClick<300){//第二次点击的button的idint id = v.getId();//判断两次点击的button是否是同一个buttonif(id == firstId){switch(id){case R.id.leftUp:break;case R.id.rightUp:break;case R.id.leftDown:if(isLogOpen){//关闭日志环境Toast.makeText(mContext, "日志关闭", 0).show();}else{//打开日志环境Toast.makeText(mContext, "日志开启", 0).show();}isLogOpen = !isLogOpen;break;case R.id.rightDown:if(isDebugOpen){//关闭调试环境Toast.makeText(mContext, "调试关闭", 0).show();}else{//打开调试环境Toast.makeText(mContext, "调试开启", 0).show();}isDebugOpen = !isDebugOpen;break;}}}clear();}}return false;}//清空状态private void clear(){count = 0;firstClick = 0;lastClick = 0;firstId = 0;}}}




更多相关文章

  1. Android之去掉ListView的点击背景
  2. android ndk-stack调试
  3. 2011.09.01(2)——— android 处理双击事件
  4. Android(安卓)Studio学习基础篇一
  5. TextView添加Onclick点击无效没反应解决办法
  6. SDK + ADT + Android(安卓)Studio 国内下载地址
  7. android神器Stetho调试
  8. android 内存占用调试
  9. 查看安卓系统是否卡开了可调试debuggable

随机推荐

  1. python进阶——利用网页爬虫写天气预报采
  2. python过滤html文档中的Tag标签
  3. python 处理csv文件的过程对换行符的处理
  4. Django 基础(一),项目创建、URL路由、数据库
  5. 是什么导致了Python分割错误?
  6. python 产生随机数,随机字符串
  7. Python3学习笔记之十九
  8. 如何正确地获取在pysnmp中被捕获的变量的
  9. Python on Raspbian - " TypeError: ' nu
  10. 不能使用python HTTPSConnection()连接到