背景
在项目手机中,点击某个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);