【问题标题】:How to make a part of text clickable like button in Android?如何在Android中使部分文本像按钮一样可点击?
【发布时间】:2012-02-22 23:36:28
【问题描述】:

我想将按钮 myButton 添加到 TextView。如何将它放在一行中,使其看起来像一组行。这是要添加的文本示例

请在此处按此刷新值集。

我想让“这里”作为可点击按钮。我该怎么做。任何关于它的 sn-p 都会有所帮助。

【问题讨论】:

标签: android button click textview


【解决方案1】:

android:background="@null"Button 的背景设置为空,并赋予与其他TextView 相同的文本颜色。

或者您可以将 3 个 TextView 和 setOnClickListener 带到中间一个。

【讨论】:

  • 如何让三个文本视图在多行中一个接一个
  • 嘿,你的建议对我很有用...谢谢
  • 简单优雅!我需要链接文本颜色与 web 中超链接的默认颜色相同。所以我把RGB(0, 122, 255),和看起来一样! (Y)
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    制作三个 textView ex: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>
    

    【讨论】:

    • 当我们进入多行文本时,这并不好。如何通过修改sn-p来解决它。
    【解决方案4】:

    只需将以下属性添加到 XML 布局文件中的 TextView 即可使其可点击。

    android:clickable="true"
    

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

    【讨论】:

      【解决方案5】:

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

      【讨论】:

      • 最简单的解决方案并准确回答原始问题。
      猜你喜欢
      • 1970-01-01
      • 2016-12-10
      • 1970-01-01
      • 2018-03-03
      • 2019-12-11
      • 1970-01-01
      • 1970-01-01
      • 2011-06-04
      • 1970-01-01
      相关资源
      最近更新 更多