1

我正在制作一个安卓游戏。我正在使用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();
}
}
4

3 回答 3

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

于 2012-07-18T11:29:15.840 回答
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();
    }
于 2012-07-18T15:04:01.667 回答
0

只需在该类的构造函数中传递 textview 的引用并使用处理程序频繁更新它。

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);

    }
});
}
}
于 2012-07-18T11:34:57.727 回答