弹窗软键盘自适应
1、引入第三方库
implementation 'io.github.razerdp:BasePopup:3.2.0'
2、代码
class EditContentPopup : BasePopupWindow {
constructor(activity: Activity) : super(activity)
constructor(fragment: Fragment) : super(fragment)
private var mBinding: DialogEditContentBinding? = null
override fun onViewCreated(contentView: View) {
super.onViewCreated(contentView)
mBinding = DataBindingUtil.bind(contentView)
mBinding?.apply {
setAutoShowKeyboard(etContent, true)
setShowKeyboardDelay(150)
setOnKeyboardChangeListener { r, b ->
val layoutParams = btnSave.layoutParams as ConstraintLayout.LayoutParams
layoutParams.setMargins(0, 0, 0, r.height() + 12.dp)
btnSave.layoutParams = layoutParams
}
ivClose.setOnClickListener { dismiss() }
}
}
fun show(
code: String,
id: Int,
title: String,
content: String
) {
showPopupWindow()
}
init {
setContentView(R.layout.dialog_edit_content)
//状态栏不遮挡
setOverlayStatusbar(false)
}
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/cst_whole"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:background="@color/white">
<View
android:id="@+id/view_topbg"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/shape_f5_fb_top_radius8"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/ivClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/dp_10"
android:padding="@dimen/dp_4"
android:scaleType="center"
android:src="@drawable/icon_toolbar_close"
app:layout_constraintBottom_toBottomOf="@+id/view_topbg"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/view_topbg" />
<LinearLayout
android:id="@+id/ll_bottom"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="@id/btnSave"
app:layout_constraintTop_toBottomOf="@id/view_topbg">
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="20dp"
android:text="标题"
android:textColor="@color/font_222222"
android:textSize="14sp" />
<RelativeLayout
android:id="@+id/rl_editext"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="@+id/etContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="16dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="16dp"
android:background="@drawable/shape_f5f5f5_radius4"
android:hint="请输入内容"
android:maxHeight="80dp"
android:minHeight="50dp"
android:paddingStart="10dp"
android:paddingTop="@dimen/dp_4"
android:paddingRight="20dp"
android:paddingBottom="@dimen/dp_4"
android:textColor="@color/font_222222"
android:textColorHint="@color/font_999999"
android:textCursorDrawable="@drawable/color_cursor"
android:textSize="16sp" />
<ImageView
android:id="@+id/iv_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginEnd="20dp"
android:padding="5dp"
android:src="@drawable/ic_edit_clear"
android:visibility="gone" />
</RelativeLayout>
</LinearLayout>
<TextView
android:id="@+id/btnSave"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="16dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="12dp"
android:background="@drawable/shape_5477f7_radius8"
android:gravity="center"
android:text="保存"
android:textColor="#ffffffff"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>