gpt4 book ai didi

java - Android - 按住按钮时重复功能

转载 作者:搜寻专家 更新时间:2023-11-01 09:38:29 24 4
gpt4 key购买 nike

在我的 Android 项目中,我试图重复这段代码(我的 MainActivity.class 文件中的一个函数):

public void positionChange(View v)
{
findViewById(R.id.player1).setX(findViewById(R.id.player1).getX() - 30);
findViewById(R.id.player2).setX(findViewById(R.id.player2).getX() + 30);

}

按住按钮时。

目前,当使用以下方法按下按钮时,每次点击都会调用此代码:

android:onClick="positionChange"

在我的 activity_main.xml 文件中。由于我是 Android 编程的新手,我不知道该怎么做,而且我发现的现有代码似乎不能正常工作,那么我怎么才能让它工作呢?

谢谢。

最佳答案

首先您必须知道,每次调用 findViewById() 都会浪费您应用程序的大量时间,因此您最好获取一次并多次使用它们。

要在按住按钮时重复此代码,您必须使用以下链接并使用此链接提供的自定义监听器:

Android - Hold Button to Repeat Action

为了方便起见,我复制了代码:

import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;

/**
* A class, that can be used as a TouchListener on any view (e.g. a Button).
* It cyclically runs a clickListener, emulating keyboard-like behaviour. First
* click is fired immediately, next one after the initialInterval, and subsequent
* ones after the normalInterval.
*
* <p>Interval is scheduled after the onClick completes, so it has to run fast.
* If it runs slow, it does not generate skipped onClicks. Can be rewritten to
* achieve this.
*/
public class RepeatListener implements OnTouchListener {

private Handler handler = new Handler();

private int initialInterval;
private final int normalInterval;
private final OnClickListener clickListener;

private Runnable handlerRunnable = new Runnable() {
@Override
public void run() {
handler.postDelayed(this, normalInterval);
clickListener.onClick(downView);
}
};

private View downView;

/**
* @param initialInterval The interval after first click event
* @param normalInterval The interval after second and subsequent click
* events
* @param clickListener The OnClickListener, that will be called
* periodically
*/
public RepeatListener(int initialInterval, int normalInterval,
OnClickListener clickListener) {
if (clickListener == null)
throw new IllegalArgumentException("null runnable");
if (initialInterval < 0 || normalInterval < 0)
throw new IllegalArgumentException("negative interval");

this.initialInterval = initialInterval;
this.normalInterval = normalInterval;
this.clickListener = clickListener;
}

public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
handler.removeCallbacks(handlerRunnable);
handler.postDelayed(handlerRunnable, initialInterval);
downView = view;
downView.setPressed(true);
clickListener.onClick(view);
return true;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
handler.removeCallbacks(handlerRunnable);
downView.setPressed(false);
downView = null;
return true;
}

return false;
}

}

然后在您的 onCreate() 中像下面这样使用它:

View player1 =  findViewById(R.id.player1);
View player2 = findViewById(R.id.player2);
button.setOnTouchListener(new RepeatListener(400, 100, new OnClickListener() {
@Override
public void onClick(View view) {
// the code to execute repeatedly
player1.setX(player1.getX() - 30);
player2.setX(player2.getX() + 30);
}
}));

为了获得更好的代码,您可以将 player1 和 player2 定义为 public(在您的 onCreate 之外),然后在 onCreate() 中启动它们

关于java - Android - 按住按钮时重复功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41406159/

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