2

当我单击缩略图时,会打开一个弹出窗口。我想在onTouch方法中解除它,但问题是该onTouch方法没有被调用。那么我怎样才能让它运行呢?如何关闭弹出窗口?

我的代码是:

public void onItemImageClicked(View view)
{
    LinearLayout layout = (LinearLayout)     getLayoutInflater().inflate(R.layout.popup,null); 
    ImageView fullSizeImg = (ImageView) layout.findViewById(R.id.fullSizeImage);
    fullSizeImg.setImageBitmap(bitmap);
    fullSizeImg.setFocusable(false);
    Display display = getWindowManager().getDefaultDisplay(); 
    int deviceWidth = display.getWidth()-40; 
    int deviceHeight = display.getHeight()-70; 
    popupWindow = new PopupWindow(layout, deviceWidth, deviceHeight, true);
    // display the popup in the center
    popupWindow.showAtLocation(layout, Gravity.CENTER, 0, 0);
    popupWindow.getMaxAvailableHeight(new View(this));
    popupWindow.setFocusable(true);
    popupWindow.setTouchInterceptor(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            popupWindow.dismiss();
            return false;
        }
    });
    popupWindow.setTouchable(true);

}
4

2 回答 2

1

试试这个

                View popupView = globalconstant.layoutInflater.inflate(R.layout.popup, null);  
                popupWindow = new PopupWindow(popupView,globalconstant.displayWidth-20,480,true);
                popupWindow.setOutsideTouchable(true);
                popupWindow.setBackgroundDrawable(new BitmapDrawable());

onClickEvent

popupWindow.showAtLocation(v, Gravity.CENTER, 0, 15);
于 2012-09-04T07:48:50.443 回答
0
private void initPopupWindow() {  
// TODO Auto-generated method stub  

View view = getLayoutInflater().inflate(R.layout.main_choice, null);  

ListView main_menu_listview = (ListView) view.findViewById(R.id.main_menu_listview);  

ShowMainChoice madapter = new ShowMainChoice(context);
main_menu_listview.setAdapter(madapter);

int width = (int)getWindowManager().getDefaultDisplay().getWidth()/2;
popupWindow = new PopupWindow(view, width,WindowManager.LayoutParams.WRAP_CONTENT);  
popupWindow.setBackgroundDrawable(new BitmapDrawable());//this is important,如果缺少这句将导致其他任何控件及监听都得不到响应
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);

main_menu_listview.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
        // TODO Auto-generated method stub

        Log.e("++++++>", arg2+"");

    }
});

}

这个问题是popupwindow的消息机制决定的,因为它是阻尼式的。是因为弹出窗口不响应onTouch或onKey事件,除非它的背景是!= null。查看我编写的一些代码来帮助解决这个问题。在基本情况下,您可以调用 PopupWindow#setBackgroundDrawable(new BitmapDrawable()) 来强制它按照您期望的方式运行。您不需要自己的 onKey 侦听器。如果您希望它在用户单击窗口边界之外时消失,您可能还需要调用 PopupWindow#setOutsideTouchable(true)。

这是链接,祝你好运!

于 2013-08-22T02:52:18.510 回答