gpt4 book ai didi

android - 按钮 setPressed for certain time period

转载 作者:行者123 更新时间:2023-11-30 03:13:49 25 4
gpt4 key购买 nike

我正在尝试将按钮状态设置为 pressed=true onTouch,然后在一段时间后将其设置回 pressed=false。

onTouch 方法正确地将状态设置为按下

myTouchListener= new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {

lastPressed = (Button)v;
lastPressed.setPressed(true);

return true;
}
};

然后我想我可以使用计时器将其设置回原始状态。但是计时器需要一个 TimerTask,它需要一个可运行的,所以我不能将对“lastPressed”按钮的引用传递给它。

我完全不知道从这里去哪里。

最佳答案

其实很简单,
创建一个指向同一个按钮的最终变量,然后你可以在线程中使用它,
因此启动一个新线程,在一段时间后将其设置为 false:

myTouchListener= new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {

lastPressed = (Button)v;
lastPressed.setPressed(true);

final Button lp = lastPressed;
Thread t = new Thread()
{
public void run()
{
try
{
Thread.sleep(1000); //your time in milliseconds here
} catch (Exception e) { }

lp.setPressed(false);
}
}
t.start();
return true;
}
};

关于android - 按钮 setPressed for certain time period,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20482564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com