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
- activity :dispatch touch event
- activity :onUserInteraction
- layout :dispatch touch event
- layout :onInterceptTouchEvent
- button :onTouch(action_down)
ACTION_UP
- activity :dispatch touch event
- layout :dispatch touch event
- layout :onInterceptTouchEvent
- button: ontouch..
so you can see the method can only be transfer to up view.
imageview is the same level to the button..
精彩评论