-
-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix #136, make sure swiftkey arrows work well
- Loading branch information
Showing
2 changed files
with
56 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
app/src/main/kotlin/com/simplemobiletools/notes/helpers/MyMovementMethod.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |