From e6ff18260cc5c9bf89b8411792ebe2005647a2ac Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 29 Jun 2019 16:18:13 +0800 Subject: [PATCH] up --- app/src/main/AndroidManifest.xml | 69 +++--- .../app/ui/sourceedit/SourceEditActivity.kt | 67 +++++- .../legado/app/ui/widget/KeyboardToolPop.kt | 39 ++++ app/src/main/res/layout/pop_keyboard_tool.xml | 216 ++++++++++++++++++ 4 files changed, 358 insertions(+), 33 deletions(-) create mode 100644 app/src/main/java/io/legado/app/ui/widget/KeyboardToolPop.kt create mode 100644 app/src/main/res/layout/pop_keyboard_tool.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f3aa77c93..7f9976067 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,47 +1,52 @@ + xmlns:tools="http://schemas.android.com/tools" + package="io.legado.app"> - - - - - - - - - - - - + + + + + + + + + + + + + android:name=".App" + android:allowBackup="true" + android:icon="@mipmap/ic_launcher" + android:label="@string/app_name" + android:networkSecurityConfig="@xml/network_security_config" + android:roundIcon="@mipmap/ic_launcher_round" + android:supportsRtl="true" + android:theme="@style/AppTheme.Light" + tools:ignore="AllowBackup,GoogleAppIndexingWarning,UnusedAttribute"> - - + + - - - - - - + + + + + + diff --git a/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditActivity.kt b/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditActivity.kt index 26d704a79..f848d95b9 100644 --- a/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditActivity.kt @@ -1,8 +1,13 @@ package io.legado.app.ui.sourceedit +import android.graphics.Rect import android.os.Bundle +import android.view.Gravity import android.view.Menu import android.view.MenuItem +import android.view.ViewTreeObserver +import android.widget.EditText +import android.widget.PopupWindow import androidx.lifecycle.Observer import androidx.recyclerview.widget.LinearLayoutManager import com.google.android.material.tabs.TabLayout @@ -10,12 +15,15 @@ import io.legado.app.R import io.legado.app.base.BaseActivity import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.rule.* +import io.legado.app.ui.widget.KeyboardToolPop import io.legado.app.utils.GSON import io.legado.app.utils.getViewModel import kotlinx.android.synthetic.main.activity_source_edit.* +import org.jetbrains.anko.displayMetrics import org.jetbrains.anko.toast +import kotlin.math.abs -class SourceEditActivity : BaseActivity() { +class SourceEditActivity : BaseActivity(), KeyboardToolPop.OnClickListener { override val viewModel: SourceEditViewModel get() = getViewModel(SourceEditViewModel::class.java) override val layoutID: Int @@ -29,6 +37,9 @@ class SourceEditActivity : BaseActivity() { private val tocEditList: ArrayList = ArrayList() private val contentEditList: ArrayList = ArrayList() + private var mSoftKeyboardTool: PopupWindow? = null + private var mIsSoftKeyBoardShowing = false + override fun onViewModelCreated(viewModel: SourceEditViewModel, savedInstanceState: Bundle?) { initView() viewModel.sourceLiveData.observe(this, Observer { @@ -66,6 +77,8 @@ class SourceEditActivity : BaseActivity() { } private fun initView() { + mSoftKeyboardTool = KeyboardToolPop(this, this) + window.decorView.viewTreeObserver.addOnGlobalLayoutListener(KeyboardOnGlobalChangeListener()) recycler_view.layoutManager = LinearLayoutManager(this) recycler_view.adapter = adapter tab_layout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener { @@ -285,5 +298,57 @@ class SourceEditActivity : BaseActivity() { return source } + override fun click(text: String) { + if (text.isNullOrBlank()) 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@SourceEditActivity.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() + } + } + } + } + class EditEntity(var key: String, var value: String?, var hint: Int) } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/widget/KeyboardToolPop.kt b/app/src/main/java/io/legado/app/ui/widget/KeyboardToolPop.kt new file mode 100644 index 000000000..b4a61e6ff --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/widget/KeyboardToolPop.kt @@ -0,0 +1,39 @@ +package io.legado.app.ui.widget + +import android.annotation.SuppressLint +import android.content.Context +import android.view.LayoutInflater +import android.view.ViewGroup +import android.widget.LinearLayout +import android.widget.PopupWindow +import android.widget.TextView +import io.legado.app.R + + +class KeyboardToolPop(context: Context, onClickListener: OnClickListener?) : PopupWindow(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) { + + init { + @SuppressLint("InflateParams") + val view = LayoutInflater.from(context).inflate(R.layout.pop_keyboard_tool, null) + this.contentView = view + + isTouchable = true + isOutsideTouchable = false + isFocusable = false + inputMethodMode = INPUT_METHOD_NEEDED //解决遮盖输入法 + + val linearLayout = contentView.findViewById(R.id.ll_content) + + for (i in 0 until linearLayout.childCount) { + val tv = linearLayout.getChildAt(i) as TextView + tv.setOnClickListener { v -> + onClickListener?.click((v as TextView).text.toString()) + } + } + } + + interface OnClickListener { + fun click(text: String) + } + +} diff --git a/app/src/main/res/layout/pop_keyboard_tool.xml b/app/src/main/res/layout/pop_keyboard_tool.xml new file mode 100644 index 000000000..d76d3f944 --- /dev/null +++ b/app/src/main/res/layout/pop_keyboard_tool.xml @@ -0,0 +1,216 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +