简体   繁体   中英

OnTouchListener in custom dialog don't work

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.

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

         }  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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