美文网首页
关于输入法遮挡系统Dialog中EditText的问题(无法将E

关于输入法遮挡系统Dialog中EditText的问题(无法将E

作者: 还是做个码农 | 来源:发表于2022-10-03 23:10 被阅读0次

自定义了一个带EditText系统级的Dialog,在Window中show出来,但是当弹出输入法时将输入框挡住了,严重影响用户体验
需要设置Window的层级为WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
同时需要在Dialog的style样式中设置windowSoftInputMode属性

    <style name="InputDialog" parent="Theme.AppCompat.Light.Dialog">
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowSoftInputMode">stateVisible|adjustPan</item>
    </style>

自定义PasswordDialog代码如下:

package com.example.myapplication;


import android.content.Context;

import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.StringRes;


public class PasswordDialog extends BaseDialog {

    private TextView mTvTitle;
    private TextView mTvContent;
    private Button mBtnConfirm;
    private Button mBtnCancel;
    private View mHorizontalLine;
    private View mVerticalLine;
    private FrameLayout mContentLayout;


    public PasswordDialog(Context context) {
        super(context);
    }

    public PasswordDialog(Context context, int theme) {
        super(context, theme);
    }

    @Override
    protected void initManager() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_warn;
    }

    @Override
    protected void initView(View view) {
        mTvTitle = view.findViewById(R.id.tv_dialog_title);
        mTvContent = view.findViewById(R.id.tv_dialog_content);
        mBtnConfirm = view.findViewById(R.id.btn_confirm);
        mBtnCancel = view.findViewById(R.id.btn_cancel);
        mHorizontalLine = view.findViewById(R.id.horizontal_line);
        mVerticalLine = view.findViewById(R.id.vertical_line);
        mContentLayout = view.findViewById(R.id.content_view);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initLayoutParam() {
        super.initLayoutParam();
        Window dialogWindow = this.getWindow();
        WindowManager.LayoutParams lp;
        if (dialogWindow != null) {
            lp = dialogWindow.getAttributes();
            lp.width = 800;
//            lp.height = (int) mContext.getResources().getDimension(R.dimen.large_window_height);
            dialogWindow.setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
            dialogWindow.setGravity(Gravity.CENTER);
        }
    }

    public void setConfirmEnable(boolean enable) {
        mBtnConfirm.setAlpha(enable ? 1f : 0.5f);
        mBtnConfirm.setEnabled(enable);
    }

    public void setCancelEnable(boolean enable) {
        mBtnCancel.setAlpha(enable ? 1f : 0.5f);
        mBtnCancel.setEnabled(enable);
    }

    @Override
    protected void setListener() {

    }

    @Override
    public void updateResource() {

    }

    public static final class Builder {
        private final Context mContext;
        private int mTheme;
        private CharSequence mTitle;
        private CharSequence mContent;
        private View mContentView;
        private CharSequence mPositiveButtonName;
        private CharSequence mNegativeButtonName;
        private OnClickListener mPositiveListener;
        private OnClickListener mNegativeListener;

        public Builder(@NonNull Context context) {
            this(context, 0);
        }

        public Builder(@NonNull Context context, int theme) {
            mContext = context;
            mTheme = theme;
        }

        public Builder setTitle(CharSequence title) {
            mTitle = title;
            return this;
        }

        public Builder setTitle(@StringRes int titleId) {
            mTitle = mContext.getText(titleId);
            return this;
        }

        public Builder setContent(CharSequence content) {
            mContent = content;
            return this;
        }

        public Builder setContentView(View contentView) {
            mContentView = contentView;
            return this;
        }

        public Builder setContentView(@LayoutRes int layoutId) {
            mContentView = View.inflate(mContext, layoutId, null);
            return this;
        }


        public Builder setPositiveButton(CharSequence positiveButtonName, OnClickListener listener) {
            mPositiveButtonName = positiveButtonName;
            mPositiveListener = listener;
            return this;
        }

        public Builder setPositiveButton(@StringRes int positiveButtonNameID, OnClickListener listener) {
            mPositiveButtonName = mContext.getText(positiveButtonNameID);
            mPositiveListener = listener;
            return this;
        }

        public Builder setNegativeButton(CharSequence negativeButtonName, OnClickListener listener) {
            mNegativeButtonName = negativeButtonName;
            mNegativeListener = listener;
            return this;
        }

        public Builder setNegativeButton(@StringRes int negativeButtonName, OnClickListener listener) {
            mNegativeButtonName = mContext.getText(negativeButtonName);
            mNegativeListener = listener;
            return this;
        }

        public PasswordDialog create() {
            final PasswordDialog settingsDialog = new PasswordDialog(mContext, mTheme);
            if (!TextUtils.isEmpty(mTitle)) {
                settingsDialog.mTvTitle.setText(mTitle);
            }
            if (!TextUtils.isEmpty(mContent)) {
                settingsDialog.mTvContent.setText(mContent);
            }
            if (mContentView != null) {
                settingsDialog.mTvContent.setVisibility(View.GONE);
                settingsDialog.mContentLayout.setVisibility(View.VISIBLE);
                settingsDialog.mContentLayout.addView(mContentView);
            }
            if (!TextUtils.isEmpty(mPositiveButtonName)) {
                settingsDialog.mBtnConfirm.setText(mPositiveButtonName);
                settingsDialog.mBtnConfirm.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mPositiveListener != null) {
                            mPositiveListener.onClick(settingsDialog, 0);
                        }
                    }
                });
            } else {
                settingsDialog.mBtnConfirm.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(mNegativeButtonName)) {
                settingsDialog.mBtnCancel.setText(mNegativeButtonName);
                settingsDialog.mBtnCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mNegativeListener != null) {
                            mNegativeListener.onClick(settingsDialog, 0);
                        }
                        settingsDialog.dismiss();
                    }
                });
            } else {
                settingsDialog.mBtnCancel.setVisibility(View.GONE);
            }
            if (TextUtils.isEmpty(mPositiveButtonName) || TextUtils.isEmpty(mNegativeButtonName)) {
                settingsDialog.mVerticalLine.setVisibility(View.GONE);
            } else if (TextUtils.isEmpty(mPositiveButtonName) && TextUtils.isEmpty(mNegativeButtonName)) {
                settingsDialog.mHorizontalLine.setVisibility(View.GONE);
            }
            return settingsDialog;
        }
    }
}

使用时需要传入自定义带EditText的布局。

        View view = View.inflate(this.getApplicationContext(), R.layout.layout_wifi_password, null);
        mEtWifiInputPwd = view.findViewById(R.id.et_wifi_input_pwd);
        PasswordDialog passwordDialog = new PasswordDialog.Builder(this.getApplicationContext(), R.style.InputDialog)
                .setContentView(view)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setNegativeButton("取消", null)
                .create();
        passwordDialog.show();

相关文章

网友评论

      本文标题:关于输入法遮挡系统Dialog中EditText的问题(无法将E

      本文链接:https://www.haomeiwen.com/subject/yditartx.html