我正在尋找一種方法來調用一個按鈕,並按住一秒鐘的功能。當按鈕被釋放時,應該調用另一個函數。如何檢查按鈕是否仍然點擊後第二
我在想一個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
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
任何想法,我做錯了什麼,或者爲什麼這個心不是工作?謝謝!
沒有閱讀所有這些代碼,但不能設置,當你開始觸摸時間,然後比較,與當你釋放的時間? – Cruncher
longClickListener實際上應該怎麼做,因爲它會使用用戶系統中所有其他應用程序使用的標準延遲 – njzk2
@Cruncher不太明白你的意思。我不想在釋放按鈕後調用該函數,而是在按鈕被第一次按下之前按下一秒,直到釋放按鈕 – patsud