はカスタムListner
String mString="";
public class ContinousRepeatListener implements View.OnTouchListener {
private Handler handler = new Handler();
private int initialInterval;
private final int normalInterval;
private final View.OnClickListener clickListener;
private Runnable handlerRunnable = new Runnable() {
@Override
public void run() {
handler.postDelayed(this, normalInterval);
clickListener.onClick(downView);
}
};
private View downView;
/**
* @param initialInterval The interval after first click event
* @param normalInterval The interval after second and subsequent click
* events
* @param clickListener The OnClickListener, that will be called
* periodically
*/
public ContinousRepeatListener (int initialInterval, int normalInterval,
View.OnClickListener clickListener) {
if (clickListener == null)
throw new IllegalArgumentException("null runnable");
if (initialInterval < 0 || normalInterval < 0)
throw new IllegalArgumentException("negative interval");
this.initialInterval = initialInterval;
this.normalInterval = normalInterval;
this.clickListener = clickListener;
}
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
handler.removeCallbacks(handlerRunnable);
handler.postDelayed(handlerRunnable, initialInterval);
downView = view;
downView.setPressed(true);
clickListener.onClick(view);
return true;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
handler.removeCallbacks(handlerRunnable);
downView.setPressed(false);
downView = null;
return true;
}
return false;
}
}
を行い、次のように試してみて、あなたがダウンしてアクションをタッチアップを検討なかったこの
btnDown.setOnTouchListener(new ContinousRepeatListener(400, 100, new View.OnClickListener() {
@Override
public void onClick(View view) {
// the code to execute repeatedly
mString += "S";
tvString.setText(mString);
}
}));
のように使用し、あなたのsendMessageコード書くのか? –
私はボタンを持っています。ボタンをクリックすると、私はこのアクションを実行する必要があります。 – Archana
ボタンにタッチリスナーを簡単に追加できます。 http://stackoverflow.com/questions/11779082/listener-for-pressing-and-releasing-a-button –