我想要做的是更改我的run()方法,以便它可以接收不同的变量。我想将player1life更改为其他内容,以便我可以在另一个上使用 customHandler.postDelayed(updateTimerThread,0); ,例如player2life。 代码的作用是,当按下按钮时,变量会改变其值。
注意:我是Android开发中最全新的人。
final android.os.Handler customHandler = new android.os.Handler();
final Runnable updateTimerThread = new Runnable()
{
public void run()
{ if(pressed) {
player1life--;
textView.setText(String.valueOf(player1life));
customHandler.postDelayed(this, 100);
}
}
};
leftButton1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()){
case MotionEvent.ACTION_DOWN :
pressed = true;
customHandler.postDelayed(updateTimerThread, 0);
leftButton1.setImageResource(R.drawable.arrownewred);
winFunction();
break;
case MotionEvent.ACTION_UP :
pressed = false;
leftButton1.setImageResource(R.drawable.arrownew); break;
}
return true;
}
});
答案 0 :(得分:1)
我闻到了XY问题......
为什么不为处理Runnable
创建不同的player2life
?在on click处理程序中,只需执行if语句来检查哪些玩家'生活应该减少!
if ... {
customHandler.postDelayed(runnableThatHandlesPlayer1Life, 0);
} else {
customHandler.postDelayed(runnableThatHandlesPlayer2Life, 0);
}
可以用这种方式声明runnable:
final Runnable runnableThatHandlesPlayer1Life = new Runnable()
{
public void run()
{ if(pressed) {
player1life--;
textView.setText(String.valueOf(player1life));
customHandler.postDelayed(this, 100);
}
}
};
final Runnable runnableThatHandlesPlayer2Life = new Runnable()
{
public void run()
{ if(pressed) {
player2life--;
textView.setText(String.valueOf(player2life));
customHandler.postDelayed(this, 100);
}
}
};
但你的代码有点可疑。根据我对你的代码的理解,当按下按钮时,player1的生命将以10 /秒的速度减少。很奇怪。
似乎你正在尝试创建一个计时器。试试我写的这个Timer
课程。它应该让事情变得更容易:
import android.os.Handler;
public class Timer {
private Handler handler;
private boolean paused;
private int interval;
private Runnable task = new Runnable () {
@Override
public void run() {
if (!paused) {
runnable.run ();
Timer.this.handler.postDelayed (this, interval);
}
}
};
private Runnable runnable;
public int getInterval() {
return interval;
}
public void setInterval(int interval) {
this.interval = interval;
}
public void startTimer () {
paused = false;
handler.postDelayed (task, interval);
}
public void stopTimer () {
paused = true;
}
public Timer (Runnable runnable, int interval, boolean started) {
handler = new Handler ();
this.runnable = runnable;
this.interval = interval;
if (started)
startTimer ();
}
}
答案 1 :(得分:1)
问题刚刚超过匿名内部类中final
变量的需求吗?如果是这样,你可以使用"技巧"使用长度为1的最终数组,然后更新数组的元素。
这是一个简单的例子:
public interface Incrementer {
void increment();
}
public class Foo {
private int bar = 0;
public int getBar() {
return bar;
}
public void increment() {
this.bar++;
System.out.println("value is now: " + bar);
}
}
public class Closure {
public static void main(String[] args) {
System.out.println("hello");
Foo foo1 = new Foo();
Foo foo2 = new Foo();
final Foo[] theFoo = {foo1};
Incrementer incrementer = new Incrementer() {
public void increment() {
theFoo[0].increment();
}
};
incrementer.increment();
incrementer.increment();
incrementer.increment();
theFoo[0] = foo2;
incrementer.increment();
incrementer.increment();
System.out.println("foo1 = " + foo1.getBar());
System.out.println("foo2 = " + foo2.getBar());
}
}