效果图

Android使用Dialog实现贴键盘输入框(类似微博评论)-LMLPHP

	android:windowSoftInputMode="stateAlwaysHidden|adjustPan"
	public class InputDialogUtils extends Dialog {

    private Context mContext;//上下文对象

    private EditText editText;//输入框对象

    private InputDialogListener listener;//监听回调

    public InputDialogUtils(@NonNull Context context) {
        super(context);
        this.mContext = context;
        initDialog();
    }

    public InputDialogUtils(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.mContext = context;
        initDialog();
    }

    protected InputDialogUtils(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mContext = context;
        initDialog();
    }

    private void initDialog() {
        View view = LinearLayout.inflate(mContext, R.layout.pop_view_session_input, null);
        setContentView(view);

        //输入框
        editText = view.findViewById(R.id.sessionInputEd);

        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
        //这里的动画效果自己创建
        window.setWindowAnimations(R.style.BottomDialog_Animation);

        //添加发送点击事件
        view.findViewById(R.id.sessionSendTxt).setOnClickListener(new ClickUtils.OnDebouncingClickListener(Config.IS_GLOBAL_CLICK, Config.MIN_CLICK_DELAY_TIME) {
            @Override
            public void onDebouncingClick(View v) {
                if (listener != null) {
                    listener.getInputTxt(editText);
                    editText.setText("");
                }
            }
        });

        //添加放大点击事件
        view.findViewById(R.id.sessionInputBigImg).setOnClickListener(new ClickUtils.OnDebouncingClickListener(Config.IS_GLOBAL_CLICK, Config.MIN_CLICK_DELAY_TIME) {
            @Override
            public void onDebouncingClick(View v) {
                listener.onCancel(editText);
            }
        });

        setCancelable(true);
        show();
		//保证自定义得布局文件充满屏幕宽度
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = (int) (ScreenUtils.getScreenWidth()); //设置宽度
        getWindow().setAttributes(lp);
        getWindow().setAttributes(layoutParams);
    }
 }
@Override
    public void dismiss() {
        KeyboardUtils.hideSoftInput(editText);
        //与键盘解绑
        editText.setFocusable(false);
        editText.setShowSoftInputOnFocus(false);
        editText.setFocusableInTouchMode(false);
        editText.requestFocus();
        super.dismiss();
    }

    @Override
    public void show() {
        //显示键盘
        KeyboardUtils.showSoftInput();
        //与键盘绑定
        editText.setFocusable(true);
        editText.setShowSoftInputOnFocus(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        super.show();
    }
public static void hideSoftInput(@NonNull final View view) {
        InputMethodManager imm =
                (InputMethodManager) Utils.getApp().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm == null) return;
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
public static void showSoftInput() {
        InputMethodManager imm = (InputMethodManager) Utils.getApp().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm == null) {
            return;
        }
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    }
<!-- dialog有遮罩 -->
    <style name="BottomDialog2" parent="@android:style/Theme.Holo.Dialog">
        <item name="android:windowFrame">@null</item><!--边框-->
        <item name="android:windowIsFloating">true</item><!--是否浮现在activity之上-->
        <item name="android:windowIsTranslucent">true</item><!--半透明-->
        <item name="android:windowNoTitle">true</item><!--无标题-->
        <item name="android:windowBackground">@color/colorTransparent</item><!--背景透明-->
        <item name="android:backgroundDimEnabled">true</item><!--模糊-->
    </style>
	if (inputDialogUtils == null) {
                        inputDialogUtils = new InputDialogUtils(this, R.style.BottomDialog2);
                    } else {
                        inputDialogUtils.show();
                    }
                    inputDialogUtils.setListener(new InputDialogUtils.InputDialogListener() {
                        @Override
                        public void onConfirm(int type) {

                        }

                        @Override
                        public void onCancel(EditText editText) {
                            inputDialogUtils.cancel();
                        }

                        @Override
                        public void getInputTxt(EditText editText) {
                            ToastUtils.showShort(editText.getText().toString());
                            inputDialogUtils.dismiss();
                        }
                    });
09-03 03:34