Android 当按钮处于按下状态时连续调用方法
只要按下按钮,我就会尝试连续调用一个方法Android 当按钮处于按下状态时连续调用方法,android,button,onclick,Android,Button,Onclick,只要按下按钮,我就会尝试连续调用一个方法 public void onClick(View view) { method(); } <?xml version="1.0" encoding="utf-8"?> <Button xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/button_send" android:layout_width="wrap_content"
public void onClick(View view) {
method();
}
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="onClick" />
public void onClick(视图){
方法();
}
您必须使用switch case实现onTouchListener和overide onTouch()方法
float eventX = event.getX();
float eventY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
return true;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
// nothing to do
break;
default:
return false;
}
use可以使用OnLongClickListener()您可以在要执行的方法()中编写while循环。当收到
操作时,while将退出。
类似这样的方法会奏效:
boolean pressed = true;
private void method() {
while(pressed) {
//Code
}
}
获取操作时,将布尔值设置为false
Hi use onTouchListener函数当您按下按钮时,它将有助于执行持续过程
})) 试试这个:
class OTL extends Handler implements OnTouchListener {
private int cnt;
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
sendEmptyMessage(0);
Log.d(TAG, "onTouch ACTION_DOWN ");
} else
if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_UP) {
removeMessages(0);
}
return false;
}
@Override
public void handleMessage(Message msg) {
Log.d(TAG, "run your method here " + cnt++);
sendEmptyMessage(0);
}
}
测试代码:
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
Button button = new Button(this);
ll.addView(button);
button.setOnTouchListener(new OTL());
setContentView(ll);
这是一个用于执行任务的触控式监听器。
增加一些延迟以使触摸事件排队
@SuppressLint("HandlerLeak")
class FrontTouchHandler extends Handler implements View.OnTouchListener {
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
sendEmptyMessage(0);
} else if (action == MotionEvent.ACTION_UP) {
removeMessages(0);
}
return false;
}
@Override
public void handleMessage(Message msg) {
//perform task here
sendEmptyMessageDelayed(0, 125);
}
}
}
onTouch事件不会连续调用。只有当用户执行任何向下、向上或移动操作时才会调用它。这不会解决问题。我不认为触碰时会调用ACTION\u DOWN。触碰时会调用ACTION\u DOWN和ACTION\u UP。触碰时会调用ACTION\u DOWN()。这不会解决问题,因为如果用户一直按下按钮,它将只被调用一次。是否希望如果您按下按钮,该方法将持续运行Y只要按钮处于按下状态,您就可以在线程中执行此操作。在用户更改触摸坐标之前,此操作将不起作用,因为将调用onTouch事件。如果要继续按,则不是这样。是的,可以使用“取消”操作来打破循环。我认为应用程序将停留在这个阶段{while(pressed){//Code}}。您的方法应该在后台线程上执行,而不是在UI线程上执行,因为UI线程被阻塞。
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
Button button = new Button(this);
ll.addView(button);
button.setOnTouchListener(new OTL());
setContentView(ll);
@SuppressLint("HandlerLeak")
class FrontTouchHandler extends Handler implements View.OnTouchListener {
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
sendEmptyMessage(0);
} else if (action == MotionEvent.ACTION_UP) {
removeMessages(0);
}
return false;
}
@Override
public void handleMessage(Message msg) {
//perform task here
sendEmptyMessageDelayed(0, 125);
}
}
}