想要顯示一些文字內容可以使用 TextView ,這個類別提供相當多的方法讓你可以自由改變顯示的格式,如顏色,尺寸,位置,字型等等,相關的方法請參考 Android API ,範例如下
MainActivity.java
   1:  package com.example.helloworld;
   2:   
   3:  import android.app.Activity;
   4:  import android.os.Bundle;
   5:  import android.widget.TextView;
   6:   
7: public class MainActivity extends Activity {
   8:      
9: TextView tv1 = null;// make a TextView reference
  10:      
  11:      @Override
12: public void onCreate(Bundle savedInstanceState) {
  13:          
  14:          super.onCreate(savedInstanceState);
  15:          setContentView(R.layout.activity_main);
  16:          
17: tv1 = (TextView)findViewById(R.id.textView1); // get xml resource (R.id.textView1)
18: tv1.setText("測試文字 Vulpes Z.G.H");// change text
  19:          
  20:      }
  21:      
  22:  }
第 9 行為了方便修改文字內容,建立 tv1 物件
第 17 行取得在 xml 定義的 TextView,注意 findViewById(R.id.textView1) 回傳為 View 物件, 由於 TextView 為 View 的子類別,所以必須強制轉型
第 18 行修改文字內容為 測試文字 Vulpes Z.G.H
通常做為版權宣告流程也是相當方便的,以下範例我把版權聲明加入
MainActivity.java
   1:  package com.example.helloworld;
   2:   
   3:  import android.app.Activity;
   4:  import android.graphics.Typeface;
   5:  import android.os.Bundle;
   6:  import android.widget.TextView;
   7:   
8: public class MainActivity extends Activity {
   9:      
10: TextView tv1 = null;// make a TextView reference
  11:      
  12:      @Override
13: public void onCreate(Bundle savedInstanceState) {
  14:          
  15:          super.onCreate(savedInstanceState);
  16:          setContentView(R.layout.activity_main);
  17:          
18: tv1 = (TextView)findViewById(R.id.textView1); // get xml resource (R.id.textView1)
19: // tv1.setTextColor(Color.RED); // set color
20: tv1.setTextColor(0xff888888); // set color
21: tv1.setTextSize(18.0f); // set size
22: tv1.setTypeface(Typeface.SERIF); // set typeface
23: tv1.setText(R.string.copy_right);// change text
  24:          
  25:      }
  26:      
  27:  }
第 20 行設定 text 顏色,你可以使用 Color 類別中已經定義好的顏色,也可以自己指定色碼
第 21 行設定 text 尺寸,參數為 float
第 22 行設定 text 字型,有 3 種可選
第 23 行設定 text 內容,其中 R.string.copy_right 為 xml 已經定義好的字串,你也可以在程式碼中加入想要顯示的字串內容
結果為
上面的範例是使用 xml 中已經定義好的 Layout 建立, 你也可以改為在程式碼中建立 TextView,如下
MainActivity.java
   1:  package com.example.helloworld;
   2:   
   3:  import android.app.Activity;
   4:  import android.content.Context;
   5:  import android.graphics.Typeface;
   6:  import android.os.Bundle;
   7:  import android.view.Gravity;
   8:  import android.widget.TextView;
   9:   
10: public class MainActivity extends Activity {
  11:      
  12:      @Override
13: public void onCreate(Bundle savedInstanceState) {
  14:          
  15:          super.onCreate(savedInstanceState);
16: // setContentView(R.layout.activity_main);
  17:          
18: TestTextView ttv = new TestTextView(this);
  19:          
20: ttv.setTextColor(0xff888888); // set color
21: ttv.setTextSize(18.0f); // set size
22: ttv.setTypeface(Typeface.SERIF);// set typeface
23: ttv.setText(R.string.copy_right);// change text
  24:          ttv.setGravity(Gravity.CENTER_VERTICAL);
  25:          setContentView(ttv);
  26:          
  27:      }
  28:      
  29:    
  30:  }
  31:   
  32:   
33: class TestTextView extends TextView{
  34:   
35: public TestTextView(Context context) {
  36:          super(context);
37: // TODO Auto-generated constructor stub
  38:      }
  39:      
  40:  }
為了方便說明我在第 33 ~ 40 行建立一個 TestTextView , 並在 18 行建立該類別的物件
第 16 行不用 xml 中的佈局,接著第 20 ~ 24 行就如同原本的程式碼內容一樣
第 25 行套用 ttv 物件
當然在 TextView 類別中還有 20~ 30 種方法,足夠處理大部分顯示文字的情況,比如說 length( )取得文字長度,由於 TextView 繼承 View ,所以你也能覆寫 onTouchEvent( ) 方法來偵測觸碰事件,以下範例,在用手指拖曳螢幕,版權畫面就會閃爍
34: class TestTextView extends TextView{
  35:   
36: public TestTextView(Context context) {
  37:          super(context);
38: // TODO Auto-generated constructor stub
  39:      }
  40:      
41: public boolean onTouchEvent(MotionEvent event){
  42:          
43: if(getAlpha()>0)
  44:              setAlpha(getAlpha()-0.1f);
45: else
  46:              setAlpha(1.0f);
  47:              
48: return true;
  49:      }
  50:      
  51:  }
第 40 行覆寫 onTouchEvent(MotionEvent event) 以取得觸碰螢幕事件,在裡面直接修改 Alpha 值,該值控制物件的顯示程度
結果就像這樣
關於 TextView 的小技巧,
限制字數
android:maxEms="4"
單行顯示
android:singleLine="true"





 Posted in:  
0 意見:
張貼留言