閱讀419 返回首頁    go 阿裏雲 go 技術社區[雲棲]


android給 TextView 加上效果和事件響應 判斷是否點擊函數

android textView 加入連接方式:
1:使用android: 隻需在textview中加入這個屬性在裏麵寫的文字中包含網址、電話、email的會自動加入連接地址。
如:
<TextView xmlns:androhttps://schemas.android.com/apk/res/android%22%C2%A0" target="_blank">https://schemas.android.com/apk/res/android"
android: android:layout_width="match_parent"
android:layout_height="match_parent" android:
android:text="@string/link_text_auto" />

2:uses a string resource containing explicit <a> tags to specify

links.
如: <string name="link_text_manual"><b>text2:</b> This is some other
text, with a <a href="https://www.google.com">link</a> specified
via an &lt;a&gt; tag. Use a \"tel:\" URL
to <a href="tel:4155551212">dial a phone number</a>.
</string>
別忘了
TextView t2 = (TextView) findViewById(R.id.text2);
t2.setMovementMethod(LinkMovementMethod.getInstance());

3: builds the text in the Java code using HTML
TextView t3 = (TextView) findViewById(R.id.text3);
t3.setText(Html.fromHtml("<b>text3:</b> Text with a "
+ "<a href=\"https://www.google.com\">link</a> "
+ "created in the Java source code using HTML."));
t3.setMovementMethod(LinkMovementMethod.getInstance());


4:字符串截取方法
SpannableString ss = new SpannableString("text4: Click here to dial the phone.");
ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new URLSpan("tel:4155551212"), 13, 17, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView t4 = (TextView) findViewById(R.id.text4);
t4.setText(ss);
t4.setMovementMethod(LinkMovementMethod.getInstance());
Android中我們為了實現文本的滾動可以在ScrollView中嵌入一個TextView,其實TextView自己也可以實現多行滾動的,畢竟ScrollView必須隻能有一個直接的子類布局。隻要在layout中簡單設置幾個屬性就可以輕鬆實現
<TextView
android:
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" <!--垂直滾動條 -->
android:singleLine="false" <!--實現多行 -->
android:maxLines="15" <!--最多不超過15行 -->
android:textColor="#FF0000"
/>
當然我們為了讓TextView動起來,還需要用到TextView的setMovementMethod方法設置一個滾動實例,代碼如下
TextView tvAndroid123 = (TextView)findViewById(R.id.tvCWJ);
tvAndroid123.setMovementMethod(ScrollingMovementMethod.getInstance()); // Android開發網提示相關的可以查看SDK中android.text.method分支了解更多
ad_link = (TextView) findViewById(R.id.ad_link);
ad_link.setText(Html.fromHtml("<a href="\" mce_href="\"""+mURL.getLink()+"\">"+Html.fromHtml(mURL.getLabel()+"</a>")));
ad_link.setMovementMethod(LinkMovementMethod.getInstance());

判斷是否點擊函數:textview.isPressed()

最後更新:2017-04-02 17:51:23

  上一篇:go 使用 Linux 搭建 VPN
  下一篇:go android string.xml中的string樣式總結