Android:可点击的TextView链接/按钮

时间:2018-08-30 07:21:10

标签: android android-layout textview

我想在我的注册视图上创建一个TextView来通知法律条款。 因此,文字很简短。像:"By signing up you accept our privacy policy and terms and conditions."
Privacy Policyterms and conditions应该可以点击并触发回调。

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

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

但是这种方法似乎有点奇怪,因为我必须创建这么多元素和其他布局。 有没有比我的方法更好的解决方案?

2 个答案:

答案 0 :(得分:0)

在“文本视图”上设置文本

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>

答案 1 :(得分:0)

在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)));
相关问题