-1

我试图在 onTouchListener 内显示警报,但无法显示。我对此很陌生,但我一直在关注一些好的教程,但无济于事。这是代码的一部分...任何想法为什么此警报不会显示?

mSwitcher.setOnTouchListener(new OnTouchListener()
{
   public void onItemClick(AdapterView<?> parent, View v, int position, long id)
   {
   }

   @Override public boolean onTouch(View v, MotionEvent event)
   {
      // the attempt at the alert
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      builder.setMessage("Are you sure you want to exit?")
             .setCancelable(false)
             .setPositiveButton("Yes", new DialogInterface.OnClickListener()
                {
                   public void onClick(DialogInterface dialog, int id)
                   {
                      MyActivity.this.finish();
                   }
                })
             .setNegativeButton("No", new DialogInterface.OnClickListener()
                {
                   public void onClick(DialogInterface dialog, int id)
                   {
                      dialog.cancel();
                   }
                });
      AlertDialog alert = builder.create();
      return false;
   }
});

我认为我的结构还可以,但我什至无法编译。

4

3 回答 3

4

尝试改变

AlertDialog alert = builder.create();

AlertDialog alert = builder.show();
于 2011-01-18T15:45:01.147 回答
2

我更新了代码,使括号位于正确的位置。它现在应该编译。正如 JLund 指出的那样,将最后一行从builder.create();to更改为builder.show();应该可以。如果您想保留builder.create();通话,只需在通话alert.show();后添加即可。

于 2011-01-18T16:10:03.963 回答
0

您几乎完成了显示对话框警报,但似乎您忘记显示AlertDialog,使用show(). 将此行添加到代码的末尾,但在inside之前。AlertDialog

return false;onTouch()

alert.show();

于 2014-06-19T09:00:01.197 回答