问题
I have a few links in my application. One for a website, one for a phone number, and one for an email. The email and phone links are both working and clickable, however the website hyperlink is still not clickable for some reason. Any thoughts? Code below.
<string name="website" ><a href="http://www.XXXXXX.com">XXXXXX Website</a></string>
<string name="email" >sales@XXXXXXX.com</string>
<string name="phone" >P: XXX.XXX.XXXX</string>
<string name="fax" >F: XXX.XXX.XXXX</string>
Above are my strings, and below is the xml file that displays them:
<TextView android:id="@+id/website"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/imageButtonTwitter"
android:gravity="center_horizontal"
android:padding="10dp"
android:autoLink="web"
android:clickable="true"
android:linksClickable="true"
android:text="@string/website" />
<TextView android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/website"
android:gravity="center_horizontal"
android:padding="10dp"
android:autoLink="email"
android:linksClickable="true"
android:text="@string/email" />
<TextView android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/email"
android:gravity="center_horizontal"
android:padding="10dp"
android:autoLink="phone"
android:linksClickable="true"
android:text="@string/phone" />
<TextView android:id="@+id/fax"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/phone"
android:gravity="center_horizontal"
android:padding="10dp"
android:text="@string/fax" />
Like I said.. the others are clickable and working. I've tested it on two emulators, as well as my Galaxy S4. Any thoughts why the website is not clickable?
回答1:
You need to call this on your textview:
TextView tv = (TextView) findViewById(R.id. website);
tv.setMovementMethod(LinkMovementMethod.getInstance());
You need to remove from your TextView:
android:autoLink="web"
android:clickable="true"
android:linksClickable="true"
回答2:
Add this line -
htmlContent.setMovementMethod(LinkMovementMethod.getInstance());
回答3:
I'm not very experienced android programmer, but my small remarks which can be helpful (at least I hope):
Don't add those additional parameters to TextView
. Just android:text
and android:clickable="true"
is enough for having html clickable link.
And next when setting text please use:
TextView myTextView = (TextView) findViewById(R.id.myId);
myTextView.setText(Html.fromHtml(htmlText));
More here: android.text.Html
I'm guessing you are missing that Html.fromHtml
I hope it helps.
来源:https://stackoverflow.com/questions/20914485/android-hyperlink-is-not-clickable