如何处理Android TextView的自动链接点击事件?

发布于 2025-01-30 06:11:07 字数 753 浏览 2 评论 0原文

这是我在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"
                />

代码效果很好。 TextView中的Web URL自动突出显示并可以单击。但是,在单击超链接中时,它以错误结尾。

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

注意:recyclerview项目中的文本视图

问题:如何解决此问题?还是如何以编程方式处理超链接点击事件?

Here is my xml code for autolink in textview.

 <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"
                />

Code works perfectly fine. Web url in TextView are auto highlighted and able to click. But on clicking in hyperlink it ends with an error.

Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

Note: TextView is placed inside recyclerview item

Question : How to resolve this issue? or how to handle hyperlink click event programmatically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾`听者〃 2025-02-06 06:11:07

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

您可以在下面使用代码来处理超链接,以编程方式单击事件:

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>

I can not reproduce your error actually.

You can use code below to handle hyperlink click event programmatically:

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)
    }
}

Full working code with 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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文