xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/layout_chouti_bar" />
<include layout="@layout/layout_common_dialog_bar" />
<androidx.core.widget.NestedScrollView
android:id="@+id/nsv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/dialog_bg_color">
<LinearLayout
android:id="@+id/ll_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<View style="@style/line_match_b2" />
<TextView android:layout_width="match_parent"
android:layout_height="700dp"
android:text="@string/app_name"
android:gravity="center"
android:textColor="@color/white"
android:textSize="30sp"
android:background="@color/red"/>
<EditText
android:id="@+id/et_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:hint="@string/app_name"
/>
<!--底部用来增加软键盘的高度-->
<View
android:id="@+id/view_bottom_soft"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</LinearLayout>
弹窗代码如下:
class TestEditorDialog(private val activity: BaseActivity) {
private lateinit var mBottomSheetDialog: SupportBottomSheetDialog
private lateinit var mContentNsv:NestedScrollView
private lateinit var mContentLl:LinearLayout
private lateinit var mBackLl:LinearLayout
private lateinit var mTitleTv: TextView
private lateinit var mContentEt:EditText
private lateinit var mBottomSoftView:View
init {
init()
}
private fun init() {
val view = LayoutInflater.from(activity).inflate(R.layout.dialog_common_editor, null)
mTitleTv = view.findViewById(R.id.tv_title)
mBackLl = view.findViewById(R.id.ll_back)
mBottomSoftView = view.findViewById(R.id.view_bottom_soft)
mContentNsv = view.findViewById(R.id.nsv_content)
mContentLl = view.findViewById(R.id.ll_content)
mContentEt = view.findViewById(R.id.et_content)
mBackLl.setOnClickListener { mBottomSheetDialog.dismiss() }
mBottomSheetDialog = SupportBottomSheetDialog(activity, view)
//监控键盘
SoftKeyBoardListener.setListener(activity,
object : SoftKeyBoardListener.OnSoftKeyBoardChangeListener {
override fun keyBoardShow(height: Int, visibleHeight: Int) {//键盘处于打开状态
//mBottomSoftView 底部用来增加软键盘的高度
mBottomSoftView.visibility = View.VISIBLE
mBottomSoftView.layoutParams.height = height
mBottomSoftView.requestLayout()
mBottomSoftView.post {
//mContentNsv.scrollTo(0,mContentNsv.scrollY + height)
/**
* 输入框在底部的输入框在底部的位置
* 内容的总高度 - 软键盘的高度
*/
mContentNsv.scrollBy(0,mContentLl.measuredHeight - height)
}
}
override fun keyBoardHide(height: Int) {//键盘处于关闭状态
mBottomSoftView.visibility = View.GONE
}
})
}
fun dismiss(){
mBottomSheetDialog?.dismiss()
}
fun show() {
mTitleTv.text = "Title"
mBackLl.visibility = View.GONE
mBottomSheetDialog.show()
activity.window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
}
}
软件盘监听类别:
/**
* Created by yanshirong on 2020-06-02.
* E-Mail:yanshirong@shanda.com
* Description:
* 判断软键盘是否弹出
* 1.activity-->android:windowSoftInputMode="adjustResize|stateHidden"
* 2.如果高版本出现输入框焦点问题,可由listView改为recycleView
* 3.监听注册监听,同时需要取消监听本listener
*/
//EditText弹出软键盘监听
class SoftKeyBoardListener(activity: Activity) {
private val rootView: View//activity的根视图
internal var rootViewVisibleHeight: Int = 0//纪录根视图的显示高度
private var onSoftKeyBoardChangeListener: OnSoftKeyBoardChangeListener? = null
init {
//获取activity的根视图
rootView = activity.window.decorView
//监听视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变
rootView.viewTreeObserver.addOnGlobalLayoutListener {
//获取当前根视图在屏幕上显示的大小
val r = Rect()
rootView.getWindowVisibleDisplayFrame(r)
val visibleHeight = r.height()
if (rootViewVisibleHeight == 0) {
rootViewVisibleHeight = visibleHeight
return@addOnGlobalLayoutListener
}
//根视图显示高度没有变化,可以看作软键盘显示/隐藏状态没有改变
if (rootViewVisibleHeight == visibleHeight) {
return@addOnGlobalLayoutListener
}
//根视图显示高度变小超过200,可以看作软键盘显示了
if (rootViewVisibleHeight - visibleHeight > 200) {
if (onSoftKeyBoardChangeListener != null) {
Log.d("KeyboardUtiNew","keyBoardShow rootViewVisibleHeight:$rootViewVisibleHeight,visibleHeight:$visibleHeight")
onSoftKeyBoardChangeListener!!.keyBoardShow(rootViewVisibleHeight - visibleHeight,visibleHeight)
}
rootViewVisibleHeight = visibleHeight
return@addOnGlobalLayoutListener
}
//根视图显示高度变大超过200,可以看作软键盘隐藏了
if (visibleHeight - rootViewVisibleHeight > 200) {
if (onSoftKeyBoardChangeListener != null) {
Log.d("KeyboardUtiNew","keyBoardHide rootViewVisibleHeight:$rootViewVisibleHeight,visibleHeight:$visibleHeight")
onSoftKeyBoardChangeListener!!.keyBoardHide(visibleHeight - rootViewVisibleHeight)
}
rootViewVisibleHeight = visibleHeight
return@addOnGlobalLayoutListener
}
}
}
private fun setOnSoftKeyBoardChangeListener(onSoftKeyBoardChangeListener: OnSoftKeyBoardChangeListener) {
this.onSoftKeyBoardChangeListener = onSoftKeyBoardChangeListener
}
interface OnSoftKeyBoardChangeListener {
fun keyBoardShow(height: Int, visibleHeight: Int)
fun keyBoardHide(height: Int)
}
companion object {
fun setListener(
activity: Activity,
onSoftKeyBoardChangeListener: OnSoftKeyBoardChangeListener
) {
val softKeyBoardListener = SoftKeyBoardListener(activity)
softKeyBoardListener.setOnSoftKeyBoardChangeListener(onSoftKeyBoardChangeListener)
}
/**
* 隐藏软键盘(只适用于Activity,不适用于Fragment)
*/
fun hideSoftKeyboard(activity: Activity) {
val view = activity.currentFocus
if (view != null) {
val inputMethodManager =
activity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager!!.hideSoftInputFromWindow(
view.windowToken,
InputMethodManager.HIDE_NOT_ALWAYS
)
}
}
fun showSoftKeyBoard(view: View) {
val inputMethodManager =
view.context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
if (inputMethodManager != null) {
view.requestFocus()
inputMethodManager.showSoftInput(view, 0)
}
}
}
}
调用方式如下:
TestEditorDialog(this).show()
网友评论