如何在Android中使文本的一部分可点击,就像按钮一样?

9
我想在TextView中添加一个名为myButton的按钮。如何将其放置在一行内,使其看起来像一组线条?这里是要添加的文本示例。
请按这里以刷新值集。
我想将“这里”作为可点击按钮。我该怎么做?有任何相关片段将会有所帮助。

重复问题: https://dev59.com/ZHA75IYBdhLWcg3wYH7I - PC.
你可以尝试以下几种方法:https://dev59.com/tHI-5IYBdhLWcg3wu7Pv - Vikram Bodicherla
5个回答

14

通过 android:background="@null"Button 的背景设置为 null,同时与其他 TextView 的文本颜色相同。

或者您可以使用 3 个 TextView 并将 setOnClickListener 应用于中间的一个。


如何在多行中使三个TextView相互跟随 - Karthik
你的建议对我非常有帮助,谢谢。 - user1387035
我需要链接文本颜色与 web 中超链接的默认颜色相同。所以我设置了 RGB(0, 122, 255),看起来一模一样!(Y) - Randika Vishman

12

使用无边框的android属性设置Button的样式,以去除背景和边框:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="?android:attr/borderlessButtonStyle"
    android:text="here" />

3

创建三个textView,例如:textView1、textView2和textView3。

<RelativeLayout android:id="@+id/textLay"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    >

    <TextView android:id="@+id/textView1" android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="Please press this"/>

    <TextView android:id="@+id/textView2" android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
         android:text="here" 
        android:layout_toRightOf="@+id/textView1"  />

    <TextView android:id="@+id/textView3" android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
         android:text="to refresh the set of values." 
        android:layout_toRightOf="@+id/textView2"  />

</RelativeLayout>

3
处理多行文本时,这个方法并不有效。如何修改代码片段以解决这个问题? - Karthik

3
在XML布局文件中,只需将以下属性添加到TextView,即可使其可点击。
android:clickable="true"

在您的Java文件中添加OnClickListener以完成对文本视图的点击操作。

2
在XML中为TextView添加android:clickable="true"android:onClick="yourclickname",然后在您的活动中定义yourclickname。这将使您的TextView可点击,并在单击时触发指定的方法。

最简单的解决方案,可以准确地回答原问题。 - Jason K.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接