2012-05-07 6 views
4

ことをユーザーリリースまでときに、ユーザー押しボタン機能の繰り返し呼び出すことができ、私は繰り返すことができますどのようにがどのように私はアンドロイドでボタン

は、時間のユーザ押しボタンから関数を呼び出しますか?

私はclickClickListenerとlongclicklistenerをチェックしていますが、自分が望むようには見えません。

ありがとうございます。

+0

私は(getLongPressTimeoutを調整することができます)ViewConfigurationでlongPressイベントが頻繁にチェックされるようにするには?私自身のLongPressイベントリスナーをより頻繁に実装しますか? – michael

+0

また、TouchListenerを実装して、デフォルトのクリックアクションが正常に機能するようにするにはどうすればよいですか? – michael

+0

[ボタンを押したままにしてアンドロイドリピートアクション]の複製が可能です(http://stackoverflow.com/questions/10511423/android-repeat-action-on-pressing-and-holding-a-button) – madx

答えて

2

public class MainActivity extends Activity implements OnTouchListener 
{ 

    private Button button; 

    // ... 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     // ... 

     button = (Button) findViewById(R.id.button_id); 
     button.setOnTouchListener(this); 

     // ... 
    } 

    // ... 

    @Override 
    public boolean onTouch(View v, MotionEvent event) 
    { 
     /* get a reference to the button that is being touched */ 
     Button b = (Button) v; 

     /* get the action of the touch event */ 
     int action = event.getAction(); 

     if(action == MotionEvent.ACTION_DOWN) 
     { 
      /* 
       A pressed gesture has started, the motion contains 
       the initial starting location. 
      */ 
     } 
     else if(action == MotionEvent.ACTION_UP) 
     { 
      /* 
       A pressed gesture has finished, the motion contains 
       the final release location as well as any intermediate 
       points since the last down or move event. 
      */ 
     } 
     else if(action == MotionEvent.ACTION_MOVE) 
     { 
      /* 
       A change has happened during a press gesture (between 
       ACTION_DOWN and ACTION_UP). The motion contains the 
       most recent point, as well as any intermediate points 
       since the last down or move event. 
      */ 
     } 
     else if(action == MotionEvent.ACTION_CANCEL) 
     { 
      /* 
       The current gesture has been aborted. You will not 
       receive any more points in it. You should treat this 
       as an up event, but not perform any action that you 
       normally would. 
      */ 
     } 
    } 
} 
0

私が正しい場合: クリック:ユーザーが指を押して離しました。 タッチ:ユーザーの指がまだデバイスにあります。

多分あなたはonTouchListenerを試してみるべきですか?私はそれを使ったことがない ...

0

のように、ユーザがボタンのリリースまで機能を呼び出すためのOnTouchListener使用:あなたはOnTouchListenerを使用することができます

private OnTouchListener otl_conn = (OnTouchListener) new TouchListenerConn(); 
private Button bv = null; 
bv = (Button) findViewById(R.id.xxxxbutton); 
bv.setOnTouchListener(otl_conn); 
class TouchListenerConn implements OnTouchListener 
    { 
     public boolean onTouch(View v, MotionEvent event) { 
      switch(event.getAction()){ 
      case MotionEvent.ACTION_DOWN: 
      //call function here 
      break; 
      case MotionEvent.ACTION_UP: 
      //DO SOMETHING 
      xxxx....; 
      break; 
      } 
      return true; 
     } 
    } 
関連する問題