android學習---TextView

1.TextView經常使用屬性html

  TextView的經常使用屬性有如下一些:前端

  setText();                        //設置文本內容,同xml中的android:text

  setTextSize();                    //設置文本字體大小,同xml中的android:textSize

  setTextColor();                   //設置文本顏色,同xml中的android:textColor

  setBackgroundColor();             //設置背景顏色,同xml中的android:background

  此外,還能夠在xml中設置一些TextView的屬性,以下:java

  android:autoLink                 //設置是否顯示爲可點擊的連接。可選值(none/web/email/phone/map/all)

  android:drawableBottom           //在text的下方輸出一個drawable(圖片)

  android:drawableLeft             //在text的左邊輸出一個drawable(圖片)

  android:drawableRight            //在text的右邊輸出一個drawable(圖片)

  android:drawableTop              //在text的正上方輸出一個drawable(圖片)

  android:drawablePadding          //設置text與drawable(圖片)的間隔,與drawableLeft、drawableRight、drawableTop、drawableBottom一                                    起使用,可設置爲負數,單獨使用沒有效果

  android:ellipsize                //設置當文字過長時,該控件該如何顯示。可設置以下屬性值:"start"省略號顯示在開頭;
                       "end」省略號顯示在結尾;"middle"省略號顯示在中間; "marquee" 以跑馬燈的方式顯示(動畫橫向移動)   android:gravity //設置文本位置,設置成"center",文本將居中顯示   android:linksClickable //設置點擊時是否連接,即便設置了autoLink   android:marqueeRepeatLimit //在ellipsize設定爲marquee時,設置重複滾動的次數,設置爲marquee_forever時表示無限次。   android:lines //設置文本的行數,設置兩行就顯示兩行,即便第二行沒有數據   android:shadowRadius //設置陰影的半徑。設置爲0.1就變成字體的顏色了,通常設置爲3.0的效果比較好   android:shadowColor //指定文本陰影的顏色,須要與shadowRadius一塊兒使用   android:singleLine //設置單行顯示   android:textColorLink //設置文字連接的顏色   android:textScaleX //設置文字之間間隔,默認爲1.0f   android:textStyle //設置字形 bold(粗體) 0, italic(斜體) 1, bolditalic(又粗又斜) 2, 能夠設置一個或多個,用「|」隔開   android:typeface //設置文本字體,必須是如下常量值之一:normal 0, sans 1, serif 2, monospace(等寬字體) 3

  2.TextView顯示URL文本android

  在TextView中預約義了一些相似HTML的標籤,經過這些標籤可使TextView控件顯示不一樣的顏色、大小和字體的文字。HTML的經常使用標籤有如下一些:web

  <font>設置文本的字體、字體顏色、字體尺寸,如:<font size ="3" color = "red">This is Some Text!</font>後端

  <big>設置大號字體效果數組

  <small>設置小號字體效果app

  <i>設置斜體文本效果框架

  <b>設置粗體文本效果ide

  <a>經過使用href屬性,建立指向另一個文檔的連接(超連接),如:<a href = http://www.baidu.com>百度</a>

  <br>插入一個簡單的換行符,注意:在HTML中,<br>標籤沒有結束標籤

  <p>自定在其先後建立一些空白

  <img>向網頁中嵌入一幅圖像,該標籤有兩個必須的屬性:src和alt

 

    使用這些標籤能夠用Html.fromHtml方法將這些標籤的字符串轉換成CharSequence接口,而後在TextView.setText()中進行設置。若是須要響應設置的HTML標籤進行響應,須要設置TextView.setMovementMethod(LinkMovementMethod.getInstance())。

  CharSequence爲接口類型,你們可能對其有點陌生,可是它的子類確定會讓你們有熟悉的感受,String、StringBuffer、StringBuilder、SpannableString、SpannableStringBuilder都是其子類,它包括了字符串的全部類,由於面向對象的多態性,在這裏把他理解成字符串類的抽象便可。

  除了使用HTML標籤的方式設定顯示文本中的URL地址、郵箱地址、電話等產生超連接出發相應的服務,可使用android:autoLink屬性來設置,如下是android:autoLink屬性的介紹:

  • None:默認的,不匹配任何鏈接。
  • web:網址。
  • email:郵箱。
  • phone:電話號碼。
  • map:匹配映射網址。
  • all:匹配全部鏈接。

