简体   繁体   English

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

[英]OnTouchListener in custom dialog don't work

I have an an activity called GameViewUI. 我有一个名为GameViewUI的活动。 In this activity I have this method: 在此活动中,我使用以下方法:

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

}

As well as this one: 以及这一个:

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

}

The dialog "pops up" when this code launch: 该代码启动时,对话框“弹出”:

 showDialog(1);

The dialog pops up, and I can see the buttons. 对话框弹出,我可以看到按钮。 But they aren't clickable! 但是它们不是可点击的! I can't click them. 我无法点击它们。

What do I do wrong? 我做错了什么?

I want to click the buttons and get to the other activity. 我想单击按钮并转到其他活动。

Please help. 请帮忙。

Maybe, you can change the logic. 也许您可以更改逻辑。 As long as it is an Alert Dialog you can try this and declare everything you need in an Alert method. 只要它是一个Alert对话框,您就可以尝试执行此操作,并在Alert方法中声明所需的所有内容。 This works for me. 这对我有用。

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

         }  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM