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. android 布局式跑马灯,非TextView
  2. Android如何获得系统版本
  3. Android(安卓)TabHost使用、动态加载内容
  4. Android异步加载图像小结 (含线程池,缓存方
  5. Android开发之消息处理机制(一)——Handler
  6. Android,一个思路实现APP版本更新
  7. android style
  8. Android(安卓)Activity界面切换添加动画
  9. Android中滑屏初探 ---- scrollTo 以及 s
  10. Android(安卓)任务和回退堆栈---启动任务