自定义对话框中的OnTouchListener不起作用

时间:2011-10-12 13:12:12

标签: java android xml dialog ontouchlistener

我有一个名为GameViewUI的活动。在这个活动中我有这个方法:

int DIALOG_GAMEOVER_ID = 1;

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DIALOG_GAMEOVER_ID:
        Context mContext = this;
        Dialog dialog = new Dialog(mContext);

        dialog.setContentView(R.layout.gameoverdialog);
        dialog.setTitle("GAME OVER");

        TextView text = (TextView) dialog
                .findViewById(R.id.pointsGameOverTextView);
        text.setText("Points: " + currentScore);
        // Get buttons and add listeners
        Button playAgainButton = (Button) dialog
                .findViewById(R.id.playAgainButton);
        Button goToMainMenuButton = (Button) dialog
                .findViewById(R.id.goToMainMenuButton);

        playAgainButton.setOnTouchListener(this);
        goToMainMenuButton.setOnTouchListener(this);

        return dialog;
    }
    return super.onCreateDialog(id);

}

以及这一个:

public boolean onTouch(View view, MotionEvent event) {
    if (view.equals((Button) findViewById(R.id.goToMainMenuButton))) {
        Intent myIntent = new Intent(view.getContext(), MainUI.class);
        startActivity(myIntent);
        return true;
}

}

此代码启动时“弹出”对话框:

 showDialog(1);

弹出对话框,我可以看到按钮。但它们不可点击!我无法点击它们。

我做错了什么?

我想点击按钮进入其他活动。

请帮忙。

1 个答案:

答案 0 :(得分:0)

也许,你可以改变逻辑。只要它是一个警报对话框,你可以试试这个,并在Alert方法中声明你需要的一切。这适合我。

private AlertDialog.Builder builder;
     private void showAlert(int id){
            switch (id) {
            case DIALOG_GAMEOVER_ID:
                    builder.setMessage("GAME OVER")
                    .setCancelable(false)
                    .setPositiveButton("Go To Main", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                        Intent myIntent = new Intent(view.getContext(),MainUI.class);
                        startActivity(myIntent);


                        }
                    }).setNegativeButton("Play Again", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                        Intent myIntent = new Intent(view.getContext(),Custom.class);
                        startActivity(myIntent);


                        }
                    });

                    builder.create();
                    builder.show();
                    break;
              }

         }