设置按钮背景drawable在不同的类

时间:2013-11-26 06:23:29

标签: android button android-activity background drawable

我正在开发一款适用于Android的游戏,并且在解决这个问题时遇到了一些麻烦。

我有一个主要活动,我有一个GameView(SurfaceView)。我想在GameView中执行操作时更改MainActivity中按钮的背景图像。

我的MainActivity是一个Activity,GameView不是一个Activity,只是在我的MainActivity中调用和显示。

我考虑过做以下事情:

主要活动:

public void setNumberLivesPicture(final Drawable background){
        MainActivity.this.runOnUiThread(new Runnable() {     
            public void run() {         
                numberLivesPicture.setBackgroundDrawable(background);     
            } 
         });
    }

GameView:

public class GameView extends SurfaceView{

    int difficulty =  HomePage.getDifficulty();
    int player = HomePage.getPlayer();
    String direction =  MainActivity.getDirection();
    boolean audio = HomePage.getAudio();
    private Drawable bloodspatterdead;
    Button numberLivesPicture;
    Button numberLivesButton;
    MainActivity mAct;


    public GameView(Context context, AttributeSet attributeSet, MainActivity MActivity, Button NumberLives) {
        super(context, attributeSet);
        gameLoopThread = new GameLoopThread(this);
        if (audio == true){
            backgroundNoise.start();
        }       
        getHolder().addCallback(new SurfaceHolder.Callback()
        {

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                boolean retry = true;
                GameLoopThread.setRunning(false);
                while (retry) {
                    try {
                        gameLoopThread.join();
                        retry = false;
                    } catch (InterruptedException e) {}
                }
            }           

            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                createSprites(difficulty);
                GameLoopThread.setRunning(true);
                gameLoopThread.start();
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format,
                    int width, int height) {
            }
        });
    }


    private void checkForEnemyCollision() {

        int x = playerSprite.getPlayerX();
        int y = playerSprite.getPlayerY();

        Rect playerRect = new Rect(x, y, (x + playerSprite.getPlayerWidth()), (y + playerSprite.getPlayerHeight()));
        for (int i = 0; i<sprites.size(); i++)
        {
            Rect zombieSprite = sprites.get(i).getZombieBounds();
            if(zombieSprite.intersect(playerRect))
            {           
       if (justDied == true){         // if you just died, we'll give you a break
                }

                if (justDied == false){

    if (NumberOfLives <= 1){   // if you do not have any extra lives.. you die!

                        if (shieldsActivated >= 1){
                            shieldsActivated--;
                            if (audio == true){
                                shielddestroyed.start();
                            }
                        }

                        if (shieldsActivated == 0)
                        {           

((MainActivity) getContext()).setTextView("THE ZOMBIES ATE YOUR BRAINS!!");

                        if (audio == true){
                            backgroundNoise.stop();
                            youaredeadmessage.start();              
                            }

                        for (int z = 0; z<playerSprites.size(); z++)
                        {
                        playerSprites.remove(z);
                        }
                        alive = false;

                    }
                    }
                    if (NumberOfLives > 1){

                        if (shieldsActivated >= 1){
                            shieldsActivated--;
                            if (audio == true){
                                shielddestroyed.start();
                            }
                        }

                        else if (shieldsActivated == 1){
                            shieldsActivated=0;
                            if (audio == true)
                            {
                                shielddestroyed.start();
                            }
                        }

else if (shieldsActivated == 0){                        
NumberOfLives = NumberOfLives -1;
justDied = true;

if (NumberOfLives == 4){

//Button numberOfLives2 = (Button) findViewById(R.id.numberLivesPicture);
numberLivesPicture = (Button) findViewById(R.id.numberLivesPicture);

((MainActivity) getContext()).setNumberLivesPicture(R.id.numberLivesPicture);                           numberLivesPicture.setBackgroundResource(R.drawable.numberlives4);
                                }

但这不起作用:(

编辑:

我已经成功地使用了它来改变TextView中的TEXT,但我不确定如何使用这样的方法来改变背景:

public void setTextView(final String txt){
        MainActivity.this.runOnUiThread(new Runnable() {     
            public void run() {         
                txv.setText(txt);     
            } 
         });
    }

用于:

((MainActivity) getContext()).setTextView("THE ZOMBIES ATE YOUR BRAINS!!");

任何建议都将不胜感激!

2 个答案:

答案 0 :(得分:2)

我建议你使用面向对象的方法论。将Activity中的Button引用引用到surfaceView类,然后使用runUiThread在任何条件下更新Button的背景

public class SurFaceViewClass extends SurfaceView {

Button btnButton;
MainActivity mAct;

public SurFaceViewClass(MainActivity mact, Button btn) {
    super(mact);
    mAct = mact;
    btnButton = btn;
}

@Override
public boolean onTouchEvent(MotionEvent event) {

    // if(yourconnditIOn)
    mAct.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            btnButton.setBackgroundResource(R.drawable.ic_launcher);
        }
    });
    return super.onTouchEvent(event);
}
}
当SurfaceView在自己的线程中运行时,运行runOnUiThread是必要的。现在看看你将如何在Activity

中调用它
    SurFaceViewClass msClass=new SurFaceViewClass(this, yourbttn);

答案 1 :(得分:1)

可能有几种可能的方法。你可以使用广播发送者和接收者。

下面提到的步骤:

步骤1:在您的主要活动中创建一个广播接收器,以便从您的表面视图接收广播。

BroadcastReceiver receiver = new BroadcastReceiver(){

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("update_button")) {
            // UPDATE YOUR BUTTON HERE.
        }
    }
};

步骤2:在主要活动中创建广播接收器后注册接收器。

IntentFilter filter = new IntentFilter("update_button");
registerReceiver(receiver, filter);

步骤3:从水面视图发送广播以更新按钮。

Intent intent = new Intent("update_button");
// HERE YOU CAN PUT EXTRAS IN  YOUR INTENT
SendBroadcast(intent);

无论您在何处注册广播接收器,它都会自动接收广播并更新主要活动中的按钮。使用上述方法,您可以更新按钮或任何类别的任何数据。