美文网首页
表情、输入布局辅助View

表情、输入布局辅助View

作者: Android小豆渣 | 来源:发表于2022-02-20 21:51 被阅读0次

    解决聊天布局 输入框、键盘弹起、表情布局、其他布局的交互布局变化问题

    
    public class InputConflictViewextends LinearLayout {
    
    private static final StringSHARE_NAME ="share_name";
    
        private static final StringCOLUMN_NAME ="column_name";
    
        private InputMethodManagerimm;
    
        private ContextmContext;
    
        private EditTextmEditText;//输入框
    
        private ViewmListView;//内容列表
    
        private ViewmPanelView, mPhotoView;//跟输入法切换的面板
    
        private ImageButtonmSwitchBtn, mPhotoBtn;//切换表情和键盘的按钮
    
        private int mEditTextId;
    
        private int mListViewId;
    
        private int mPanelViewId;
    
        private int mSwitchBtnId;
    
        private int mPhotoViewId;
    
        private int mPhotoBtnId;
    
        private boolean mIsKeyboardActive =false; //输入法是否激活
    
        public InputConflictView(Context context) {
    
    super(context);
    
            this.mContext = context;
    
            init(null);
    
        }
    
    public InputConflictView(Context context, AttributeSet attrs) {
    
    super(context, attrs);
    
            this.mContext = context;
    
            init(attrs);
    
        }
    
    public InputConflictView(Context context, AttributeSet attrs, int defStyleAttr) {
    
    super(context, attrs, defStyleAttr);
    
            this.mContext = context;
    
            init(attrs);
    
        }
    
    private void init(AttributeSet attrs) {
    
    //监听布局变化
    
            getViewTreeObserver().addOnGlobalLayoutListener(new KeyboardOnGlobalChangeListener());
    
            imm = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
    
            TypedArray custom = getContext().obtainStyledAttributes(attrs,
    
                    R.styleable.InputConflictView);
    
            mEditTextId = custom.getResourceId(
    
    R.styleable.InputConflictView_editText, NO_ID);
    
            mListViewId = custom.getResourceId(
    
    R.styleable.InputConflictView_listView, NO_ID);
    
            mPanelViewId = custom.getResourceId(R.styleable.InputConflictView_panelView,
    
                    NO_ID);
    
            mSwitchBtnId = custom.getResourceId(R.styleable.InputConflictView_switchBtn,
    
                    NO_ID);
    
            mPhotoViewId = custom.getResourceId(R.styleable.InputConflictView_photoView,
    
                    NO_ID);
    
            mPhotoBtnId = custom.getResourceId(R.styleable.InputConflictView_photoBtn,
    
                    NO_ID);
    
            custom.recycle();
    
        }
    
    @Override
    
        protected void onFinishInflate() {
    
    super.onFinishInflate();
    
            initView();
    
            initListener();
    
        }
    
    private void initView() {
    
    if (mEditTextId !=NO_ID) {
    
    mEditText = (EditText)this.findViewById(mEditTextId);
    
            }
    
    if (mListViewId !=NO_ID) {
    
    mListView = findViewById(mListViewId);
    
            }
    
    if (mPanelViewId !=NO_ID) {
    
    mPanelView = findViewById(mPanelViewId);
    
                int keyboardHeight = SPUtils.getInt(mContext, COLUMN_NAME);
    
                if (keyboardHeight >0) {
    
    ViewGroup.LayoutParams params =mPanelView.getLayoutParams();
    
                    params.height = keyboardHeight;
    
                    mPanelView.setLayoutParams(params);
    
                }
    
    }
    
    if (mSwitchBtnId !=NO_ID) {
    
    mSwitchBtn = (ImageButton) findViewById(mSwitchBtnId);
    
            }
    
    if (mPhotoBtnId !=NO_ID) {
    
    mPhotoBtn = findViewById(mPhotoBtnId);
    
            }
    
    if (mPhotoViewId !=NO_ID) {
    
    mPhotoView = findViewById(mPhotoViewId);
    
                int keyboardHeight = SPUtils.getInt(mContext, COLUMN_NAME);
    
                if (keyboardHeight >0) {
    
    ViewGroup.LayoutParams params =mPhotoView.getLayoutParams();
    
                    params.height = keyboardHeight;
    
                    mPhotoView.setLayoutParams(params);
    
                }
    
    }
    
    }
    
    private void initListener() {
    
    //        mListView.setOnTouchListener(new OnTouchListener() {
    
    //                                        @Override
    
    //                                        public boolean onTouch(View v, MotionEvent event) {
    
    //                                            hideKeyboard();
    
    //                                            mPanelView.setVisibility(View.GONE);
    
    ////                                            mSwitchBtn.setBackgroundResource(R.drawable.icon_more);
    
    //                                            return false;
    
    //                                        }
    
    //                                    }
    
    //        );
    
            mSwitchBtn.setOnClickListener(
    
    new OnClickListener() {
    
    @Override
    
                        public void onClick(View v) {
    
    mSwitchBtn.setSelected(!mSwitchBtn.isSelected());
    
                            mPhotoBtn.setSelected(!mSwitchBtn.isSelected());
    
                            if (mIsKeyboardActive) {// 输入法打开状态下
    
                                if (mSwitchBtn.isSelected()) {// 打开表情
    
    //                                mSwitchBtn.setBackgroundResource(R.drawable.icon_key);
    
                                    // 设置为不会调整大小,以便输入弹起时布局不会改变。若不设置此属性,输入法弹起时布局会闪一下
    
                                    ((Activity)mContext).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
    
                                    InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    
                                    imm.hideSoftInputFromWindow(mEditText.getApplicationWindowToken(), 0);
    
                                    mPanelView.setVisibility(View.VISIBLE);
    
                                    mPhotoView.setVisibility(View.GONE);
    
                                }else {
    
    //                                mSwitchBtn.setBackgroundResource(R.drawable.icon_more);
    
                                    mPanelView.setVisibility(View.GONE);
    
                                    InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    
                                    imm.hideSoftInputFromWindow(mEditText.getApplicationWindowToken(), 0);
    
                                    ((Activity)mContext).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
    
                                            | WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    
                                }
    
    }else {
    
    //  输入法关闭状态下
    
                                if (mSwitchBtn.isSelected()) {
    
    //                                mSwitchBtn.setBackgroundResource(R.drawable.icon_key);
    
                                    // 设置为不会调整大小,以便输入弹起时布局不会改变。若不设置此属性,输入法弹起时布局会闪一下
    
                                    ((Activity)mContext).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
    
                                    mPanelView.setVisibility(View.VISIBLE);
    
                                    mPhotoView.setVisibility(View.GONE);
    
                                }else {
    
    //                                mSwitchBtn.setBackgroundResource(R.drawable.icon_more);
    
                                    imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);
    
                                    mSwitchBtn.postDelayed(new Runnable() {
    
    @Override
    
                                        public void run() {// 输入法弹出之后,重新调整
    
                                            mPanelView.setVisibility(View.GONE);
    
                                            ((Activity)mContext).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
    
                                                    | WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    
                                        }
    
    }, 250); // 延迟一段时间,等待输入法完全弹出*/
    
                                }
    
    }
    
    }
    
    }
    
    );
    
            mPhotoBtn.setOnClickListener(new OnClickListener() {
    
    @Override
    
                public void onClick(View v) {
    
    mPhotoBtn.setSelected(!mPhotoBtn.isSelected());
    
                    mSwitchBtn.setSelected(!mPhotoBtn.isSelected());
    
                    if (mIsKeyboardActive) {// 输入法打开状态下
    
                        if (mPhotoBtn.isSelected()) {// 打开表情
    
                            // 设置为不会调整大小,以便输入弹起时布局不会改变。若不设置此属性,输入法弹起时布局会闪一下
    
                            ((Activity)mContext).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
    
                            InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    
                            imm.hideSoftInputFromWindow(mEditText.getApplicationWindowToken(), 0);
    
                            mPhotoView.setVisibility(View.VISIBLE);
    
                            mPanelView.setVisibility(View.GONE);
    
                        }else {
    
    mPhotoView.setVisibility(View.GONE);
    
                            InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    
                            imm.hideSoftInputFromWindow(mEditText.getApplicationWindowToken(), 0);
    
                            ((Activity)mContext).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
    
                                    | WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    
                        }
    
    }else {
    
    //  输入法关闭状态下
    
                        if (mPhotoBtn.isSelected()) {
    
    // 设置为不会调整大小,以便输入弹起时布局不会改变。若不设置此属性,输入法弹起时布局会闪一下
    
                            ((Activity)mContext).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
    
                            mPhotoView.setVisibility(View.VISIBLE);
    
                            mPanelView.setVisibility(View.GONE);
    
                        }else {
    
    imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);
    
                            mPhotoBtn.postDelayed(new Runnable() {
    
    @Override
    
                                public void run() {// 输入法弹出之后,重新调整
    
                                    mPhotoView.setVisibility(View.GONE);
    
                                    ((Activity)mContext).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
    
                                            | WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    
                                }
    
    }, 250); // 延迟一段时间,等待输入法完全弹出*/
    
                        }
    
    }
    
    }
    
    });
    
            mEditText.setOnClickListener(new OnClickListener() {
    
    @Override
    
                                            public void onClick(View v) {
    
    mEditText.setCursorVisible(true);
    
    //                                            mSwitchBtn.setBackgroundResource(R.drawable.icon_more);
    
                                                mEditText.postDelayed(new Runnable() {
    
    @Override
    
                                                    public void run() {
    
    mSwitchBtn.setSelected(false);
    
                                                        mPhotoBtn.setSelected(false);
    
                                                        mPanelView.setVisibility(View.GONE);
    
                                                        mPhotoView.setVisibility(View.GONE);
    
                                                        // 输入法弹出之后,重新调整
    
                                                        ((Activity)mContext).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
    
                                                                | WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    
                                                    }
    
    }, 250); // 延迟一段时间,等待输入法完全弹出
    
                                            }
    
    }
    
    );
    
        }
    
    private class KeyboardOnGlobalChangeListenerimplements ViewTreeObserver.OnGlobalLayoutListener {
    
    int mScreenHeight =0;
    
            RectmRect =new Rect();
    
            private int getScreenHeight() {
    
    if (mScreenHeight >0) {
    
    return mScreenHeight;
    
                }
    
    mScreenHeight = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE))
    
    .getDefaultDisplay().getHeight();
    
                return mScreenHeight;
    
            }
    
    @Override
    
            public void onGlobalLayout() {
    
    // 获取当前页面窗口的显示范围
    
                getWindowVisibleDisplayFrame(mRect);
    
                int screenHeight = getScreenHeight();
    
                int keyboardHeight = screenHeight -mRect.bottom; // 输入法的高度
    
                boolean isActive =false;
    
                if (Math.abs(keyboardHeight) > screenHeight /5) {
    
    isActive =true; // 超过屏幕五分之一则表示弹出了输入法
    
                }
    
    mIsKeyboardActive = isActive;
    
                onKeyboardStateChanged(mIsKeyboardActive, keyboardHeight);
    
            }
    
    }
    
    public void hideKeyboard() {
    
    InputMethodManager imm = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
    
            imm.hideSoftInputFromWindow(mEditText.getApplicationWindowToken(), 0);
    
        }
    
    private void onKeyboardStateChanged(boolean isActive, int keyboardHeight) {
    
    if (isActive) {
    
    mSwitchBtn.setFocusable(false);
    
                mSwitchBtn.setFocusableInTouchMode(false);
    
                mEditText.requestFocus();
    
                if (mSwitchBtn.isSelected()) {// 表情打开状态下
    
                    mPanelView.setVisibility(View.GONE);
    
                    mSwitchBtn.setSelected(false);
    
                }
    
    SPUtils.saveInt(mContext, COLUMN_NAME, keyboardHeight);
    
                ViewGroup.LayoutParams params =mPanelView.getLayoutParams();
    
                if (!(params.height == keyboardHeight)) {
    
    params.height = keyboardHeight;
    
                    mPanelView.setLayoutParams(params);
    
                }
    
    }else {
    
    if (mSwitchBtn.isSelected()) {
    
    return;
    
                }
    
    }
    
    }
    
    private StringfileName ="cacheFile";
    
    }
    
    
    image.png

    相关文章

      网友评论

          本文标题:表情、输入布局辅助View

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