實現頁面跳轉

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:orientation="vertical" >
 6 
 7     <TextView
 8         android:id="@+id/textview1"
 9         android:layout_width="fill_parent"
10         android:layout_height="wrap_content"
11         android:padding="20sp" />
12 
13     <TextView
14         android:id="@+id/textview2"
15         android:layout_width="fill_parent"
16         android:layout_height="wrap_content"
17         android:autoLink="all"
18         android:padding="20sp"
19         android:textSize="20sp" />
20 
21 </LinearLayout>
main.xml
 1 package com.leaf.android;
 2 
 3 import android.R.string;
 4 import android.app.Activity;
 5 import android.os.Bundle;
 6 import android.text.Html;
 7 import android.text.method.LinkMovementMethod;
 8 import android.widget.TextView;
 9 
10 public class Main extends Activity {
11     /** Called when the activity is first created. */
12     private TextView textView1, textView2;
13 
14     @Override
15     public void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.main);
18         textView1 = (TextView) this.findViewById(R.id.textview1);
19         textView2 = (TextView) this.findViewById(R.id.textview2);
20         
21         // 添加一段html的標誌
22         String html = "<font color = 'red'><big><i>I Love Android<i><big></font><p>";
23         html += "<big><a herf = ‘http://www.cnblogs.com/’>博客園</a></big>";
24         CharSequence charSequence = Html.fromHtml(html);
25         textView1.setText(charSequence);
26         textView1.setMovementMethod(LinkMovementMethod.getInstance());// 點擊的時候產生超連接
27 
28         String text = "個人博客: http://www.cnblogs.com/lea-fu/\n";
29         text += "個人郵箱:leaf816@gmail.com\n";
30         textView2.setText(text);
31         textView2.setMovementMethod(LinkMovementMethod.getInstance());// 點擊的時候產生超連接
32 
33     }
34 }
Main.java

效果              

 

textview圖文顯示

  在TextView中顯示圖片的例子,依然是使用HTML標籤的方式定義樣式,可是使用的是Html.fromHtml()的另一個重載的靜態方法,能夠設定<img>標籤中的圖像資源。

  static Spanned fromHtml(String source,Html.ImageGetter imageGetter,Html.TagHandler tagHandler)

  對於這個方法,在imageGetter參數中設定<img>標籤中的圖像資源文件,tagHandler主要是爲了處理Html類沒法識別的html標籤的狀況,通常不會用上,傳值爲null便可。

 1 package com.leaf.android;
 2 
 3 import java.lang.reflect.Field;
 4 
 5 import android.R.color;
 6 import android.app.Activity;
 7 import android.graphics.Color;
 8 import android.graphics.drawable.Drawable;
 9 import android.os.Bundle;
10 import android.text.Html;
11 import android.text.Html.ImageGetter;
12 import android.text.method.LinkMovementMethod;
13 import android.widget.TextView;
14 
15 public class Main extends Activity {
16     /** Called when the activity is first created. */
17     public int getResourcedId(String name) {
18         try {
19             // 根據資源的ID的變量名得到Field的對象,使用反射機制來實現的
20             Field field = R.drawable.class.getField(name);
21             // 取得並返回資源的id的字段(靜態變量)的值,使用反射機制
22             return Integer.parseInt(field.get(null).toString());
23         } catch (Exception e) {
24             // TODO: handle exception
25         }
26         return 0;
27     }
28 
29     @Override
30     public void onCreate(Bundle savedInstanceState) {
31         super.onCreate(savedInstanceState);
32         setContentView(R.layout.main);
33         TextView textView = (TextView) this.findViewById(R.id.textview);
34         textView.setTextColor(Color.BLACK);
35         textView.setBackgroundColor(Color.WHITE);
36         textView.setTextSize(20);
37         String html = "圖像1<img src='img1'/>圖像2<img src='img2'/>圖像3<img src='img3'/><p>";
38         html += "圖像4<a href = 'http://www.baidu.com'><img src='img4'></a>";
39         CharSequence charSequence = Html.fromHtml(html, new ImageGetter() {
40 
41             public Drawable getDrawable(String source) {
42                 // TODO Auto-generated method stub
43                 // 得到系統資源的信息,好比圖片信息
44                 Drawable drawable = getResources().getDrawable(
45                         getResourcedId(source));
46                 // 第三個圖片文件按照50%的比例進行壓縮
47                 if (source.equals("img3")) {
48                     drawable.setBounds(0, 0, drawable.getIntrinsicWidth() / 2,
49                             drawable.getIntrinsicHeight() / 2);
50                 } else {
51                     drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
52                             drawable.getIntrinsicHeight());
53                 }
54                 return drawable;
55             }
56 
57         }, null);
58         textView.setText(charSequence);
59         textView.setMovementMethod(LinkMovementMethod.getInstance());
60     }
61 }
Main.java
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:orientation="vertical" >
 6 
 7     <TextView
 8         android:id="@+id/textview"
 9         android:layout_width="fill_parent"
