如何设置窗口焦点

时间:2013-12-26 15:59:38

标签: android

在Android游戏应用程序中,我有启动,恢复和退出按钮的初始屏幕布局。从开始按钮我转到surfaceview并从那里开始一个工作线程。 我想正确实现设备后退按钮。当用户触摸后退按钮时,我回到启动画面菜单。问题是这样的:当我返回启动画面时,启动,恢复和退出按钮不再对用户交互作出反应。似乎他们放松了焦点。 如何将启动画面重新设置为焦点?

public class SplashScreen extends Activity {

    ...
    ...
    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.splash_screen_layout);

        this.mRoadView = (RoadView) findViewById(R.id.road_view);

        this.mStartButton = (Button) findViewById(R.id.start_button);
        this.mResumeButton = (Button) findViewById(R.id.resume_button);
        this.mExitButton = (Button) findViewById(R.id.exit_button);

        prepareButtonListeners();
    }

    @Override
    public void onBackPressed() {
        if (mDoubleBackPressed) {
            super.onBackPressed();
        } else {
            setContentView(R.layout.splash_screen_layout);
            mDoubleBackPressed = true;
        }
    }

    private void prepareButtonListeners() {
        this.mStartButton.setOnClickListener(new OnClickListener() {            
            @Override
            public void onClick(View v) {
                mDoubleBackPressed = false;
                setContentView(R.layout.road_view_layout);
            }
        });

        ...
        ...
        ...

非常感谢, 丹尼尔。

1 个答案:

答案 0 :(得分:0)

KeyEvent downEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK);

KeyEvent upEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK);

view.dispatchTouchEvent(downEvent);

view.dispatchTouchEvent(upEvent);