popupWindow 使用

更新:

虚拟按键不覆盖:

mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
 
全屏幕设置(有了虚拟按键全屏设置不要再使用直接设置屏幕高度的办法)用
mPopupWindow = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);//全屏幕设置
或者
setContentView(contentView);
setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
setHeight(ViewGroup.LayoutParams.MATCH_PARENT);



1、android 对话框常见的两种:dialog,popupWindow。其中dialog是android提供的,包含了很多,现成的定制,同时,功能也受到了限制。普通窗口建议用dialog。

dialog满足不了的用popupWindow,popupWindow 是很自由的自定义显示界面。官方文档的描述是:A popup window that can be used to display an arbitrary view(任意视图). The popup window is a floating container that appears on top of the current activity(显示在当前activity 前).


2、popupWindow使用 参见官网:http://developer.android.com/reference/android/widget/PopupWindow.html

AlertDialog 使用和普通控件类似只要定义在指定activity 的Context,需要的时候show出来就行了。

popopWindow 使用上有区别:1、定义popupwindow关联自己的视图,2、显示popupWindow 需要一个指定视图做参考坐标

它的构造函数有好多个,我用到的是

public PopupWindow (View contentView, int width, int height, boolean focusable)
contentView the popup's content
width the popup's width
height the popup's height
focusable true if the popup can be focused, false otherwise


显示函数用:其中parent 及是 参考坐标视图
public void showAtLocation (View parent, int gravity, int x, int y)
parent a parent view to get the getWindowToken() token from
gravity the gravity which controls the placement of the popup window
x the popup's x location offset
y the popup's y location offset

gracity 可以选择头部,中部(Gravity.CENTER),底部


3、使用注意
popupWindow 默认是不响应内部的响应事件的,所以用了前面说的构造函数,最后一个参数设置为true
这样子还不够必须,添加以下代码:

        // 这两句加了,才能点击并且响应
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());// 响应返回键必须的语句。
        mPopupWindow.setOutsideTouchable(true);// 点击外部按钮 取消。按返回按键也取消

在popupwindow的xml界面 外部布局里可以添加android:focusableInTouchMode="true"   (不是必须的,如果已经有添加上面的几步的话)

4、如果popupwindow里有edit的话可以添加以下代码,增强用户体验,及点击空白区域,缩下键盘
更好的方式:
 首先让edit失去焦点
让其父类控件获取焦点(就不会一进入就获取键盘)
            android:focusable="true"
            android:focusableInTouchMode="true"

其次重写
@Override
public boolean onTouchEvent(MotionEvent event) {

InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
return super.onTouchEvent(event);

}


普通方式:



popView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                InputMethodManager imm = (InputMethodManager) mContext
                        .getSystemService(Context.INPUT_METHOD_SERVICE);
                // login_auto_account.setCursorVisible(false);
                imm.hideSoftInputFromWindow(edit_mony.getWindowToken(), 0);
            }
        });


private void updatePopwindows(final HashMap
     
     
      
       map) {

        edit_business.setText(String.valueOf(map.get(BUSINESS)));
        
        radioGroup.clearCheck();
        if (map.get(TAB_NUM).equals(TAB_INCOME)) {
            edit_mony.setText(String.valueOf(map.get(INCOME)));
            radio_income.setChecked(true);

        } else {
            edit_mony.setText(String.valueOf(map.get(OUTLAY)));
            radio_outlay.setChecked(true);
        }

        String datesString = (String) map.get(DATE);
        String[] dates = datesString.split("-");
        mDatePicker.init(Integer.valueOf(dates[0]),
                Integer.valueOf(dates[1]) - 1, Integer.valueOf(dates[2]), null);

        int width = (int) (getWindowManager().getDefaultDisplay().getWidth());
        int height = (int) (getWindowManager().getDefaultDisplay().getHeight());

        // mDatePicker.set

        final PopupWindow mPopupWindow = new PopupWindow(popView, width,
                height, true);// 自己视图

        // 这两句加了,才能点击并且响应
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());// 响应返回键必须的语句。
        mPopupWindow.setOutsideTouchable(true);// 点击外部按钮 取消。按返回按键也取消

        // 判断2.3版本
        // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
        // mDatePicker.setCalendarViewShown(false);
        // }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // 新的 api
            mDatePicker.setCalendarViewShown(false);
        } else {
            // 2.3 api
        }

        btn_cancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mPopupWindow.dismiss();
            }
        });

        btn_ok.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                String dateString = mDatePicker.getYear() + "-"
                        + mDatePicker.getMonth() + "-"
                        + mDatePicker.getDayOfMonth();

                HashMap
      
      
       
        mHashMap = new HashMap
       
       
        
        ();

                // api 插入
                ContentValues cValues = new ContentValues();
                mDatabase = mdatebaseUtils.getWritableDatabase();

                switch (radioGroup.getCheckedRadioButtonId()) {
                case R.id.radio_income:

                    cValues.put(DATE, dateString);
                    cValues.put(BUSINESS, edit_business.getText().toString());
                    cValues.put(INCOME, edit_mony.getText().toString());
                    // mDatabase.insert(TAB_INCOME, null, cValues);
                    mDatabase.update(TAB_INCOME, cValues, "num=?",
                            new String[] { String.valueOf(map.get(NUM)) });
                    break;

                case R.id.radio_outlay:
                    cValues.put(DATE, dateString);
                    cValues.put(BUSINESS, edit_business.getText().toString());
                    cValues.put(OUTLAY, edit_mony.getText().toString());
                    // mDatabase.insert(TAB_OUTLAY, null, cValues);
                    mDatabase.update(TAB_OUTLAY, cValues, "num=?",
                            new String[] { String.valueOf(map.get(NUM)) });
                    break;

                default:
                    break;
                }

                mDatabase.close();
                // mAdapter.notifyDataSetChanged();
                query();

                mPopupWindow.dismiss();
            }
        });

        // 显示视图与指定视图之间的偏差
        // mPopupWindow.showAsDropDown(mListView);//指定视图下方
        mPopupWindow.showAtLocation(mListView, Gravity.TOP, 0, 0);// 指定视图偏移
    }

       
       
      
      
     
     


