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);
retu开发者_JAVA百科rn 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;
}
}
精彩评论