自定义对话框中的 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);
弹出对话框,我可以看到按钮。但它们不可点击!我无法点击它们。
我做错了什么?
我想单击按钮并进入其他活动。
请帮忙。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许,你可以改变逻辑。只要它是一个警报对话框,您就可以尝试这个并在警报方法中声明您需要的所有内容。这对我有用。
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.