项目中刚开始使用的是popupWindow弹出输入框同时弹出输入法 ,使用起来也非常方便,然而测试人员发现弹出的输入框竟然无法复制粘贴,这对喜欢使用cv大法的我来说是无法忍受的。仔细查看了一下,原来popupwindow中的输入框是一直不支持复制粘贴的,据说是Android的bug。呃呃呃,好吧。只能换了。于是dialogFragment登场。。。
具体的写法和普通的dialogFragment没什么区别,于是找找旧代码使用cv大法,然后改成原来popupwindow的布局。其他的都不用动。写完收工。自己随手测了一下,输入框弹出很完美,但是这个输入法是怎么回事不能主动弹出来,这是不给面子啊。然后。。。我一通猛如虎的操作,然而并没有什么用。这这这这这。。。我忽然间想起来这可能是焦点的原因,于是我手动在edittext外的布局中设置
android:focusable="false"
android:focusableInTouchMode="false"
然后在代码中设置
mEditText.setFocusable(true);
mEditText.setFocusableInTouchMode(true);
mEditText.requestFocus();
嗯,似乎有点用,但是并没有达到想要的效果。于是再次操作
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
这下好用了。非常的丝滑。
网上也有些使用的是
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
然而这个方法对我的项目来说并没有用,有兴趣的小伙伴可以试试。
还有一种方法,据说是把输入法的弹出放在一个延迟的线程中,这个操作太骚了,我还是喜欢清淡一点的口味。有喜欢的小伙伴也可以试下。
具体完整代码如下:
public class InputDialogFragment extends BaseDialogFragment<BookdetailLayoutEditViewBookDetailBinding, NoViewModel> implements View.OnClickListener {
OnPublishClickListener onPublishClickListener;
@Override
public int initVariableId() {
return 0;
}
@Override
protected int getLayoutId() {
return R.layout.bookdetail_layout_edit_view_book_detail;
}
@Override
public void setUpWindow() {
if (getDialog() == null) {
return;
}
Window window = getDialog().getWindow();
if (window == null) {
return;
}
window.setBackgroundDrawableResource(android.R.color.transparent);
WindowManager.LayoutParams params = window.getAttributes();
params.gravity = Gravity.BOTTOM;
params.width = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(params);
window.setWindowAnimations(R.style.bookdetail_share_dialog_bottom_anim);
}
@Override
public void initView() {
super.initView();
UIUtils.setTypeface(mBinding.tvTitle).setIncludeFontPadding(false);
EditText mEditText = mBinding.customEditText.findViewById(R.id.review_input);
mBinding.tvPublish.setOnClickListener(this);
mBinding.tvPublish.setEnabled(false);
mBinding.cancelEdit.setOnClickListener(this);
mBinding.tvRatingDesc.setText(RatingBarLevel.getRatingLevelText((int) mBinding.ratingBar.getRating()));
mBinding.ratingBar.setOnRatingBarChangeListener((ratingBar, rating, fromUser) -> {
mBinding.tvRatingDesc.setText(RatingBarLevel.getRatingLevelText((int) rating));
});
mBinding.customEditText.setOnTextChangedListener(s -> mBinding.tvPublish.setEnabled(!TextUtils.isEmpty(s)));
mEditText.setFocusable(true);
mEditText.setFocusableInTouchMode(true);
mEditText.requestFocus();
if (getDialog() != null && getDialog().getWindow() != null) {
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.cancel_edit) {
this.dismiss();
} else if (v.getId() == R.id.tv_publish) {
publish();
}
}
private void publish() {
String comment = mBinding.customEditText.getEditTextContent();
float rating = mBinding.ratingBar.getRating();
if (TextUtils.isEmpty(comment)) {
ToastUtils.getInstance().showCustomToast("内容不能为空");
return;
}
if (rating == 0) {
ToastUtils.getInstance().showCustomToast("请给这本书评分");
return;
}
if (onPublishClickListener != null) {
onPublishClickListener.onPublishClick(comment, rating);
}
}
@Override
public void onDismiss(@NonNull DialogInterface dialog) {
super.onDismiss(dialog);
mBinding.customEditText.clearEditTextContent();
mBinding.ratingBar.setRating(0);
}
public void setOnPublishClickListener(OnPublishClickListener onPublishClickListener) {
this.onPublishClickListener = onPublishClickListener;
}
public interface OnPublishClickListener {
void onPublishClick(String comment, float rating);
}
@Override
public void onDestroy() {
super.onDestroy();
if (onPublishClickListener != null) {
onPublishClickListener = null;
}
}
public void showDialog(FragmentManager supportFragmentManager, String tag) {
//避免重复添加的异常 java.lang.IllegalStateException: Fragment already added
Fragment fragment = supportFragmentManager.findFragmentByTag(tag);
if (fragment != null) {
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.remove(fragment);
fragmentTransaction.commitAllowingStateLoss();
}
//避免状态丢失的异常 java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
try {
super.show(supportFragmentManager, tag);
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
}