10         android:layout_height="wrap_content"
11         android:layout_margin="10dp"
12         android:background="#FFFFFF" />
13 
14 </LinearLayout>
main.xml

android實現點擊textview彈出activity

  在TextView添加點擊事件,導航到其餘的Activity中。使用SpannableString.setSpan()設定那一段文本須要相應點擊事件。與之相似的還有SpannableBuilder對象,他們的關係和String與StringBuilder同樣。

  void setSpan(Object what,int start,int end,int flags)

  在what參數中傳遞一個抽象類ClickableSpan,須要實現其onClick()方法,此爲指定文本的點擊相應時間。start和end分別指定須要響應onClick()方法的文本開始與結束。flags設定一個標識,肯定使用什麼方式選擇文本塊,通常使用Spanned接口下的SPAN_EXCLUSIVE_EXCLUSIVE對其進行賦值,表示遵循設定的開始於結束位置的文本塊。

 1 package com.leaf.android;
 2 
 3 import android.app.Activity;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 import android.text.SpannableString;
 7 import android.text.Spanned;
 8 import android.text.method.LinkMovementMethod;
 9 import android.text.style.ClickableSpan;
10 import android.view.View;
11 import android.widget.TextView;
12 
13 public class Main extends Activity {
14     /** Called when the activity is first created. */
15     @Override
16     public void onCreate(Bundle savedInstanceState) {
17         super.onCreate(savedInstanceState);
18         setContentView(R.layout.main);
19         TextView textView = (TextView) this.findViewById(R.id.textview);
20         TextView textView2 = (TextView) this.findViewById(R.id.textview2);
21         String text1 = "顯示Activity1";
22         String text2 = "顯示Activity2";
23         // 主要用來拆分字符串
24         SpannableString spannableString = new SpannableString(text1);
25         SpannableString spannableString2 = new SpannableString(text2);
26         spannableString.setSpan(new ClickableSpan() {
27 
28             @Override
29             public void onClick(View widget) {
30                 // TODO Auto-generated method stub
31                 Intent intent = new Intent(Main.this, Activity1.class);
32                 startActivity(intent);
33             }
34         }, 0, text1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
35         spannableString2.setSpan(new ClickableSpan() {
36 
37             @Override
38             public void onClick(View widget) {
39                 // TODO Auto-generated method stub
40                 Intent intent = new Intent(Main.this, Activity2.class);
41                 startActivity(intent);
42             }
43         }, 0, text2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
44         textView.setText(spannableString);
45         textView2.setText(spannableString2);
46         textView.setMovementMethod(LinkMovementMethod.getInstance());
47         textView2.setMovementMethod(LinkMovementMethod.getInstance());
48     }
49 }
Main.java
 1 package com.leaf.android;
 2 
 3 import android.os.Bundle;
 4 
 5 public class Activity1 extends Main {
 6 
 7     public Activity1() {
 8         // TODO Auto-generated constructor stub
 9         
10     }
11     @Override
12     public void onCreate(Bundle savedInstanceState) {
13         // TODO Auto-generated method stub
14         super.onCreate(savedInstanceState);
15         setTitle("Activity1");
16     }
17 }
Activity1.java
 1 package com.leaf.android;
 2 
 3 import android.os.Bundle;
 4 
 5 public class Activity2 extends Main {
 6 
 7     public Activity2() {
 8         // TODO Auto-generated constructor stub
 9     }
10     
11     @Override
12     public void onCreate(Bundle savedInstanceState) {
13         // TODO Auto-generated method stub
14         super.onCreate(savedInstanceState);
15         setTitle("Activity2");
16     }
17 
18 }
Activity2.java
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:orientation="vertical" >
 6 
 7     <TextView
 8         android:id="@+id/textview"
 9         android:layout_width="fill_parent"
10         android:layout_height="wrap_content"
11         android:textSize="20sp" />
12 
13     <TextView
14         android:id="@+id/textview2"
15         android:layout_width="fill_parent"
16         android:layout_height="wrap_content"
17         android:layout_marginTop="20dp"
18         android:textSize="20sp" />
19 
20 </LinearLayout>
main.xml
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.leaf.android"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6 
 7     <uses-sdk android:minSdkVersion="10" />
 8 
 9     <application
10         android:icon="@drawable/ic_launcher"
11         android:label="@string/app_name" >
12         <activity
13             android:label="@string/app_name"
14             android:name=".Main" >
15             <intent-filter >
16                 <action android:name="android.intent.action.MAIN" />
17 
18                 <category android:name="android.intent.category.LAUNCHER" />
19             </intent-filter>
20         </activity>
21         <activity android:name=".Activity1" >
22         </activity>
23         <activity android:name=".Activity2" >
24         </activity>
25     </application>
26 
27 </manifest>
AndroidMainfest.xml

效果:

textView中添加跑馬燈效果

 說到文本顯示,最多見的效果就是跑馬燈效果,這裏以一個例子展現跑馬燈的效果,在看代碼前,先講解一下等下會碰到的屬性:

  • android:elipsize: 若是文本長度大於TextView的顯示長度,則隱藏那一部分,可賦值爲:none(不隱藏)、start(隱藏開始)、middle(隱藏中間)、end(隱藏結束)、marquee(滾動效果)。
  • android:marqueRepeatLimit:設定須要重複動畫的次數,傳遞一個int值,-1爲無限循環。
  • android:focusable:是否容許得到焦點,傳遞一個bool值。
  • android:focusableInTouchMode:是否在得到焦點時對控件有聯繫,傳遞一個bool值。
 1 package com.leaf.android;
 2 
 3 import android.R.string;
 4 import android.app.Activity;
 5 import android.os.Bundle;
 6 import android.text.Html;
 7 import android.text.method.LinkMovementMethod;
 8 import android.widget.TextView;
 9 
10 public class Main extends Activity {
11     /** Called when the activity is first created. */
12     private TextView textView;
13 
14     @Override
15     public void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.main);
18         textView = (TextView) this.findViewById(R.id.textview);
19         String html = "框架佈局中的子視圖老是被繪製到相對於屏幕的左上角上。全部添加到這個佈局中的視圖都是以層疊的方式顯示。第一個添加到<a href = 'http://www.baidu.com'>框架佈局</a>中的視圖顯示在最底層";
20         CharSequence charSequence = Html.fromHtml(html);
21         textView.setText(charSequence);
22         textView.setMovementMethod(LinkMovementMethod.getInstance());
23 
24     }
25 }
Main.java
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:orientation="vertical" >
 6 
 7     <TextView
 8         android:id="@+id/textview"
 9         android:layout_width="fill_parent"
