Android之自定义popwindow

public class SexPopWindow extends PopupWindow {
    private TextView boy;
    private TextView girl;
    public SexPopWindow(Activity context) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View content = inflater.inflate(R.layout.sex_pop, null);
        initview(content);
        this.setContentView(content);
        //xml布局需要在外面在嵌套一层    不然布局文件不是你想要的
        this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        this.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.update();//刷新状态
        ColorDrawable dw = new ColorDrawable(0000000000);//半透明
        this.setBackgroundDrawable(dw);// 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ,设置其他控件变化等操作
        this.setAnimationStyle(R.style.AnimationPreview); // 设置SelectPicPopupWindow弹出窗体动画效果
    }

    private void initview(View v) {
        boy = v.findViewById(R.id.boy);
        girl = v.findViewById(R.id.girl);
        boy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View pView) {
                EventBean bean = new EventBean();
                bean.setContent(boy.getText().toString());
                EventBus.getDefault().post(bean);
                SexPopWindow.this.dismiss();
            }
        });

        girl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View pView) {
                EventBean bean = new EventBean();
                bean.setContent(girl.getText().toString());
                EventBus.getDefault().post(bean);
                SexPopWindow.this.dismiss();
            }
        });
    }

    //显示popupWindow
    public void showPopupWindow(View v) {
        if (!this.isShowing()) {
         //   this.showAtLocation(v, Gravity.BOTTOM, 0, 0);        //相对于屏幕的
            this.showAsDropDown(v, 0, 0);  //相对于某个控件的位置
        } else {
            this.dismiss();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值