欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android popupwidown 实现朋友圈评论弹窗显示在软键盘上面

程序员文章站 2022-08-12 15:51:06
布局:

布局:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:background="@color/color_f4f4f5">
    <EditText
        android:id="@+id/popup_circle_comment_edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="?text_size_normal"
        android:layout_marginTop="8dp"
        android:clickable="true"
        android:enabled="true"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="10dp"
        android:textColor="@color/color_303133"
        android:padding="6dp"
        android:layout_toStartOf="@+id/popup_circle_comment_send"
        android:background="@drawable/shape_radius5_white"/>
    <TextView
        android:id="@+id/popup_circle_comment_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/color_606266"
        android:textSize="?text_size_normal"
        android:text="发送"
        android:paddingTop="6dp"
        android:paddingBottom="6dp"
        android:paddingStart="10dp"
        android:paddingEnd="10dp"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_centerVertical="true"
        android:background="@drawable/edit_layout_bg"/>
</RelativeLayout>

初始化popupwidown 

PopupWindow commentPopup = new PopupWindow(popupBinding.getRoot(), ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
commentPopup.setTouchable(true);
commentPopup.setFocusable(true);
commentPopup.setOutsideTouchable(true);//点击弹窗外弹窗消失
commentPopup.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);//显示在软键盘上面配置
commentPopup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);//显示在软键盘上面配置
commentPopup.showAtLocation(popupBinding.getRoot(), Gravity.BOTTOM, 0, 0);
showSoft();
/**
 * 弹出输入法窗口
 */
private void showSoft() {
    Handler handle = new Handler();
    handle.postDelayed(new Runnable() {

        @Override
        public void run() {
            //设置可获得焦点
            popupCircleCommentEdit.setFocusable(true); //popupCircleCommentEdit
            popupCircleCommentEdit.setFocusableInTouchMode(true);
            //请求获得焦点
            popupCircleCommentEdit.requestFocus();
            InputMethodManager inputMethodManager = (InputMethodManager)    
            popupCircleCommentEdit.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.showSoftInput(popupCircleCommentEdit, 0);//弹出时强制让输入框popupCircleCommentEdit获取焦点
        }
    }, 200);//延迟弹出
}

本文地址:https://blog.csdn.net/wang670144941/article/details/107932586