【问题标题】:OnTouchListener in custom dialog don't work自定义对话框中的 OnTouchListener 不起作用
【发布时间】:2011-10-12 13:12:12
【问题描述】:

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

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

我做错了什么?

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

请帮忙。

【问题讨论】:

  • playAgainButton.setOnTouchListener(this) 为什么不使用 setOnClickListener? developer.android.com/guide/topics/ui/…
  • @atzu 我也试过了。完全没有区别。
  • 尝试将 xml 上的 android:onClick="onTouch" 添加到按钮中。必须是 clickListeners。
  • @atzu 也试过了。没有不同。当我添加了一个监听器时,这个按钮甚至没有被“按下”。当我点击时没有任何反应。调试时根本不进入onTouch方法。我只是不明白为什么。

标签: java android xml dialog ontouchlistener


【解决方案1】:

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

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

         }  

【讨论】:

  • 这实际上解决了我的问题。听众正在工作!仍然很高兴知道为什么旧方法不起作用......所以我可以从中学到一些东西。另外,现在我无法制作自定义对话框,但无论如何它都可以工作。但是非常感谢您的回答,现在我可以继续实现其他功能,而不是被困在这里。干得好!
  • LayoutInflater inflater = getLayoutInflater();查看 dialoglayout = inflater.inflate(R.layout.gameoverdialog); builder.setView(dialoglayout);
猜你喜欢
  • 1970-01-01
  • 2016-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多