PopupWindow使用 样式,监听

来源:互联网 发布:关于所有淘宝软件 编辑:程序博客网 时间:2024/06/05 07:07

PopupWindow要实现的效果



主要涉及两方面,一是样式的实现,二是点击对应位置的监听


这里是自定义pop初始化时设置的一些属性

public class MenuPopupWindow extends PopupWindow implements View.OnClickListener {    private View view;    TextView tvDeviceName, tvBindState, tvManualInput, tv_loadl_input;    View ll_bind_device;    /**     * @param act     * @param deviceName 传入需要显示的设备名     */    public MenuPopupWindow(Activity act, String deviceName, boolean isManual) {        super(act);        view = LayoutInflater.from(act).inflate(R.layout.pop_more_menu, null);        this.setContentView(view);        this.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);        this.setFocusable(true);        this.setOutsideTouchable(true);        this.setAnimationStyle(android.R.style.Animation_Dialog);        this.setBackgroundDrawable(new BitmapDrawable());//必要,保证背景是自定义的.9patch图        ll_bind_device = ViewUtil.getView(view, R.id.ll_bind_device);        tvDeviceName = ViewUtil.getView(view, R.id.tv_device_name);        tvBindState = ViewUtil.getView(view, R.id.tv_bind_state);        tvBindState = ViewUtil.getView(view, R.id.tv_bind_state);        tv_loadl_input = ViewUtil.getView(view, R.id.tv_loadl_input);        tvManualInput = ViewUtil.getView(view, R.id.tv_manual_input);        ll_bind_device.setOnClickListener(this);        tvManualInput.setOnClickListener(this);        tv_loadl_input.setOnClickListener(this);        if (!isManual) {            ViewUtil.getView(view, R.id.line).setVisibility(View.GONE);            tvManualInput.setVisibility(View.GONE);        }        if (!TextUtils.isEmpty(deviceName)) {            tvDeviceName.setText(deviceName);            tvDeviceName.setVisibility(View.VISIBLE);            tvBindState.setText("解除绑定");        } else {            tvDeviceName.setVisibility(View.GONE);            tvBindState.setText("绑定设备");        }    }    public MenuPopupWindow(Activity act, String deviceName) {        this(act, deviceName, true);    }    private OnMyClickListener listener;    public void setOnMenuItemClickListener(OnMyClickListener listener) {        this.listener = listener;    }    @Override    public void onClick(View v) {        this.dismiss();        if (listener != null) {            if (v.getId() == R.id.ll_bind_device) {                listener.onClick(R.id.ll_bind_device, "");            } else if (v.getId() == R.id.tv_manual_input) {                listener.onClick(R.id.tv_manual_input, "");            } else if (v.getId() == R.id.tv_loadl_input) {                listener.onClick(R.id.tv_loadl_input, "");            }        }    }    public void setMyHandInputVisibile(int visibile) {        tvManualInput.setVisibility(visibile);    }    public void setMyHandInputText(String text) {        tvManualInput.setText(text);    }}

其中的监听是利用回调来完成,当然还有很多其他方法,例如eventbus等

pop的xml布局要注意一点 两层包裹自定义背景才有效


最后就是使用了-->显示和设置监听

MenuPopupWindow pop = new MenuPopupWindow(this, PreferenceHelper.getString(PreferenceHelper.SWEAT_NANLYZER_NAME, ""), true);        pop.showAsDropDown(titlebarRightView);        pop.setOnMenuItemClickListener(new OnMyClickListener() {            @Override            public void onClick(int position, String flag) {                if (R.id.ll_bind_device == position) {                    if (PreferenceHelper.getBoolean(PreferenceHelper.SWEAT_NANLYZER_IS_BING, false)) {                        //TODO 解绑操作                        PreferenceHelper.putBoolean(PreferenceHelper.SWEAT_NANLYZER_IS_BING, false);                        PreferenceHelper.putString(PreferenceHelper.SWEAT_NANLYZER_ADDRESS, "");                        PreferenceHelper.putString(PreferenceHelper.SWEAT_NANLYZER_NAME, "");                        showToast("解绑成功");                        if (mBluetoothLeService != null) {                            mBluetoothLeService.close();                        }                        //tvBluetooth.setText("(未连接)");                    } else {                        scanLeDevice(true);                    }                } else if (R.id.tv_manual_input == position) {                    //TODO 手动输入                    System.out.println("采集频率");                } else if (R.id.tv_loadl_input == position) {                    ActivityUtils.showActivity(SweatAnalyzerActivity.this, MyShopStoreActivity.class);                }            }        });



1 0
原创粉丝点击