.Override方法的不同类

时间:2014-08-14 10:30:43

标签: android eclipse class

onBackPressed上调用Activity class的{​​{1}}方法?可能吗?那我该怎么办?谢谢你的帮助

更新:我刚创建了我的游戏的GameView,它延伸到View Class。我创建了一个变量,每当我完成每个级别时它都会递增,因此它将限制每个游戏的级别。所以我想实现一个View Class,只要玩家按下后退键,我就可以将增量变量设置回零。

onBackPressed method 主要活动

FULL CODE:

然后进入菜单类

public class MainActivity extends Activity {    
private GameView mGameView;
@Override
public void onBackPressed() {
    if (mGameView.interceptBackPressed()) {
        return;
    }
    // TODO Auto-generated method stub
    super.onBackPressed();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    mGameView = (GameView) findViewById(GameView.countmaze);

然后在我的 GameView类

public class menu extends Activity implements OnClickListener {
static int nextmaze;
int countmaze = 0;
GameView gameView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button btnplay = (Button) findViewById(R.id.btnPlay);
    btnplay.setOnClickListener(this);
}
@Override
public void onBackPressed() {
    //I have here an AlertDialog builder
}
@Override
public void onClick(View v) {
    case R.id.btnPlay:
        Toast.makeText(getApplicationContext(), "LEVEL 1",
                Toast.LENGTH_SHORT).show();
        startnextmaze();

    }
}
void startnextmaze() {
    Random rand = new Random();
    Intent game = new Intent(menu.this, Game.class);
    nextmaze = rand.nextInt(5) + 1;
    Maze maze = MazeCreator.getMaze(nextmaze);
    game.putExtra("maze", maze);
    startActivity(game);
}

2 个答案:

答案 0 :(得分:3)

在Activity onBackPressed上创建一个View类将实现的回调

实施例

public void interface onBackPressedHandler {

   public void onBackPressed();
}

活动类

public onBackPressedHandler mHandler;


@Override 
public void onCreate(....) {

   GameView game... //Inflate or create.
   game.setActivity(this);

}

public void setListener(onBackPressedHandler handler) {
    mHandler = handler;
}

@Override
public voind onBackPressed() {

  if(mHandler != null) {
       mHandler.onBackPressed();
  }
  super.onBackPressed();

查看课程

public GameView extends View implements onBackPressedHandler {

public void setActivity(Activity activity) {
     activity.setListener(this);
}    


public void onBackPressed() {
  //your code here.
    }
}

此示例显示当您按下后退按钮时,它将调用视图上的onBackPress。

希望它有所帮助。

答案 1 :(得分:1)

使用以下更改更新您的代码:

<强>活动

public class MyActivity extends Activity {
    private GameView mGameView;

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.main);
        //View reference to object defined in XML
        mGameView = (GameView) findViewById(R.id.gameview);//id must be specified in XML
        //I assumed that GameView is a part of your XML layout

        //View created programmatically.
        mGameView = new GameView(this);

       //Use one of the above initialisation techniques of mGameView.
    }

    @Override
    public void onBackPressed() {
        //here we are calling method on mGameView which cannot be null
        //that's why we initialised it in onCreate() method
        if (mGameView.interceptBackPressed()) {
            return;
        }
        super.onBackPressed();//finish your Activity
    }
}

<强> GameView

public class GameView extends View {

    public boolean interceptBackPressed() {
        //TODO handle your game logic here and return true if you don't
        //want your game to be shutdown, otherwise return false
        return true;
    }

}