简体   繁体   中英

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

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 .

View 's can trigger the onClick(View v) callback if you set the OnClickListener upon them. When the callback is invoked, you can simply switch trough View.getId(), in order to understand which View has been clicked. If the view 's clicked is the right one, instantiate and show your dialog.

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);
  2. use onClickListener for mImageView :

     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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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