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

发布于 2024-12-09 04:58:14 字数 1400 浏览 0 评论 0原文

我有一个名为 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);

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

我做错了什么?

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

请帮忙。

I have an an activity called 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海风掠过北极光 2024-12-16 04:58:14

也许,你可以改变逻辑。只要它是一个警报对话框,您就可以尝试这个并在警报方法中声明您需要的所有内容。这对我有用。

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

         }  

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

         }  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文