5、精确设置popwindow 大小
/*
     * 带测量popwindow高度的方法
     */
    @Deprecated
    private void hintPopWindow() {


        final View fatherView = getLayoutInflater().inflate(
                R.layout.activity_set, null);
        
        //外圈布局是指定pop大小的时候会指定出这个大小。所以无法代表原来窗口的大小
        final View popView = getLayoutInflater().inflate(
                R.layout.popwindow_set, null);
        
        //内圈布局-真实窗口大小。到时候测出这个大小  回调函数设置具体的大小
        final View subView = popView.findViewById(R.id.layout_set);
 
        //设置回调函数  在新生成的窗口的瞬间回调该函数  重新写窗口高度  宽度
        ViewTreeObserver vto = popView.getViewTreeObserver();
        vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {


            public boolean onPreDraw() {


                int h = subView.getMeasuredHeight();
                int w = subView.getMeasuredWidth();


                mPopupWindow.dismiss();
                mPopupWindow.setHeight(h);
                mPopupWindow.showAtLocation(fatherView, Gravity.BOTTOM, 0, 0);// 指定视图偏移


                return true;
            }


        });




        int width = (int) (getWindowManager().getDefaultDisplay().getWidth());
        int height = (int) (getWindowManager().getDefaultDisplay().getHeight());


        Log.d("11", height + " -----height");


        // mDatePicker.set
        mPopupWindow = new PopupWindow(popView, width, height, true);// 自己视图


        // 这两句加了,才能点击并且响应
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());// 响应返回键必须的语句。
        mPopupWindow.setOutsideTouchable(true);// 点击外部按钮 取消。按返回按键也取消


        // 显示视图与指定视图之间的偏差
        // mPopupWindow.showAsDropDown(mListView);//指定视图下方
        mPopupWindow.showAtLocation(fatherView, Gravity.BOTTOM, 0, 0);// 指定视图偏移
    }

ex:

private void updatePopwindows(Activity context, View view) {



View popView = LayoutInflater.from(context).inflate(R.layout.popupwindow_task,null);

TextView logTextView = (TextView) popView.findViewById(R.id.txt_task_info);


// mDatePicker.set


//final PopupWindow mPopupWindow =new PopupWindow(popView, (int) (width), (int) (height),true);// 这句过时了。用下一句

mPopupWindow = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);//全屏幕设置

mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);//虚拟按键不覆盖的处理

// 这两句加了,才能点击并且响应

mPopupWindow.setBackgroundDrawable(newBitmapDrawable());// 响应返回键必须的语句。

mPopupWindow.setOutsideTouchable(true);// 点击外部按钮 取消。按返回按键也取消


// 显示视图与指定视图之间的偏差

// mPopupWindow.showAsDropDown(mListView);//指定视图下方

mPopupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);// 指定视图偏移

popView.findViewById(R.id.imageView_close).setOnClickListener(new OnClickListener() {


@Override

publicvoid onClick(View v) {

//TODO Auto-generated method stub

mPopupWindow.dismiss();

}

});

popView.findViewById(R.id.imageView_close).setOnClickListener(new OnClickListener() {

@Override

publicvoid onClick(View v) {

//TODO Auto-generated method stub

mPopupWindow.dismiss();

}

});

}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值