feat: 优化

pull/219/head
kunfei 5 years ago
parent 1415573eb1
commit 76dec07358
  1. 75
      app/src/main/java/io/legado/app/ui/replacerule/edit/ReplaceEditDialog.kt

@ -1,10 +1,13 @@
package io.legado.app.ui.replacerule.edit package io.legado.app.ui.replacerule.edit
import android.graphics.Rect
import android.os.Bundle import android.os.Bundle
import android.util.DisplayMetrics import android.view.LayoutInflater
import android.view.* import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
import android.widget.EditText
import android.widget.PopupWindow import android.widget.PopupWindow
import androidx.appcompat.widget.Toolbar import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
@ -19,8 +22,7 @@ import io.legado.app.utils.applyTint
import io.legado.app.utils.getViewModel import io.legado.app.utils.getViewModel
import io.legado.app.utils.toast import io.legado.app.utils.toast
import kotlinx.android.synthetic.main.dialog_replace_edit.* import kotlinx.android.synthetic.main.dialog_replace_edit.*
import org.jetbrains.anko.displayMetrics import org.jetbrains.anko.sdk27.listeners.onFocusChange
import kotlin.math.abs
class ReplaceEditDialog : DialogFragment(), class ReplaceEditDialog : DialogFragment(),
Toolbar.OnMenuItemClickListener, Toolbar.OnMenuItemClickListener,
@ -45,14 +47,11 @@ class ReplaceEditDialog : DialogFragment(),
} }
private lateinit var viewModel: ReplaceEditViewModel private lateinit var viewModel: ReplaceEditViewModel
private var mSoftKeyboardTool: PopupWindow? = null private lateinit var mSoftKeyboardTool: PopupWindow
private var mIsSoftKeyBoardShowing = false
override fun onStart() { override fun onStart() {
super.onStart() super.onStart()
val dm = DisplayMetrics() dialog?.window?.setLayout(MATCH_PARENT, WRAP_CONTENT)
activity?.windowManager?.defaultDisplay?.getMetrics(dm)
dialog?.window?.setLayout((dm.widthPixels * 0.9).toInt(), WRAP_CONTENT)
} }
override fun onCreateView( override fun onCreateView(
@ -67,7 +66,6 @@ class ReplaceEditDialog : DialogFragment(),
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
mSoftKeyboardTool = KeyboardToolPop(requireContext(), AppConst.keyboardToolChars, this) mSoftKeyboardTool = KeyboardToolPop(requireContext(), AppConst.keyboardToolChars, this)
ll_content.viewTreeObserver.addOnGlobalLayoutListener(KeyboardOnGlobalChangeListener())
tool_bar.inflateMenu(R.menu.replace_edit) tool_bar.inflateMenu(R.menu.replace_edit)
tool_bar.menu.applyTint(requireContext(), Theme.getTheme()) tool_bar.menu.applyTint(requireContext(), Theme.getTheme())
tool_bar.setOnMenuItemClickListener(this) tool_bar.setOnMenuItemClickListener(this)
@ -77,6 +75,14 @@ class ReplaceEditDialog : DialogFragment(),
arguments?.let { arguments?.let {
viewModel.initData(it) viewModel.initData(it)
} }
et_replace_rule.onFocusChange { v, hasFocus ->
if (hasFocus) {
mSoftKeyboardTool.width = et_replace_rule.width
mSoftKeyboardTool.showAsDropDown(v)
} else {
mSoftKeyboardTool.dismiss()
}
}
} }
override fun onMenuItemClick(item: MenuItem?): Boolean { override fun onMenuItemClick(item: MenuItem?): Boolean {
@ -119,42 +125,27 @@ class ReplaceEditDialog : DialogFragment(),
val callBack get() = activity as? CallBack val callBack get() = activity as? CallBack
override fun sendText(text: String) { private fun insertText(text: String) {
if (text.isBlank()) return
} val view = dialog?.window?.decorView?.findFocus()
if (view is EditText) {
private fun showKeyboardTopPopupWindow() { val start = view.selectionStart
mSoftKeyboardTool?.let { val end = view.selectionEnd
if (it.isShowing) return val edit = view.editableText//获取EditText的文字
view?.let { view -> if (start < 0 || start >= edit.length) {
it.showAtLocation(view, Gravity.BOTTOM, 0, 0) edit.append(text)
} } else {
edit.replace(start, end, text)//光标所在位置插入文字
} }
} }
private fun closePopupWindow() {
mSoftKeyboardTool?.dismiss()
} }
private inner class KeyboardOnGlobalChangeListener : ViewTreeObserver.OnGlobalLayoutListener { override fun sendText(text: String) {
override fun onGlobalLayout() { if (text == AppConst.keyboardToolChars[0]) {
activity?.let { val view = dialog?.window?.decorView?.findFocus()
val rect = Rect() view?.clearFocus()
// 获取当前页面窗口的显示范围
dialog?.window?.decorView?.getWindowVisibleDisplayFrame(rect)
val screenHeight = it.displayMetrics.heightPixels
val keyboardHeight = screenHeight - rect.bottom // 输入法的高度
val preShowing = mIsSoftKeyBoardShowing
if (abs(keyboardHeight) > screenHeight / 5) {
mIsSoftKeyBoardShowing = true // 超过屏幕五分之一则表示弹出了输入法
showKeyboardTopPopupWindow()
} else { } else {
mIsSoftKeyBoardShowing = false insertText(text)
if (preShowing) {
closePopupWindow()
}
}
}
} }
} }

Loading…
Cancel
Save