Android 长按按钮
我正在创建一个Android应用程序来控制我电脑的鼠标。该应用程序有4个按钮(左、右、上、下)。单击它们时,应用程序将向pc发送一个整数,在pc上运行的Java应用程序将接收该整数并移动光标 现在我想做的就是当用户长时间按下按钮时,应用程序必须不断地将数字发送到pc,直到用户松开按钮。请有人帮我做这件事。用于监听,并在发生时开始向计算机发送适当的信号,例如每0.5秒发送一次。在Android 长按按钮,android,button,Android,Button,我正在创建一个Android应用程序来控制我电脑的鼠标。该应用程序有4个按钮(左、右、上、下)。单击它们时,应用程序将向pc发送一个整数,在pc上运行的Java应用程序将接收该整数并移动光标 现在我想做的就是当用户长时间按下按钮时,应用程序必须不断地将数字发送到pc,直到用户松开按钮。请有人帮我做这件事。用于监听,并在发生时开始向计算机发送适当的信号,例如每0.5秒发送一次。在MotionEvent之后停止执行此操作。操作\u UP编辑: public class MainActivity ex
MotionEvent之后停止执行此操作。操作\u UP
编辑:
public class MainActivity extends Activity implements OnTouchListener {
private TextView TV;
private Thread move_curser;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TV = (TextView) findViewById(R.id.TV1);
TV.setOnTouchListener(this);
}
public boolean onTouch(View v, MotionEvent event) {
boolean isPressed = event.getAction() == MotionEvent.ACTION_DOWN;
boolean isReleased = event.getAction() == MotionEvent.ACTION_UP;
if(isPressed) {
move_curser = new Thread(new move_curser());
move_curser.start();
your_methode();
return true;
} else if(isReleased){
move_curser.interrupt();
return true;
}
return false;
}
public class move_curser implements Runnable {
public void run() {
int time = 500;
try {
Thread.sleep(time);
} catch (InterruptedException e) {
interrupt();
}
while(true){
your_methode();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
interrupt();
}
}
}
}
}
我以前试过这种代码。问题是isPressed一直都是真的,应用程序会崩溃。@anoopsgolden我想你必须使用新线程。。interrupt()方法对您有效吗..?它对我也无效。。。光标仍在无限期移动。。。。即使松开按钮,它也不会停止。