2017-02-18 46 views
0

當用戶在活動中的任何地方敲擊5次時,我想顯示一個對話框(祕密菜單)。這是否有可能?我無法做到這一點,也沒有找到任何文件。當用戶敲擊5次時獲取觸摸事件

+0

檢查我的答案,這是你想要的? –

回答

0

試試這個代碼:覆蓋onTouchEvent(MotionEvent event)方法在您的活動,並計算抽頭數..

public class MainActivity extends Activity { 

private int count = 0; 

//detect any touch event in the screen 

@Override 
public boolean onTouchEvent(MotionEvent event) { 

    int eventaction = event.getAction(); 
    if (eventaction == MotionEvent.ACTION_UP) { 

    //get system current milliseconds 
    long time= System.currentTimeMillis(); 
    ++count; 

    if (count==5) { 
     //show Dialog 
     new AlertDialog.Builder(MainActivity.this) 
       .setTitle("Your Alert") 
       .setMessage("Your Message") 
       .setCancelable(false) 
       .setPositiveButton("ok", new OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         // Whatever... 
        } 
       }).show(); 

    } 
    return true;  
    } 
    return false; 
    } 

} 
0

首先Intialize全局變量mCounter用於跟蹤你有多少時間點擊活動。

int mCounter = 0;

可以覆蓋的onTouchEvent方法如下面的活動中

@Override 
    public boolean onTouchEvent(MotionEvent event) { 

     switch (event.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
       // increase counter here 
       counter = counter + 1; 
       if(counter == 5) { 
        // show dialog here 
       } 
       break; 


     } 
     return true; 
    } 
} 

希望這些幫助你。

0

這很簡單。該代碼將是這樣的:

public class MainActivity extends Activity { 

private int count = 0; 

public boolean onTouchEvent(MotionEvent event) { 

int eventaction = event.getAction(); 
if (eventaction == MotionEvent.ACTION_UP) { 
count++; 
} 
else{ 
break; 
} 
if (count==5) { 
    //do whatever you need 
Toast.makeText(getActivity(), "You tapped 5 times on screen", 
Toast.LENGTH_LONG).show(); 
} 
return true;  
} 
return false; 
} 
0

爲了您的信息

是的,你可以重寫onTouchEvent(MotionEvent event)

用戶得到祕密菜單,每當他完成5個接觸。

但對於一個真正的祕密菜單,當過用戶觸摸5次 快速對話顯示出來。

對於

public class MainActivity extends Activity { 

private final int count = 0; 
Handler handler; 
Runnable runnable; 


@Override 
public boolean onTouchEvent(MotionEvent event) { 

    int eventaction = event.getAction(); 
    if (eventaction == MotionEvent.ACTION_UP) { 

     count++; 
     if (count > 0) { 
      handler = new Handler(); 
      runnable = new Runnable() { 
       @Override 
       public void run() { 
        count = 0; 
       } 
      }; 
      handler.postDelayed(runnable, 1000); // clear counter if user does not touch for one sec 
     } 
     if (count == 5) { 
      //do whatever 

     } 
     return true; 
    } 
    return false; 
} 

}

如果用戶不碰了一下使計數器爲0

要小心,以正確導入處理

import android.os.Handler;