我正在制作一款安卓游戏。我正在使用RelativeLayout,它承载一个FrameLayout,它承载一个SurfaceView。但是,我在RelativeLayout上添加了一个textview,以显示玩家剩下的健康点数。我有一个方法,它设置他留在文本视图中的健康点数的int值。 int值位于SurfaceView中,这是另一个类。但是textview和更新textview的方法是在另一个类中。我希望不断更新其他类中的方法,以便textview始终显示健康值。我怎样才能做到这一点?该方法称为updateHealthPointsTextView()。请帮帮我,非常感谢!我的代码:
package com.mysoftwaremobileapps.alien.attack;
import java.util.ArrayList;
public class GameScreenActivity extends Activity {
/** Called when the activity is first created. */
List<String> myList = new ArrayList<String>();
RadioButton radioEasy, radioMedium, radioHard;
private ExampleView eView;
public int ParachuterIndex;
TextView healthPoints;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dropParachuters();
}
private void dropParachuters() {
// TODO Auto-generated method stub
}
@Override
protected void onStart()
{
FrameLayout fl = new FrameLayout(this);
eView = new ExampleView(this);
fl.addView(eView);
healthPoints = new TextView(this);
healthPoints.setText("Health Points: " + eView.getThread().healthPoints);
RelativeLayout relativeLayout= new RelativeLayout(this);
relativeLayout.addView(fl);
relativeLayout.addView(healthPoints);
setContentView(relativeLayout);
eView.getThread().drawAlien();
eView.getThread().drawAlien();
eView.getThread().drawAlien();
eView.getThread().drawAlien();
eView.getThread().drawAlien();
eView.getThread().drawAlien();
eView.getThread().drawAlien();
eView.getThread().drawAlien();
eView.getThread().drawAlien();
eView.getThread().drawAlien();
eView.getThread().publicFloatX = 750;
eView.getThread().gettingAttacked();
updateHealthPointsTextView();
super.onStart();
try {
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("KEY");
}
} catch (Exception e) {
// TODO: handle exception
}
}
private void updateHealthPointsTextView() {
// TODO Auto-generated method stub
//Updating textview with health points value
healthPoints.setText(Integer.toString(eView.getThread().healthPoints));
}
}
@Override
public boolean onKeyDown(int KeyCode, KeyEvent Event) {
if ((KeyCode == KeyEvent.KEYCODE_MENU)) {
//Call performSpecialAttack()
Log.d("Parachute Hunter", "calling performSpecialAttack()");
eView.getThread().performSpecialAttack();
return true;
}
return super.onKeyDown(KeyCode, Event);
}
@Override
protected void onStop()
{
try {
eView.getThread().setRunning(false);
eView = null;
}
catch (NullPointerException e) {}
super.onStop();
}
}
答案 0 :(得分:0)
如何使用这样的方法:
public void setHealthPoints(int points) {
eView.getThread().healthPoints = points;
//Updating textview with health points value
healthPoints.setText("Health Points: " + Integer.toString(points));
}
}
从任何地方调用它,它不应该阻止GUI-Thread
答案 1 :(得分:0)
只需在该类的构造函数中传递textview的引用,并使用handler来频繁更新它。
public final class MethodUtil {
TextView mTextView;
public MethodUtil(TextView mTextView) {
//this.mTextView =(TextView)mTextView.findViewById(R.id.textview);
this.mTextView=mTextView;
}
public void updateHealthPointsTextView(){
mTextView.post(new Runnable() {
@Override
public void run() {
mTextView.setText("Health Points: " + eView.getThread().healthPoints);
}
});
}
}
答案 2 :(得分:0)
我通过简单地使倒数计时器从1秒倒计时然后重新启动并在结束时更新textview来修复它。它的工作原理
public void updateHealthPointsTextView() {
//The aliens are firing and throwing rocks, make the defender lose health
new CountDownTimer(1000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
//Updating textview
healthPoints.setText("Health points: " + Integer.toString(eView.getThread().healthPoints));
updateHealthPointsTextView();
}
}.start();
}