25

ユーザーがボタンを長時間押し続ける必要があるアプリケーションに取り組んでいます。

ユーザーがプレスを終了するか、タッチ位置を移動する瞬間を検出するにはどうすればよいですか?

ありがとう

4

4 に答える 4

66

あなたの最善の策は、そのボタンに onLongClickListener() と onTouchListener() の組み合わせを使用することだと思います。タッチ リスナーはすべてのタッチ イベントに対してトリガーされるため、タッチ リスナーで特定のイベントをキャッチする必要があります。

次のようなことを試してください。

class Blah extends Activity {
     private Button mSpeak;
     private boolean isSpeakButtonLongPressed = false;

     @Override
     public void onCreate(Bundle icicle) {
          super.onCreate(icicle);
          setContentView(R.layout.blahlayout);
          Button mSpeak = (Button)findViewById(R.id.speakbutton);
          mSpeak.setOnLongClickListener(speakHoldListener);
          mSpeak.setOnTouchListener(speakTouchListener);
     }

     private View.OnLongClickListener speakHoldListener = new View.OnLongClickListener() {

          @Override
          public boolean onLongClick(View pView) {
               // Do something when your hold starts here.
               isSpeakButtonLongPressed = true;
               return true;
          }
     }

     private View.OnTouchListener speakTouchListener = new View.OnTouchListener() {

          @Override
          public boolean onTouch(View pView, MotionEvent pEvent) {
               pView.onTouchEvent(pEvent);
               // We're only interested in when the button is released.
               if (pEvent.getAction() == MotionEvent.ACTION_UP) {
                    // We're only interested in anything if our speak button is currently pressed.
                    if (isSpeakButtonLongPressed) {
                         // Do something when the button is released.
                         isSpeakButtonLongPressed = false;
                    }
               }
               return false;
          }
     }
}
于 2012-05-24T23:11:35.033 に答える
1

これにはOnTouchListenerを使用できると思います。

于 2011-05-31T07:48:05.553 に答える
-1

これにはonFocusChanged- Listenerを使用できると思います。

于 2011-05-31T06:54:37.330 に答える