【问题标题】:Android: Clickable TextView links / buttons [duplicate]Android:可点击的TextView链接/按钮[重复]
【发布时间】:2018-08-30 07:21:09
【问题描述】:

我想在我的注册视图上创建一个 TextView,用于通知法律条款。 所以文字是smth。比如:"By signing up you accept our privacy policy and terms and conditions."
Privacy Policy 以及 terms and conditions 应该是可点击的并触发回调。

通过最初的方法来自 HTML 是在其中添加一个链接,但这些似乎仅适用于网络/浏览器应用程序。
所以我考虑创建一个嵌套布局,其中包含作为单独文本的文本,例如:

<LinearLayout /* ... */>
  <TextView android:text="By signing up you accept our "/>

  <TextView android:text="privacy policy" 
    android:clickable="true" 
    android:focusable="true" />

  <TextView android:text="and" />

  <TextView android:text="terms and conditions" 
    android:clickable="true" 
    android:focusable="true" />

  <TextView android:text="." />
</LinearLayout>

但这种方法似乎有点奇怪,因为我必须创建如此多的元素和额外的布局。 有比我的方法更好的解决方案吗?

【问题讨论】:

标签: android android-layout textview


【解决方案1】:

在文本视图上设置文本

textView.setText(Html.fromHtml("I have read and agree to the " +
        "<a href='app.activity.termsanduse://Kode'>Terms of Use</a>"));

在清单中设置活动

<activity
    android:name=".app.activity.TermsAndUse"
    android:screenOrientation="portrait">
    <intent-filter>
        <category android:name="android.intent.category.DEFAULT" />
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="app.activity.termsanduse" />
    </intent-filter>
</activity>

【讨论】:

    【解决方案2】:

    在 strings.xml 中定义您的字符串/语句(也有助于本地化)

    将此添加到您的 strings.xml 中

    <string name="taclink"> By signing up you accept our <![CDATA[ <a href="http://web.com/xyz/Terms.html">Terms</a>]]>
         and <![CDATA[<a href="http://web.com/pqr/Privacy.html">Privacy Policy</a>]]></string>
    

    在你的java代码中设置“taclink”字符串资源textview,

    termsandcondition.setText(Html.fromHtml(resources.getString(com.app.android.R.string.taclink)));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-13
      • 2013-04-12
      • 1970-01-01
      • 2016-04-07
      • 2015-04-27
      • 1970-01-01
      • 2012-09-07
      相关资源
      最近更新 更多