Kpan icon indicating copy to clipboard operation
Kpan copied to clipboard

LinkMovementMethod with text gravity and singleLine issue

Open 2dxgujun opened this issue 5 years ago • 0 comments

private class LinkMovementMethodTouchListener : View.OnTouchListener {
    override fun onTouch(
      v: View,
      event: MotionEvent
    ): Boolean {
      val widget = v as TextView
      val action = event.action
      if (widget.text is SpannedString && action == MotionEvent.ACTION_UP) {
        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 = (widget.text as SpannedString).getSpans(off, off, ClickableSpan::class.java)
         if (links.isNotEmpty()) {
          links[0].onClick(widget)
          return true
        }
      }
      return true
    }
  }

2dxgujun avatar Nov 02 '18 02:11 2dxgujun