android處理雙擊事件

來源:互聯網
上載者:User

需求:一些透明的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”
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.