简体   繁体   English

Android:如何通过单击视图弹出窗口?

[英]Android: How to pop up a window by clicking views?

Here if that image is clicked, it opens a new window. 在这里,如果单击该图像,它将打开一个新窗口。 How to achieve this? 如何实现呢? Its not a Menu right?? 它不是菜单吧?

在此处输入图片说明

You can set an onClickListener on the ImageView like 您可以像这样在ImageView上设置onClickListener

mImageView.setOnClickListener(new View.OnClickListener() {
                            public void onClick(View v) {

                             // start a dialog

                            }
                        });

On click, you open a dialog http://developer.android.com/guide/topics/ui/dialogs.html . 点击后,您将打开一个对话框http://developer.android.com/guide/topics/ui/dialogs.html

View 's can trigger the onClick(View v) callback if you set the OnClickListener upon them. View的可以触发onClick(View v)如果你设置回调OnClickListener在他们身上。 When the callback is invoked, you can simply switch trough View.getId(), in order to understand which View has been clicked. 当回调被调用时,您可以简单地通过View.getId()来切换,以了解单击了哪个View If the view 's clicked is the right one, instantiate and show your dialog. 如果单击的view正确,则实例化并显示对话框。

try like this. 尝试这样。

mImageView.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                AlertDialog.Builder builder = new AlertDialog.Builder(
                        ctContext);
                builder.setItems(listdatatopopulate,
                        new DialogInterface.OnClickListener() {
                            public void onClick(
                                    DialogInterface dialogInterface,
                                    int item) {
                                // do some thing on item click
                            }
                        });
                builder.create().show();

            }
        });
  1. OnCreate you need this.registerForContextMenu(mImageView); OnCreate您需要this.registerForContextMenu(mImageView);
  2. use onClickListener for mImageView : 使用onClickListenermImageView

     mImageView.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { v.performLongClick(); } }); 

  3. Implement the context menu http://developer.android.com/guide/topics/ui/menus.html#context-menu 实施上下文菜单http://developer.android.com/guide/topics/ui/menus.html#context-menu

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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