android處理雙擊事件 – Android移動開發技術文章_手機開發 Android移動開發教學課程

需求:一些透明的button 用來處理軟件環境的改變

xml:

Java代碼 
<?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:

 

Java代碼 
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; 
        } 
         
    } 
     

 

作者“TryLoveCatch”
 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。