2011.09.01(2)——— android 处理双击事件

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的id
	private int firstId;
	//调试环境是否开启
	private boolean isDebugOpen = false;
	//日志环境是否开启
	private boolean isLogOpen = false;
	
	
	@Override
	public 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{

		@Override
		public 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();
					//记录第一次点得按钮的id
					firstId = v.getId();
				}else if(count==2){
					lastClick = System.currentTimeMillis();
					//两次点击小于300ms 也就是连续点击
					if(lastClick-firstClick<300){
						//第二次点击的button的id
						int id = v.getId();
						//判断两次点击的button是否是同一个button
						if(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;
		}
		
	}
	
}




你可能感兴趣的:(android)