繁体   English   中英

如何处理android textview的自动链接点击事件?

[英]How to handle autolink click event of android textview?

这是我在 textview 中自动链接的 xml 代码。

 <TextView
                style="@style/statusTextList"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:autoLink="web"
                android:textColorLink="@color/hyperlinkColor"
                android:linksClickable="true"
                android:textColor="#ffffff"
                android:autoSizeTextType="uniform"
                />

代码工作得很好。 Web url 中的 TextView 自动高亮显示,可以点击。 但是在点击超链接时它以错误结束。

从活动上下文外部调用 startActivity() 需要 FLAG_ACTIVITY_NEW_TASK 标志。 这真的是你想要的吗?

注意:TextView 放在recyclerview item里面

问题:如何解决这个问题? 或者如何以编程方式处理超链接点击事件?

我实际上无法重现您的错误。

您可以使用下面的代码以编程方式处理超链接点击事件:

override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
    viewHolder.textView.apply {
        // replace first dataSet[position] with your url
        // replace second dataSet[position] with your url description
        text = Html.fromHtml(
            "<a href=\"${dataSet[position]}\">${dataSet[position]}</a> ")
        movementMethod = LinkMovementMethod.getInstance()
        setLinkTextColor(Color.BLUE)
    }
}

xml 的完整工作代码:

// MainActivity.kt
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val recyclerView = findViewById<RecyclerView>(R.id.rv)
        recyclerView.adapter = CustomAdapter(arrayOf("https://www.google.com"))
    }
}

class CustomAdapter(private val dataSet: Array<String>) :
    RecyclerView.Adapter<CustomAdapter.ViewHolder>() {

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val textView: TextView

        init {
            textView = view.findViewById(R.id.tv_test)
        }
    }

    override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(viewGroup.context)
            .inflate(R.layout.item_test, viewGroup, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
        viewHolder.textView.text = dataSet[position]
    }

    override fun getItemCount() = dataSet.size

}

// activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

</androidx.constraintlayout.widget.ConstraintLayout>

// item_test.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    xmlns:tools="http://schemas.android.com/tools">

    <TextView
        android:id="@+id/tv_test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:autoLink="web"
        android:linksClickable="true"
        android:textColor="#ffffff"
        tools:text="http://www.google.com"
        android:textColorLink="@color/design_default_color_primary"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM