pull/36/head
kunfei 5 years ago
parent 5c3d50657a
commit 358f955071
  1. 6
      app/src/main/java/io/legado/app/ui/book/source/edit/SourceEditActivity.kt
  2. 90
      app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt
  3. 8
      app/src/main/java/io/legado/app/ui/widget/KeyboardToolPop.kt

@ -34,7 +34,7 @@ import kotlin.math.abs
class SourceEditActivity : class SourceEditActivity :
VMBaseActivity<SourceEditViewModel>(R.layout.activity_book_source_edit, false), VMBaseActivity<SourceEditViewModel>(R.layout.activity_book_source_edit, false),
KeyboardToolPop.OnClickListener { KeyboardToolPop.CallBack {
override val viewModel: SourceEditViewModel override val viewModel: SourceEditViewModel
get() = getViewModel(SourceEditViewModel::class.java) get() = getViewModel(SourceEditViewModel::class.java)
@ -92,7 +92,7 @@ class SourceEditActivity :
toast("书源名称和URL不能为空") toast("书源名称和URL不能为空")
} else { } else {
viewModel.save(bookSource) { viewModel.save(bookSource) {
startActivity<SourceDebugActivity>("key" to bookSource.bookSourceUrl) startActivity<SourceDebugActivity>(Pair("key", bookSource.bookSourceUrl))
} }
} }
} }
@ -567,7 +567,7 @@ class SourceEditActivity :
return source return source
} }
override fun click(text: String) { override fun sendText(text: String) {
if (text.isBlank()) return if (text.isBlank()) return
val view = window.decorView.findFocus() val view = window.decorView.findFocus()
if (view is EditText) { if (view is EditText) {

@ -1,20 +1,108 @@
package io.legado.app.ui.rss.source.edit package io.legado.app.ui.rss.source.edit
import android.graphics.Rect
import android.os.Bundle import android.os.Bundle
import android.view.Gravity
import android.view.Menu
import android.view.ViewTreeObserver
import android.widget.EditText
import android.widget.PopupWindow
import androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.VMBaseActivity import io.legado.app.base.VMBaseActivity
import io.legado.app.constant.AppConst
import io.legado.app.lib.theme.ATH
import io.legado.app.ui.widget.KeyboardToolPop
import io.legado.app.utils.getViewModel import io.legado.app.utils.getViewModel
import kotlinx.android.synthetic.main.activity_book_source_edit.*
import org.jetbrains.anko.displayMetrics
import kotlin.math.abs
class RssSourceEditActivity : class RssSourceEditActivity :
VMBaseActivity<RssSourceEditViewModel>(R.layout.activity_rss_source_edit, false) { VMBaseActivity<RssSourceEditViewModel>(R.layout.activity_rss_source_edit, false),
KeyboardToolPop.CallBack {
private var mSoftKeyboardTool: PopupWindow? = null
private var mIsSoftKeyBoardShowing = false
private lateinit var adapter: RssSourceEditAdapter
override val viewModel: RssSourceEditViewModel override val viewModel: RssSourceEditViewModel
get() = getViewModel(RssSourceEditViewModel::class.java) get() = getViewModel(RssSourceEditViewModel::class.java)
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
initView()
}
override fun onDestroy() {
super.onDestroy()
mSoftKeyboardTool?.dismiss()
}
override fun onCompatCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.source_edit, menu)
return super.onCompatCreateOptionsMenu(menu)
}
private fun initView() {
ATH.applyEdgeEffectColor(recycler_view)
mSoftKeyboardTool = KeyboardToolPop(this, AppConst.keyboardToolChars, this)
window.decorView.viewTreeObserver.addOnGlobalLayoutListener(KeyboardOnGlobalChangeListener())
recycler_view.layoutManager = LinearLayoutManager(this)
recycler_view.adapter = adapter
} }
override fun sendText(text: String) {
if (text.isBlank()) return
val view = window.decorView.findFocus()
if (view is EditText) {
val start = view.selectionStart
val end = view.selectionEnd
val edit = view.editableText//获取EditText的文字
if (start < 0 || start >= edit.length) {
edit.append(text)
} else {
edit.replace(start, end, text)//光标所在位置插入文字
}
}
}
private fun showKeyboardTopPopupWindow() {
mSoftKeyboardTool?.isShowing?.let { if (it) return }
if (!isFinishing) {
mSoftKeyboardTool?.showAtLocation(ll_content, Gravity.BOTTOM, 0, 0)
}
}
private fun closePopupWindow() {
mSoftKeyboardTool?.let {
if (it.isShowing) {
it.dismiss()
}
}
}
private inner class KeyboardOnGlobalChangeListener : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
val rect = Rect()
// 获取当前页面窗口的显示范围
window.decorView.getWindowVisibleDisplayFrame(rect)
val screenHeight = this@RssSourceEditActivity.displayMetrics.heightPixels
val keyboardHeight = screenHeight - rect.bottom // 输入法的高度
val preShowing = mIsSoftKeyBoardShowing
if (abs(keyboardHeight) > screenHeight / 5) {
mIsSoftKeyBoardShowing = true // 超过屏幕五分之一则表示弹出了输入法
recycler_view.setPadding(0, 0, 0, 100)
showKeyboardTopPopupWindow()
} else {
mIsSoftKeyBoardShowing = false
recycler_view.setPadding(0, 0, 0, 0)
if (preShowing) {
closePopupWindow()
}
}
}
}
} }

@ -18,7 +18,7 @@ import org.jetbrains.anko.sdk27.listeners.onClick
class KeyboardToolPop( class KeyboardToolPop(
context: Context, context: Context,
private val chars: List<String>, private val chars: List<String>,
val onClickListener: OnClickListener? val callBack: CallBack?
) : PopupWindow(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) { ) : PopupWindow(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) {
init { init {
@ -45,13 +45,13 @@ class KeyboardToolPop(
override fun convert(holder: ItemViewHolder, item: String, payloads: MutableList<Any>) { override fun convert(holder: ItemViewHolder, item: String, payloads: MutableList<Any>) {
with(holder.itemView) { with(holder.itemView) {
text_view.text = item text_view.text = item
onClick { onClickListener?.click(item) } onClick { callBack?.sendText(item) }
} }
} }
} }
interface OnClickListener { interface CallBack {
fun click(text: String) fun sendText(text: String)
} }
} }

Loading…
Cancel
Save