Android開發之路七----UI組件2
今天我們繼續學習UI組件,主要是學習了TextView和EditText這兩種組件
TextView組件介紹:
直接的子類:
Button、CheckdTextView、Cheronometer、DigitalClock、EditText
間接的子類:
AutoCompleteTextView、CheckBox、CommpoundButton、MultiAutoCompleteTextView
在其xml檔案中有許多的屬性今天講到例如:
Android:autoLink:這是設定文本為URL連結/email/電話號碼時,文本顯示為可點擊的連結,可選的值為(none/web/email/phone/map這屬性今天沒講/all)
Android:autoText:這是如果設定,將自動執行輸入值的拼字糾正,在顯示IME並輸入的時候起作用。
Android:drawableButton:這是在text中的下方輸入一個drawable,圖片,如果指定一個顏色的話會把text的背景顏色設為該顏色,並且同時和background使用時覆蓋後者。
Android:gravity:設定文本位置,可以設定成“left”“center”“right”對應的分別是左,置中,右。
Android:inputType 設定文本的類型
..................................
下面就介紹一下用TextView組件來實現程式這是三個不同的Helloword,不同之處就是利用了TextView中的標籤
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
//原始的helloword
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#00FF00"
android:padding="30dp"
android:textSize="30dp"
android:layout_margin="50dp"
android:text="@string/hello" />
//加上顏色--textcolor和大小--textSize
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:gravity="center"
/>
//設定了文本的置中格式
android:gravity="center"
</LinearLayout>
運行後形成的介面就是這個樣子的
、
這就是利用標籤形成的文本的不同效果。
為文本添加背景圖片
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_margin="10dp"
android:textColor="#cccccc"
android:text="@string/hello"
android:background="@drawable/ic_launcher"
/>
利用上面的這段代碼就可以實現為你的文本添加背景圖片,雖然這是添加背景圖片但是這可能會使圖片失真,隨意的展開圖片。這就要求我們很好的掌握photoshop的功夫,要把圖片p好,那樣就不會失真。
為文本加連結
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="web"
android:text="@string/webUrl"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="email"
android:text="@string/email"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="phone"
android:text="@string/phoneNumber"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="all"
android:text="@string/autoAll"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textview"
/>
所形成的介面是這個樣子的
所有的連結就加上了。
自訂帶邊框的TextView
public class BorderTextView extends TextView {
public BorderTextView(Context context, AttributeSet attr) {
super(context,attr);
}
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(android.graphics.Color.GREEN);
canvas.drawLine(0, 0, this.getWidth() - 1, 0, paint);
canvas.drawLine(0, 0, 0, this.getHeight() - 1, paint);
canvas.drawLine(this.getWidth() - 1, 0, this.getWidth() - 1,
this.getHeight() - 1, paint);
canvas.drawLine(0, this.getHeight() - 1, this.getWidth() - 1,
this.getHeight() - 1, paint);
}
}
<cn.class3g.activity.BorderTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="30dp"
android:text="xxxxxxxxxxxxx"
/>
EditText組件的應用
EditText是TextView的子類,繼承了TextView的大部分xml屬性,所以用法與TextView的用法大致一致。
其中一些特定的字串
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:password="true"
android:digits="01234" />
//這是設定文本可以允許那些字元可以輸入,上面的命令就是就可以在01234中的字元可以輸進文本編輯中
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:digits="abcd" />
//這是設定文本可以允許那些字元可以輸入,上面的命令就是就可以在abcd中的字元可以輸進文本編輯中
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:password="true"
/>
//這是在文字框中密碼的設定,只要輸上字元,會以原點的形式顯示在介面
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
/>
//郵箱的設定,在打上字元,會有@在編輯的地方來提示
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number"
/>
//必須是數位字元
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numeric="decimal|signed" />
//顯示文字框
為EditText對象的註冊OnKeyListener事件,實現onKey()方法
<EditText
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="text1" />
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="Button" />
public boolean onKey(View view, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
btn.setText(et.getText());
et.setVisibility(View.GONE);
btn.setVisibility(View.VISIBLE);
}
return true;
}
按下斷行符號鍵之後就會成這樣