开发者

how to set an OnTouchListener for a PopupWindow?

Here's my code that shows a PopUp when the button is touched and dismisses it, when the user releases his finger.

But I need to know weather the user had just released his finger or did he slide into the popup and release his finger from there.

In my code this doesn't really work, since I guess the popup doesn't get focus without the user releasing his finger. Is there a way to fix it?

Thanks!

    LinearLayout LinLay = new LinearLayout(this);
    ImageView ivg = new ImageView(this);
    ivg.setImageResource(R.drawable.icon);
    LinLay.addView(ivg);
    final PopupWindow pw = new PopupWindow(LinLay, 100, 100, true);

    final Button bt2 = (Button)findViewById(R.id.button2);
    bt2.setOnTouchListener(new OnTouchListener(){
        @Override
        public boolean onTouch(View v, MotionEvent event开发者_C百科) {
            if(event.getAction() == MotionEvent.ACTION_DOWN )
                {
                pw.showAtLocation(bt2, Gravity.CENTER, 0, 0); 
                }
            else if(event.getAction() == MotionEvent.ACTION_UP )
                {
                pw.dismiss();
                }
            return true;
        }
        });


    ivg.setOnTouchListener(new OnTouchListener(){
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_UP )
                {
                showToast(); /// doesn't work! :)
                }
            return true;
        }
        });
    }


public void showToast()
{
    Toast.makeText(this, "AAA!", 500).show();
}


To use imageview and imageview onclick() to display showpopoup() window

im = new ImageView(this);
        im.setImageResource(R.drawable.btn);

        im.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopup(R.layout.popup_example);
            }
        });

and

private void showPopup(int resourses) {

        final LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflate.inflate(resourses, null, false);
        ViewGroup viewgroup = (ViewGroup) view
                .findViewById(R.id.buttonContainer);
        LinearLayout layout = new LinearLayout(this);

        view.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                mPopup.dismiss();
                return true;
            }
        });

        mPopup = new PopupWindow(view, 160, LayoutParams.FILL_PARENT, true);
        mPopup.setAnimationStyle(android.R.style.Animation_InputMethod);
        mPopup.showAtLocation(view, Gravity.LEFT, 0, 10);
    }


obviously ,it does not work. message send as follow:

ACTION_DOWN

  1. activity :dispatch touch event
  2. activity :onUserInteraction
  3. layout :dispatch touch event
  4. layout :onInterceptTouchEvent
  5. button :onTouch(action_down)

ACTION_UP

  1. activity :dispatch touch event
  2. layout :dispatch touch event
  3. layout :onInterceptTouchEvent
  4. button: ontouch..

so you can see the method can only be transfer to up view.
imageview is the same level to the button..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