pull/32/head
kunfei 5 years ago
parent b4b1625deb
commit e6ff18260c
  1. 9
      app/src/main/AndroidManifest.xml
  2. 67
      app/src/main/java/io/legado/app/ui/sourceedit/SourceEditActivity.kt
  3. 39
      app/src/main/java/io/legado/app/ui/widget/KeyboardToolPop.kt
  4. 216
      app/src/main/res/layout/pop_keyboard_tool.xml

@ -38,10 +38,15 @@
<activity android:name=".ui.replacerule.ReplaceRuleActivity" /> <activity android:name=".ui.replacerule.ReplaceRuleActivity" />
<activity android:name=".ui.search.SearchActivity" /> <activity android:name=".ui.search.SearchActivity" />
<activity android:name=".ui.about.AboutActivity" /> <activity android:name=".ui.about.AboutActivity" />
<activity android:name=".help.permission.PermissionActivity" <activity
android:name=".help.permission.PermissionActivity"
android:theme="@style/Activity.Permission" /> android:theme="@style/Activity.Permission" />
<activity android:name=".ui.config.ConfigActivity" /> <activity android:name=".ui.config.ConfigActivity" />
<activity android:name=".ui.sourceedit.SourceEditActivity"/> <activity
android:name=".ui.sourceedit.SourceEditActivity"
android:configChanges="locale|keyboardHidden|orientation|screenSize|smallestScreenSize|screenLayout"
android:launchMode="singleTask"
android:windowSoftInputMode="adjustResize|stateHidden" />
<activity android:name=".ui.bookshelf.BookshelfActivity" /> <activity android:name=".ui.bookshelf.BookshelfActivity" />
<activity android:name=".ui.qrcode.QrCodeActivity" /> <activity android:name=".ui.qrcode.QrCodeActivity" />
<activity android:name=".ui.about.DonateActivity" /> <activity android:name=".ui.about.DonateActivity" />

@ -1,8 +1,13 @@
package io.legado.app.ui.sourceedit package io.legado.app.ui.sourceedit
import android.graphics.Rect
import android.os.Bundle import android.os.Bundle
import android.view.Gravity
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.ViewTreeObserver
import android.widget.EditText
import android.widget.PopupWindow
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.tabs.TabLayout 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.base.BaseActivity
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.data.entities.rule.* 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.GSON
import io.legado.app.utils.getViewModel import io.legado.app.utils.getViewModel
import kotlinx.android.synthetic.main.activity_source_edit.* import kotlinx.android.synthetic.main.activity_source_edit.*
import org.jetbrains.anko.displayMetrics
import org.jetbrains.anko.toast import org.jetbrains.anko.toast
import kotlin.math.abs
class SourceEditActivity : BaseActivity<SourceEditViewModel>() { class SourceEditActivity : BaseActivity<SourceEditViewModel>(), KeyboardToolPop.OnClickListener {
override val viewModel: SourceEditViewModel override val viewModel: SourceEditViewModel
get() = getViewModel(SourceEditViewModel::class.java) get() = getViewModel(SourceEditViewModel::class.java)
override val layoutID: Int override val layoutID: Int
@ -29,6 +37,9 @@ class SourceEditActivity : BaseActivity<SourceEditViewModel>() {
private val tocEditList: ArrayList<EditEntity> = ArrayList() private val tocEditList: ArrayList<EditEntity> = ArrayList()
private val contentEditList: ArrayList<EditEntity> = ArrayList() private val contentEditList: ArrayList<EditEntity> = ArrayList()
private var mSoftKeyboardTool: PopupWindow? = null
private var mIsSoftKeyBoardShowing = false
override fun onViewModelCreated(viewModel: SourceEditViewModel, savedInstanceState: Bundle?) { override fun onViewModelCreated(viewModel: SourceEditViewModel, savedInstanceState: Bundle?) {
initView() initView()
viewModel.sourceLiveData.observe(this, Observer { viewModel.sourceLiveData.observe(this, Observer {
@ -66,6 +77,8 @@ class SourceEditActivity : BaseActivity<SourceEditViewModel>() {
} }
private fun initView() { private fun initView() {
mSoftKeyboardTool = KeyboardToolPop(this, this)
window.decorView.viewTreeObserver.addOnGlobalLayoutListener(KeyboardOnGlobalChangeListener())
recycler_view.layoutManager = LinearLayoutManager(this) recycler_view.layoutManager = LinearLayoutManager(this)
recycler_view.adapter = adapter recycler_view.adapter = adapter
tab_layout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener { tab_layout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
@ -285,5 +298,57 @@ class SourceEditActivity : BaseActivity<SourceEditViewModel>() {
return source 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) class EditEntity(var key: String, var value: String?, var hint: Int)
} }

@ -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<LinearLayout>(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)
}
}

@ -0,0 +1,216 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/keyboard_top_view_tip_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="5dp"
android:background="@color/background_card"
xmlns:android="http://schemas.android.com/apk/res/android">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:ignore="UselessParent">
<LinearLayout
android:id="@+id/ll_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:text="@string/at_char"
android:textSize="14sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:text="@string/and_char"
android:textSize="14sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:text="|"
android:textSize="14sp"
tools:ignore="HardcodedText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="%"
android:textSize="14sp"
tools:ignore="HardcodedText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:text="/"
android:textSize="14sp"
tools:ignore="HardcodedText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:text=":"
android:textSize="14sp"
tools:ignore="HardcodedText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:text="["
android:textSize="14sp"
tools:ignore="HardcodedText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:text="]"
android:textSize="14sp"
tools:ignore="HardcodedText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:text="{"
android:textSize="14sp"
tools:ignore="HardcodedText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:text="}"
android:textSize="14sp"
tools:ignore="HardcodedText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:text="$"
android:textSize="14sp"
tools:ignore="HardcodedText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:text="#"
android:textSize="14sp"
tools:ignore="HardcodedText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:text="!"
android:textSize="14sp"
tools:ignore="HardcodedText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:text="."
android:textSize="14sp"
tools:ignore="HardcodedText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:text="xpath"
android:textSize="14sp"
tools:ignore="HardcodedText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:text="json"
android:textSize="14sp"
tools:ignore="HardcodedText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:text="css"
android:textSize="14sp"
tools:ignore="HardcodedText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:text="id"
android:textSize="14sp"
tools:ignore="HardcodedText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:text="class"
android:textSize="14sp"
tools:ignore="HardcodedText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:text="tag"
android:textSize="14sp"
tools:ignore="HardcodedText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:text="children"
android:textSize="14sp"
tools:ignore="HardcodedText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:text="href"
android:textSize="14sp"
tools:ignore="HardcodedText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:text="src"
android:textSize="14sp"
tools:ignore="HardcodedText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:text="textNodes"
android:textSize="14sp"
tools:ignore="HardcodedText" />
</LinearLayout>
</HorizontalScrollView>
</FrameLayout>
Loading…
Cancel
Save