2014-03-06 52 views
0

我想在點擊畫布5秒後輸出彈出對話框。不過,當我點擊畫布後,我的對話框立即出現。我該如何解決這個問題?Android:如何在點擊畫布一段時間後顯示彈出對話框

我onTouch代碼如下:

public boolean onTouchEvent(MotionEvent event) { 

    x = event.getX(); 
    y = event.getY(); 

    System.out.println(x); 
    System.out.println(y); 

    switch (event.getAction()) { 
    case MotionEvent.ACTION_DOWN: 

      AlertDialog.Builder builder = new Builder(getContext()); 
        final EditText text = new EditText(getContext()); 

        builder.setTitle("Change Name") 
          .setMessage("New Name").setView(text); 
        builder.setPositiveButton("Change", 
          new DialogInterface.OnClickListener() { 

           public void onClick(DialogInterface di, int i) { 
            name = text.getText().toString(); 

           } 

          }); 
        builder.setNegativeButton("Cancel", 
          new DialogInterface.OnClickListener() { 

           public void onClick(DialogInterface di, int i) { 
           } 

          }); 
        builder.create().show(); 

     } 

     break; 
    case MotionEvent.ACTION_UP: 
     sX = event.getX(); 
     sY = event.getY(); 
     break; 

    case MotionEvent.ACTION_MOVE: 
     fX = event.getX(); 
     fY = event.getY(); 


     break; 

    }// switch 

    return true; 

}// ontouch 

回答

0

您可以嘗試使用處理器與postDelayed這樣的:

Handler handler = new Handler(); 
handler.postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        AlertDialog.Builder builder = new Builder(getContext()); 
        final EditText text = new EditText(getContext()); 

        builder.setTitle("Change Name") 
          .setMessage("New Name").setView(text); 
        builder.setPositiveButton("Change", 
          new DialogInterface.OnClickListener() { 

           public void onClick(DialogInterface di, int i) { 
            name = text.getText().toString(); 

           } 

          }); 
        builder.setNegativeButton("Cancel", 
          new DialogInterface.OnClickListener() { 

           public void onClick(DialogInterface di, int i) { 
           } 

          }); 
        builder.create().show(); 
       } 
      }, 5000); 
+0

它的工作!謝謝! – user3306996

+0

我很高興我可以幫助:) – Laura

0

我建議你檢查的文件上Timer

相關問題