[英]How to check if Button is still clicked after second
I am looking for a way to call a function when a button is pressed and held for one second. 我正在寻找一种按住按钮一秒钟的方法来调用函数。 When the button is released another function should be called.
释放按钮时,应调用另一个功能。
I was thinking about an onLongClickedListener but this won't work well for me since the text that is going to be displayed would stay too long or short. 我当时在考虑onLongClickedListener,但这对我来说不太好,因为要显示的文本会太长或太短。 I am thinking a TouchListener could help me because the Action_Up event would give me the option to let the text dissapear when the button isn't pressed anymore.
我认为TouchListener可以为我提供帮助,因为Action_Up事件可以让我选择不再按下按钮时使文本消失。 The Action_down event gives me when the button is pressed and I thought I could start a timer when the button is pressed, wait a second, check again if the button still is pressed and then call the function (show the text).
当按下按钮时,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;
}
For the Button in xml: 对于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
Any ideas what I did wrong or why this isnt working? 任何想法,我做错了什么或为什么这不起作用? Thanks!
谢谢!
Why not use the setOnLongClickListener
function? 为什么不使用
setOnLongClickListener
函数? This should solve your issue. 这应该可以解决您的问题。 An example can be find here: setOnLongClickListener
可以在此处找到一个示例: setOnLongClickListener
You're returning true, which tells the system that you are handling the touch events, which means the button is not, which means it's probably not updating its pressed state. 您返回的是true,这表示系统正在处理触摸事件,这意味着按钮没有,这意味着它可能没有更新其按下状态。 Try returning false.
尝试返回false。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.