如何更改Runnable中方法的变量?
How to change the variable of a method in Runnable?
我想做的是改变我的 运行() 方法,这样它就可以接收不同的变量。我想将 player1life 更改为其他内容,以便我可以在不同的 player2life 上使用 customHandler.postDelayed(updateTimerThread, 0);。
代码的作用是,当按下按钮时,变量会更改其值。
注意:我是 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;
}
});
我闻到了 XY 问题...
为什么不创建一个不同的 Runnable
来处理 player2life
?在点击处理程序中,只需执行一个 if 语句来检查您应该减少哪些玩家的生命值!
if ... {
customHandler.postDelayed(runnableThatHandlesPlayer1Life, 0);
} else {
customHandler.postDelayed(runnableThatHandlesPlayer2Life, 0);
}
并且可以这样声明可运行对象:
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
class 。它应该会让事情变得更容易一些:
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 ();
}
}
问题是否只是超过了匿名内部 class 中对 final
变量的需求?如果是这样,您可以使用 "trick" 使用长度为 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());
}
}
我想做的是改变我的 运行() 方法,这样它就可以接收不同的变量。我想将 player1life 更改为其他内容,以便我可以在不同的 player2life 上使用 customHandler.postDelayed(updateTimerThread, 0);。 代码的作用是,当按下按钮时,变量会更改其值。
注意:我是 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;
}
});
我闻到了 XY 问题...
为什么不创建一个不同的 Runnable
来处理 player2life
?在点击处理程序中,只需执行一个 if 语句来检查您应该减少哪些玩家的生命值!
if ... {
customHandler.postDelayed(runnableThatHandlesPlayer1Life, 0);
} else {
customHandler.postDelayed(runnableThatHandlesPlayer2Life, 0);
}
并且可以这样声明可运行对象:
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
class 。它应该会让事情变得更容易一些:
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 ();
}
}
问题是否只是超过了匿名内部 class 中对 final
变量的需求?如果是这样,您可以使用 "trick" 使用长度为 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());
}
}