10         android:layout_height="wrap_content"
11         android:layout_margin="10dp"
12         android:background="#FFFFFF"
13         android:ellipsize="marquee"
14         android:focusable="true"
15         android:focusableInTouchMode="true"
16         android:marqueeRepeatLimit="marquee_forever"
17         android:padding="10dp"
18         android:singleLine="true"
19         android:textColor="#000"
20         android:textSize="20dp" />
21 
22 </LinearLayout>
main.xml

AutoCompleteTextView

  自動完成文本框(AutoCompleteTextView),能夠從官方文檔上看出,是從EditText繼承而來,因此它實際上也是一個文本編輯框,只是多了一個自動提示輸入補全的功能。功能相似於:當用戶輸入必定字符以後,自動完成文本框會顯示一個下拉列表,供用戶從中選擇,當用戶選擇某個菜單項以後,AutoCompleteTextView會按照的選擇自動填寫該文本框。

 

  經常使用屬性

  由於是繼承自EditText,因此AutoCompleteTextView除了可使提供用Edit的屬性和方法以外,還支持以下一些特殊的屬性及方法,這裏只介紹一些經常使用的,具體請參見官方文檔:

  • android:completionHint/setCompletionHint(CharSequence):設置出現下拉列表的提示標題。
  • android:completionTjreshold/setThreshold(int):設置至少輸入幾個字符纔會顯示提示。
  • android:dropDownHeight/setDropHeight(int):設置下拉列表的高度。
  • android:dropDownWidth/setDropWidth(int):設置下拉列表的寬度。
  • android:popupBackground/setDropDownbackgroundResource(int):設置下拉列表的背景。

 

  填充選擇數據

  在Android程序中,爲了展現數據,一般會用到一個Adapter的接口。沒錯,這是一個接口,是鏈接後端數據和前端顯示的橋樑,是data souce和UI(View)之間一個重要的紐帶。下圖展現了Adapter在Android程序中的關係:

  對於Adapter,它是一個接口,Android爲其聲明瞭各類實現類,對於在AutoCompleteTextView控件中,通常使用ArrayAdapter<T>便可完成功能,對於一些其餘實現類的應用場景,之後會慢慢介紹。

  ArrayAdapter<T>繼承自一個抽象類BaseAdapter,而這個抽象類實現了Adapter接口,因此繼承關係應該是:Adapter→BaseAdater→ArrayAdapter<T>。

  從名字上能夠看出,ArrayAdapter<T>是以一個數組的形式去存儲數據的,它也確實是這麼作的,而且能夠傳遞一個數組對其進行構造。因此咱們只須要填充一個數組對象,就完成ArrayAdapter對象的初始化工做,在把獲得的ArrayAdapter對象傳遞給AutoCompleteTextView控件,便可對其進行選擇數據設置。

