TextView(文本框)是Android系統中最常見的控件之一,使用TextView可生成一段文本文字,合理使用TextView的屬性還能使文字變得有姿有色。
TextView控件可以通過XML文件設置全部屬性,也可以通過Java代碼設置屬性。
java代碼:
//獲得TextView控件 TextView myText = (TextView) findViewById(R.id.myText); //調用set方法設置屬性 myText.setTextColor(Color.BLUE);//設置文件的顏色為藍色 myText.setTextSize(25);//設置文本的字體大小為25dp
XML代碼:
<TextView android:id="@+id/myText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello_world" android:textColor="#00f" android:textSize="20dp"/>
EditView是Android系統中的編輯框,可以理解為可編輯的TextView,用法和屬性都和TextView相似。
java代碼:
EditText myEdit = (EditText)findViewById(R.id.myEdit); myEdit.setHint("請輸入文字"); myEdit.setCursorVisible(true); myEdit.setTextColor(Color.BLUE);
XML代碼:
<EditText android:id="@+id/myEdit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="" />