版权声明:本文为博主原创文章,转载需注;
由于时间问题,就不做GIF动画了,先上几幅效果图
(我知道界面丑,这个就忽略了吧)
由于是模拟器,所有软键盘遮挡问题演示不了,不过解决方法代码有注明
下面直接上源码:
/** * PopInputWindow 封装弹窗输入框,并解决输入法弹出将其遮挡解决方法 * * @author zgq * @version 1.0, 2017/1/22 */ public class PopInputWindow { private Activity activity; private LayoutInflater layoutInflater; private PopupWindow pop; private EditText inputContent; private Button btnYes; public PopInputWindow(Activity activity) { this.activity = activity; layoutInflater = LayoutInflater.from(activity); View layout = layoutInflater.inflate(R.layout.pop, null); inputContent = (EditText) layout.findViewById(R.id.inputCmpny); btnYes = (Button) layout.findViewById(R.id.btn_yes); pop = new PopupWindow(layout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); //点击 back 键的时候,窗口会自动消失 pop.setBackgroundDrawable(new BitmapDrawable()); //让pop覆盖在输入法上面 pop.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED); //让pop自适应输入状态 pop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); pop.setTouchable(true); // 设置popupWindow可点击 pop.setOutsideTouchable(true); // 设置popupWindow外部可点击 pop.setFocusable(true); // 获取焦点 pop.setOnDismissListener(new PopupDismissListener()); pop.setTouchInterceptor(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { //如果点击了popupWindow的外部,popupWindow也会消失 if (event.getAction() == MotionEvent.ACTION_OUTSIDE) { pop.dismiss(); return true; } return false; } }); } /** * 监听确认按钮点击事件 */ public void setOnClickListener(View.OnClickListener listener) { btnYes.setOnClickListener(listener); } /** * 获取输入内容 */ public String getInputContent() { return inputContent.getText().toString(); } public void dismiss() { pop.dismiss(); } /** * 设置添加屏幕的背景透明度 * * @param bgAlpha 透明度 */ public void backgroundAlpha(float bgAlpha) { WindowManager.LayoutParams lp = activity.getWindow().getAttributes(); lp.alpha = bgAlpha; //0.0-1.0 activity.getWindow().setAttributes(lp); } /** * @param isBackground 是否显示半透明背景 * @param paramView 点击的按钮 */ public void show(View paramView, boolean isBackground) { if (isBackground) { backgroundAlpha(0.7f);//CCT.POP_BG_ALPHA } //设置窗口显示位置, 后面两个0 是表示偏移量,可以自由设置 pop.showAtLocation(paramView, Gravity.CENTER, 0, 0); //更新窗口状态 pop.update(); } /** * 添加新笔记时弹出的popWin关闭的事件,主要是为了将背景透明度改回来 */ class PopupDismissListener implements PopupWindow.OnDismissListener { @Override public void onDismiss() { // TODO Auto-generated method stub backgroundAlpha(1f); } } }以上就是封装类的全部源码,快过年了,都没心情敲代码了 (还不放假啊!)