MultiAutoCompleteTextView  

  既然講到了AutoCompleteTextView,那就順帶講一下MultiAutoCompleteTextView,它繼承自AutoCompleteTextView。新擴展的功能是:能夠進行屢次提示,而且每次指定完成的內容經過符號進行分隔顯示。使用MultiAutoCompleteTextView必須實現一個MultiAutoCompleteTextView.Tokenizer接口,用於聲明用於選項與選項之間分隔的符號,通常如不特殊指定,可使用Android爲咱們提供的實現類MultiAutoCompleteTextView.CommaTokenizer,它設定使用英文逗號","進行分隔選項。

 

 1 package com.leaf.android;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.view.View;
 6 import android.widget.ArrayAdapter;
 7 import android.widget.AutoCompleteTextView;
 8 import android.widget.MultiAutoCompleteTextView;
 9 
10 public class Main extends Activity {
11     /** Called when the activity is first created. */
12     private AutoCompleteTextView auto;
13     private MultiAutoCompleteTextView mul;
14 
15     @Override
16     public void onCreate(Bundle savedInstanceState) {
17         super.onCreate(savedInstanceState);
18         setContentView(R.layout.main);
19         auto = (AutoCompleteTextView) this.findViewById(R.id.autotext);
20         String[] autoStrings = new String[] { "Google", "Google map",
21                 "Google search", "baidu", "baidu map", "baidu music" };
22         // 定義數組適配器,第二個參數表示適配器的下拉風格
23         ArrayAdapter<String> adapter = new ArrayAdapter<String>(Main.this,
24                 android.R.layout.simple_dropdown_item_1line, autoStrings);
25         auto.setAdapter(adapter);
26         //找到自動完成組件
27 
28         mul = (MultiAutoCompleteTextView) this.findViewById(R.id.mul);
29         mul.setAdapter(adapter);//爲其設置適配器 mul.setTokenizer(new
30         mul.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());//完成對選項的拆分功能,以逗號進行拆分
31         
32 
33     }
34 }
Main.java
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:orientation="vertical" >
 6 
 7     <TextView
 8         android:layout_width="fill_parent"
 9         android:layout_height="wrap_content"
10         android:text="AutoCompleteTextView" />
11 
12     <AutoCompleteTextView
13         android:id="@+id/autotext"
14         android:layout_width="fill_parent"
15         android:layout_height="wrap_content" >
16     </AutoCompleteTextView>
17 
18     <TextView
19         android:layout_width="fill_parent"
20         android:layout_height="wrap_content"
21         android:text="MultiAutoCompleteTextView" />
22 
23     <MultiAutoCompleteTextView
24         android:id="@+id/mul"
25         android:layout_width="fill_parent"
26         android:layout_height="wrap_content" >
27     </MultiAutoCompleteTextView>
28 
29 </LinearLayout>
main.xm                         

 

相關文章
相關標籤/搜索