【Android】文本输入框弹出的同时软键盘也弹出,不需要点击两次才弹出软键盘window.setSoftInputMode解决

背景

在项目手机中,点击某个view弹出一个包含editText和发送组件的Dialog。
原代码:

public class ChatInputTextDialogFragment extends DialogFragment {
    GoImChatLayoutDialogInputTextBinding binding;
    View.OnClickListener onClickListener;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = GoImChatLayoutDialogInputTextBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        // 进行数据初始化
        binding.tvConfirm.setOnClickListener(onClickListener);
        //限制输入长度为30,无论中英文
        binding.etInput.addTextChangedListener(new TextWatcher() {
            private CharSequence temp;
            private boolean isEdit = true;
            private int selectionStart;
            private int selectionEnd;

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                temp = s;
                if (TextUtils.isEmpty(s)) {
                    binding.tvConfirm.setBackground(getResources().getDrawable(R.mipmap.im_ic_not_send));
                    binding.tvConfirm.setClickable(false);
                }
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                binding.tvConfirm.setBackground(getResources().getDrawable(R.mipmap.im_ic_send));
                binding.tvConfirm.setClickable(true);
            }

            @Override
            public void afterTextChanged(Editable s) {
                selectionStart = binding.etInput.getSelectionStart();
                selectionEnd = binding.etInput.getSelectionEnd();
                if (temp.length() > 30) {
                    s.delete(selectionStart - 1, selectionEnd);
                    binding.etInput.setText(s);
                }
                String message = s.toString().trim();
                if (TextUtils.isEmpty(message)) {
                    binding.tvConfirm.setBackground(getResources().getDrawable(R.mipmap.im_ic_not_send));
                    binding.tvConfirm.setClickable(false);
                } else {
                    binding.tvConfirm.setBackground(getResources().getDrawable(R.mipmap.im_ic_send));
                    binding.tvConfirm.setClickable(true);
                }

            }
        });
        initBottomWindow();
    }

    private void initBottomWindow() {
        // 对该Dialog 的布局优化
        Window window = getDialog().getWindow();
        window.setBackgroundDrawable(null);
        window.getAttributes().gravity = Gravity.BOTTOM;
        window.getAttributes().height = ViewGroup.LayoutParams.WRAP_CONTENT;
        window.getAttributes().width = ViewGroup.LayoutParams.MATCH_PARENT;
        window.setDimAmount(0f); //背景蒙层不需要

        // 键盘也跟着弹出来
        getView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                binding.etInput.requestFocus();
               
                InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                inputMethodManager.showSoftInput(binding.etInput, InputMethodManager.SHOW_IMPLICIT);
            }
        });
    }

    private void setInputText(String textString) {
        binding.etInput.setText(textString);
    }

    //构造器模式
    public static class Builder {
        private ChatInputTextDialogFragment dialog = new ChatInputTextDialogFragment();

        public void showNow(FragmentManager fragmentManager) {
            dialog.showNow(fragmentManager, "input");
        }

        public Builder setSendClickListener(View.OnClickListener onClickListener) {
            dialog.onClickListener = onClickListener;
            return this;
        }

        public void emptyEnterMessage() {
            dialog.binding.etInput.setText("");
        }

        public String getInputText() {
            return dialog.binding.etInput.getText().toString();
        }

        public void dismiss() {
            dialog.dismiss();
        }
    }

}

问题解决

window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值