popWindow的使用方法

popWindow的使用方式

=====

popWindow的使用方法_第1张图片
今天说的是比较简单的popwindow,弹出框,这个其实挺简单的主要是有个地方比较可能会出问题就是点击事件

[java] view plain copy
private void initPopWindow(final TextView tview) {  
        final String[] name = { "份", "斤", "个", "人", "桌", "只", "半份", "小份", "大份",  
                "两", "半只", "锅", "碗", "壶", "打", "瓶", "杯", "听", "罐", "例", "套",  
                "串" }; 
        View contentView = LayoutInflater.from(getActivity()).inflate(  
                R.layout.popwindow, null); 
        contentView.setBackgroundColor(Color.WHITE); 

        popupWindow = new PopupWindow(findViewById(R.id.fl_layout), 159, 250); 

        popupWindow.setContentView(contentView); 

        TextView textView = (TextView) contentView.findViewById(R.id.text); 
        textView.setText("单位"); 

        ListView listView = (ListView) contentView.findViewById(R.id.list); 
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),  
                android.R.layout.simple_list_item_1, name); 
        listView.setAdapter(adapter); 

`[java] view plain copy
//这个很重要的没有这个 点击popwindow以外的布局pop是不会消失的,本人在此犯了大错切记

popupWindow.setBackgroundDrawable(new PaintDrawable());  
[java] view plain copy
//获取焦点 
    popupWindow.setFocusable(true);  
[java] view plain copy
//点击popwindow以外的布局让pop消失 
opupWindow.setOutsideTouchable(true);  
[java] view plain copy
//是在哪个布局(控件)下面(可以修改上下左右的都可以) 
opupWindow.showAsDropDown(tview);  
[java] view plain copy
     //监听 popupWindow消失的监听 

    popupWindow.setOnDismissListener(new OnDismissListener() {  

        @Override  
        public void onDismiss() {  

        }  
    });  

    listView.setOnItemClickListener(new OnItemClickListener() {  

        @Override  
        public void onItemClick(AdapterView<?> parent, View view,  
                int position, long id) {  
            mUp.setImageDrawable(getActivity().getResources()  
                    .getDrawable(R.drawable.puj));  
            popupWindow.dismiss();  
            tview.setText(name[position]);  
        }  
    });  
}  

一些其他的bug在这里进行汇总:

给显示这个popwindow的activity设置一个theme属性!–这样activity背景会变成黑色需要设置背景色为#ffffff 放置魅族的虚拟按键遮挡

设置成不被键盘挡住

this.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

你可能感兴趣的:(PopWindow的)