[英]How to check if Button is still clicked after second
我正在尋找一種按住按鈕一秒鍾的方法來調用函數。 釋放按鈕時,應調用另一個功能。
我當時在考慮onLongClickedListener,但這對我來說不太好,因為要顯示的文本會太長或太短。 我認為TouchListener可以為我提供幫助,因為Action_Up事件可以讓我選擇不再按下按鈕時使文本消失。 當按下按鈕時,Action_down事件給了我,我認為可以在按下按鈕時啟動計時器,等待一秒鍾,再次檢查按鈕是否仍被按下,然后調用該函數(顯示文本)。
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (v.getId()) {
// the button. I set bFertig.setOnTouchListener(this); in onCreate
case R.id.bFertig:
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// everything works up to here
waitTimerNotif = new CountDownTimer(1000, 500) {
@Override
public void onTick(long arg0) {}
@Override
public void onFinish() {
// TODO Auto-generated method stub
// here im checking if the button still is pressed
if (bFertig.isPressed()) {
// It never goes into here
ShowNotifBox("Fertig", "fertig", false, false,false);
}
}
}.start();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
DissapearNotifBox();
Log.d("Debug", "Button released");
}
break;
}
return true;
}
對於xml中的Button:
<Button
android:id="@+id/bFertig"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@drawable/fertigbutton"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="18.5dp"
android:clickable="true"/> <!--Googleing suggested I need this for isPressed() to work but it didnt help
任何想法,我做錯了什么或為什么這不起作用? 謝謝!
為什么不使用setOnLongClickListener
函數? 這應該可以解決您的問題。 可以在此處找到一個示例: setOnLongClickListener
您返回的是true,這表示系統正在處理觸摸事件,這意味着按鈕沒有,這意味着它可能沒有更新其按下狀態。 嘗試返回false。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.