我正在制作一个安卓游戏。我正在使用RelativeLayout,它承载一个FrameLayout,它承载一个SurfaceView。但是,我在 RelativeLayout 上添加了一个文本视图,以显示玩家还剩下多少生命值。我有一个方法可以设置他在文本视图中留下多少健康点的 int 值。int 值位于 SurfaceView 中,它是另一个类。但是文本视图和更新文本视图的方法在另一个类中。我希望其他类中的方法不断更新,让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();
}
}