fix #136, make sure swiftkey arrows work well

This commit is contained in:
tibbi 2018-04-05 20:10:07 +02:00
parent 153fd81150
commit 696858e2bc
2 changed files with 56 additions and 6 deletions

View File

@ -5,7 +5,6 @@ import android.os.Bundle
import android.support.v4.app.Fragment
import android.text.Editable
import android.text.TextWatcher
import android.text.method.LinkMovementMethod
import android.text.util.Linkify
import android.util.TypedValue
import android.view.Gravity
@ -18,10 +17,7 @@ import com.simplemobiletools.commons.extensions.onGlobalLayout
import com.simplemobiletools.notes.R
import com.simplemobiletools.notes.activities.MainActivity
import com.simplemobiletools.notes.extensions.*
import com.simplemobiletools.notes.helpers.DBHelper
import com.simplemobiletools.notes.helpers.GRAVITY_CENTER
import com.simplemobiletools.notes.helpers.GRAVITY_RIGHT
import com.simplemobiletools.notes.helpers.NOTE_ID
import com.simplemobiletools.notes.helpers.*
import com.simplemobiletools.notes.models.Note
import kotlinx.android.synthetic.main.fragment_note.*
import kotlinx.android.synthetic.main.fragment_note.view.*
@ -47,7 +43,7 @@ class NoteFragment : Fragment() {
view.notes_view.apply {
linksClickable = true
autoLinkMask = Linkify.WEB_URLS or Linkify.EMAIL_ADDRESSES
movementMethod = LinkMovementMethod.getInstance()
movementMethod = MyMovementMethod.getInstance()
}
}

View File

@ -0,0 +1,54 @@
package com.simplemobiletools.notes.helpers
import android.text.Selection
import android.text.Spannable
import android.text.method.ArrowKeyMovementMethod
import android.text.style.ClickableSpan
import android.view.MotionEvent
import android.widget.TextView
class MyMovementMethod : ArrowKeyMovementMethod() {
companion object {
private var sInstance: MyMovementMethod? = null
fun getInstance(): MyMovementMethod {
if (sInstance == null) {
sInstance = MyMovementMethod()
}
return sInstance!!
}
}
override fun onTouchEvent(widget: TextView, buffer: Spannable, event: MotionEvent): Boolean {
val action = event.action
if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
var x = event.x.toInt()
var y = event.y.toInt()
x -= widget.totalPaddingLeft
y -= widget.totalPaddingTop
x += widget.scrollX
y += widget.scrollY
val layout = widget.layout
val line = layout.getLineForVertical(y)
val off = layout.getOffsetForHorizontal(line, x.toFloat())
val links = buffer.getSpans(off, off, ClickableSpan::class.java)
if (links.isNotEmpty()) {
if (action == MotionEvent.ACTION_UP) {
links[0].onClick(widget)
} else if (action == MotionEvent.ACTION_DOWN) {
Selection.setSelection(buffer,
buffer.getSpanStart(links[0]),
buffer.getSpanEnd(links[0]))
}
return true
}
}
return super.onTouchEvent(widget, buffer, event)
}
}