第五章 用戶界面基礎(chǔ)(EditText)

參考資料:
《Android應(yīng)用程序開發(fā)》ISBN 9787302283164
參考軟件:
Android Studio、Eclipse+ADT、Android SDK、JDK
用戶界面基礎(chǔ)(接上節(jié)內(nèi)容)
二、edittext
常用屬性
android:layout_gravity="center_vertical"//設(shè)置控件顯示的位置:默認(rèn)top,這里居中顯示,還有bottom
android:hint="請(qǐng)輸入數(shù)字!"//設(shè)置顯示在空間上的提示信息
android:numeric="integer"//設(shè)置只能輸入整數(shù),如果是小數(shù)則是:decimal
android:singleLine="true"//設(shè)置單行輸入,一旦設(shè)置為true,則文字不會(huì)自動(dòng)換行。
android:gray="top"?//多行中指針在第一行第一位置et.setSelection(et.length());//調(diào)整光標(biāo)到最后一行
android:autoText?//自動(dòng)拼寫幫助
android:capitalize?//首字母大寫
android:digits?//設(shè)置只接受某些數(shù)字
android:singleLine?//是否單行或者多行,回車是離開文本框還是文本框增加新行
android:numeric?//只接受數(shù)字
android:password?//密碼
android:phoneNumber?//?輸入電話號(hào)碼
android:editable?//是否可編輯
android:autoLink=”all”?//設(shè)置文本超鏈接樣式當(dāng)點(diǎn)擊網(wǎng)址時(shí),跳向該網(wǎng)址
android:password="true"//設(shè)置只能輸入密碼
android:textColor?=?"#ff8c00"//字體顏色
android:textStyle="bold"//字體,bold,?italic,?bolditalic
android:textSize="20dip"//大小
android:capitalize?=?"characters"//以大寫字母寫
android:textAlign="center"//EditText沒有這個(gè)屬性,但TextView有
android:textColorHighlight="#cccccc"//被選中文字的底色,默認(rèn)為藍(lán)色
android:textColorHint="#ffff00"//設(shè)置提示信息文字的顏色,默認(rèn)為灰色
android:textScaleX="1.5"//控制字與字之間的間距
android:typeface="monospace"//字型,normal,?sans,?serif,?monospace
android:background="@null"//空間背景,這里沒有,指透明
android:layout_weight="1"//權(quán)重?在控制控???件顯示的大小時(shí)蠻有用的。
android:textAppearance="?android:attr/textAppearanceLargeInverse"//文字外觀,這里引用的是系統(tǒng)自帶的一個(gè)外觀,?表示系統(tǒng)是否有這種外觀,否則使用默認(rèn)的外觀。
常用代碼
1、代碼示例

?<TableLayout ?xmlns:android="http://schemas.android.com/apk/res/android"
? ? ?android:layout_width="match_parent"
? ? ?android:layout_height="match_parent"
? ? ?android:orientation="vertical"
? ? ?android:stretchColumns="1" >
? ? <TableRow ?>
? ? ? ? ?<TextView
? ? ? ? ? ? ?android:id="@+id/name_text"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? ?android:layout_height="wrap_content"
? ? ? ? ? ? ?android:text="姓名:"/>
? ? ? ? ?<EditText
? ? ? ? ? ? ?android:id="@+id/name_edit"
? ? ? ? ? ? ?android:layout_height="wrap_content"
? ? ? ? ? ? ?android:layout_width="wrap_content"
? ? ? ? ? ? ?android:selectAllOnFocus="true"
? ? ? ? ? ? android:hint="請(qǐng)輸入您的姓名"/>
? ? ?</TableRow>
? ? <TableRow ?>
? ? ? ? ?<TextView
? ? ? ? ? ? ?android:id="@+id/code_text"
? ? ? ? ? ? ?android:layout_width="wrap_content"
? ? ? ? ? ? ?android:layout_height="wrap_content"
? ? ? ? ? ? ?android:text="密碼:"/>
? ? ? ? ?<EditText
? ? ? ? ? ? ?android:id="@+id/code_edit"
? ? ? ? ? ? ?android:layout_height="wrap_content"
? ? ? ? ? ? ?android:layout_width="wrap_content"
? ? ? ? ? ? ?android:inputType="numberPassword" />
? ? ?</TableRow>
? ? <TableRow ?>
? ? ? ? <TextView ?
? ? ? ? ? ? ?android:id="@+id/addr_text"
? ? ? ? ? ? ?android:layout_width="wrap_content"
? ? ? ? ? ? ?android:layout_height="wrap_content"
? ? ? ? ? ? android:text="年齡:"/>
? ? ? ? ?<EditText
? ? ? ? ? ? ?android:id="@+id/addr_edit"
? ? ? ? ? ? ?android:layout_height="wrap_content"
? ? ? ? ? ? ?android:layout_width="wrap_content"
? ? ? ? ? ? ?android:inputType="number" />
? ? ?</TableRow>
? ? <TableRow ?>
? ? ? ? ?<TextView
? ? ? ? ? ? ?android:id="@+id/birth_text"
? ? ? ? ? ? ?android:layout_width="wrap_content"
? ? ? ? ? ? ?android:layout_height="wrap_content"
? ? ? ? ? ? ?android:text="生日:"/>
? ? ? ? ?<EditText
? ? ? ? ? ? ?android:id="@+id/birth_edit"
? ? ? ? ? ? ?android:layout_height="wrap_content"
? ? ? ? ? ? ?android:layout_width="wrap_content"
? ? ? ? ? ? ?android:inputType="date" />
? ? ?</TableRow>
? ? <TableRow ?>
? ? ? ? ?<TextView
? ? ? ? ? ? ?android:id="@+id/phone_text"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? ?android:layout_height="wrap_content"
? ? ? ? ? ? ?android:text="TEL:"/>
? ? ? ? ?<EditText
? ? ? ? ? ? ?android:id="@+id/phone_edit"
? ? ? ? ? ? ?android:layout_height="wrap_content"
? ? ? ? ? ? ?android:layout_width="wrap_content"
? ? ? ? ? ? ?android:selectAllOnFocus="true"
? ? ? ? ? ? android:hint="請(qǐng)輸入您的電話號(hào)碼"
? ? ? ? ? ? ?android:inputType="phone" />
? ? ?</TableRow>
? ? <Button
? ? ? ? ?android:id="@+id/press_ok"
? ? ? ? ?android:layout_height="wrap_content"
? ? ? ? ?android:layout_width="wrap_content"
? ? ? ? android:text="確認(rèn)輸入"/>
</TableLayout>
2、添加圖片主要用SpannableString和ImageSpan類
Drawable drawable = ?getResources().getDrawable(id);?
??????? drawable.setBounds(0, 0, ?drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());?
??????? //需要處理的文本,[smile]是需要被替代的文本?
??????? SpannableString spannable = new ?SpannableString(getText().toString()+"[smile]");?
??????? //要讓圖片替代指定的文字就要用ImageSpan?
??????? ImageSpan span = new ?ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);?
??????? //開始替換,注意第2和第3個(gè)參數(shù)表示從哪里開始替換到哪里替換結(jié)束(start和end)?
??? ???//最后一個(gè)參數(shù)類似數(shù)學(xué)中的集合,[5,12)表示從5到12,包括5但不包括12 ??
??????? spannable.setSpan(span, ?getText().length(),getText().length()+"[smile]".length(), ?Spannable.SPAN_INCLUSIVE_EXCLUSIVE); ? ??
??????? setText(spannable);
3、將需要的文字高亮顯示
public void highlight(int start,int end){
? ? ? ? SpannableStringBuilder spannable=new SpannableStringBuilder(getText().toString());//用于可變字符串
? ? ? ? ForegroundColorSpan span=new ForegroundColorSpan(Color.RED);
? ? ? ? spannable.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
? ? ? ? setText(spannable);
? ? } ?
4、加下劃線:
public?void?underline(int?start,int?end){
????????SpannableStringBuilder?spannable=new?SpannableStringBuilder(getText().toString());
????????CharacterStyle?span=new?UnderlineSpan();
????????spannable.setSpan(span,?start,?end,?Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
????????setText(spannable);
????}
5、長度和空白提示文字,提示文字顏色,是否可編輯等
EditText有一些屬性可以設(shè)置EditText的特性,比如最大長度,空白提示文字等。
1.????有時(shí)候我們有一些特屬的需要,要求只能在EditText中輸入特定個(gè)數(shù)的字符,比如身份證號(hào)、手機(jī)號(hào)嗎等。這時(shí)候就可以通過android:maxLength屬性來設(shè)置最大輸入字符個(gè)數(shù),比如android:maxLength=“4”就表示最多能輸入4個(gè)字符,再多了就輸入不進(jìn)去了。
2.????空白提示文字。有時(shí)候我們需要說明你定義的這個(gè)EditText是做什么用的,比如讓輸入“用戶名”,或者輸入“電話號(hào)碼”等,但是你又不想在EditText前面加一個(gè)TextView來說明這是輸入“用戶名”的,因?yàn)檫@會(huì)使用一個(gè)TextView,那么怎么辦呢?EditText為我們提供了android:hint來設(shè)置當(dāng)EditText內(nèi)容為空時(shí)顯示的文本,這個(gè)文本只在EditText為空時(shí)顯示,你輸入字符的時(shí)候就消失了,不影響你的EditText的文本。。修改main.xml如下:?
Xml代碼??

1.?<?xml?version="1.0"?encoding="utf-8"?>??
2.?<LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
3.?????android:orientation="vertical"??
4.?????android:layout_width="fill_parent"??
5.?????android:layout_height="fill_parent"??
6.?????>??
7.?<EditText??
8.?????android:id="@+id/edit_text"????
9.?????android:layout_width="fill_parent"???
10.????android:layout_height="wrap_content"??
11.????android:maxLength="40"??
12.????android:hint="請(qǐng)輸入用戶名..."/>??
13.</LinearLayout>??
?運(yùn)行應(yīng)用就會(huì)看到如下的效果:?

?看看吧,簡潔明了還不用新增一個(gè)TextView說明,也不影響用戶操作。3.????上面列出了空白時(shí)的提示文字,有的人說了,我不想要這個(gè)灰色的提示文字,和我的應(yīng)用整體風(fēng)格不協(xié)調(diào),那也行啊,我們可以換顏色,怎么換呢,就是通過android:textColorHint屬性設(shè)置你想要的顏色。修改main.xml如下:
Xml代碼??

1.?<?xml?version="1.0"?encoding="utf-8"?>??
2.?<LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
3.?????android:orientation="vertical"??
4.?????android:layout_width="fill_parent"??
5.?????android:layout_height="fill_parent"??
6.?????>??
7.?<EditText??
8.?????android:id="@+id/edit_text"????
9.?????android:layout_width="fill_parent"???
10.????android:layout_height="wrap_content"??
11.????android:maxLength="40"??
12.????android:hint="請(qǐng)輸入用戶名..."??
13.????android:textColorHint="#238745"/>??
14.</LinearLayout>??
?運(yùn)行程序效果如下:?

?看到了吧,顏色已經(jīng)變了。。4.????還有一個(gè)比較實(shí)用的功能,就是設(shè)置EditText的不可編輯。設(shè)置android:enabled="false"可以實(shí)現(xiàn)不可編輯,可以獲得焦點(diǎn)。這時(shí)候我們看到EditText和一個(gè)TextView差不多:?

?5.????實(shí)現(xiàn)類似html中Textarea的文本域。在Android中沒有專門的文本域組件,但是可以通過設(shè)置EditText的高來實(shí)現(xiàn)同樣的文本域功能。修改main.xml如下:?
Xml代碼??

1.?<?xml?version="1.0"?encoding="utf-8"?>??
2.?<LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
3.?????android:orientation="vertical"??
4.?????android:layout_width="fill_parent"??
5.?????android:layout_height="fill_parent"??
6.?????>??
7.?<EditText??
8.?????android:id="@+id/edit_text"????
9.?????android:layout_width="fill_parent"???
10.????android:layout_height="200dip"/>??
11.</LinearLayout>??
?運(yùn)行程序效果如下:?

6、輸入特殊格式的字符
在我們開發(fā)程序的時(shí)候不免會(huì)輸入一些特屬個(gè)數(shù)的字符,比如密碼(輸入框的字符要加密顯示),電話號(hào)碼(比如數(shù)字和-),數(shù)字等,這些都算是一些特屬格式的字符,強(qiáng)大的EditText同樣為我們提供了輸入這些特屬格式字符的設(shè)置。
1.????密碼文本框。密碼輸入也是Android應(yīng)用常用的功能,通過配置EditText的android:password="true"就可以實(shí)現(xiàn)這一密碼輸入功能,修改main.xml如下:?
Xml代碼??

1.?<?xml?version="1.0"?encoding="utf-8"?>??
2.?<LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
3.?????android:orientation="vertical"??
4.?????android:layout_width="fill_parent"??
5.?????android:layout_height="fill_parent"??
6.?????>??
7.?<EditText??
8.?????android:id="@+id/edit_text"????
9.?????android:layout_width="fill_parent"???
10.????android:layout_height="wrap_content"??
11.????android:password="true"/>??
12.</LinearLayout>??
?運(yùn)行效果如下:?

?可以看到我們輸入的字符已經(jīng)被“.”這樣的掩碼所代替。2.????手機(jī)中發(fā)短信打電話是必不可少的,所以用于專門輸入電話號(hào)碼的文本框也是大有用途,有了他我們對(duì)是否是電話號(hào)碼的校驗(yàn)就容易的多了(因?yàn)樽址钦_的,只要校驗(yàn)格式?).通過設(shè)置android:phoneNumber="true"就可以把EditText變成只接受電話號(hào)碼輸入的文本框,連軟鍵盤都已經(jīng)變成撥號(hào)專用軟鍵盤了,所以不用再擔(dān)心輸入其他字符了。修改main.xml如下:?
Xml代碼??

1.?<?xml?version="1.0"?encoding="utf-8"?>??
2.?<LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
3.?????android:orientation="vertical"??
4.?????android:layout_width="fill_parent"??
5.?????android:layout_height="fill_parent"??
6.?????>??
7.?<EditText??
8.?????android:id="@+id/edit_text"????
9.?????android:layout_width="fill_parent"???
10.????android:layout_height="wrap_content"??
11.????android:phoneNumber="true"/>??
12.</LinearLayout>??
?運(yùn)行程序效果如下:?

?注意看軟鍵盤,已經(jīng)變成撥號(hào)專用的啦.3.????有時(shí)候我們只想輸入數(shù)字,不想輸入字母,EditText為我們提供了android:numeric來控制輸入的數(shù)字類型,一共有三種分別為integer(正整數(shù))、signed(帶符號(hào)整數(shù))和decimal(浮點(diǎn)數(shù))。這里以signed類型的為例,修改main.xml如下:?
Xml代碼??

1.?<?xml?version="1.0"?encoding="utf-8"?>??
2.?<LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
3.?????android:orientation="vertical"??
4.?????android:layout_width="fill_parent"??
5.?????android:layout_height="fill_parent"??
6.?????>??
7.?<EditText??
8.?????android:id="@+id/edit_text"????
9.?????android:layout_width="fill_parent"???
10.????android:layout_height="wrap_content"??
11.????android:numeric="signed"/>??
12.</LinearLayout>??
?運(yùn)行效果如下:?

?注意這里的軟鍵盤變成“數(shù)字鍵盤”的變化.
7、為文本指定特定的軟鍵盤類型
前面我們通過指定為電話號(hào)碼特定格式,然后鍵盤類型變成了撥號(hào)專用的鍵盤,這個(gè)是自動(dòng)變的,其實(shí)我們也可以通過android:inputType來設(shè)置文本的類型,讓輸入法選擇合適的軟鍵盤的。。android:inputType有很多類型,這里使用date類型來演示,修改main.xml如下:?
Xml代碼??

1.?<?xml?version="1.0"?encoding="utf-8"?>??
2.?<LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
3.?????android:orientation="vertical"??
4.?????android:layout_width="fill_parent"??
5.?????android:layout_height="fill_parent"??
6.?????>??
7.?<EditText??
8.?????android:id="@+id/edit_text"????
9.?????android:layout_width="fill_parent"???
10.????android:layout_height="wrap_content"??
11.????android:inputType="date"/>??
12.</LinearLayout>??
??運(yùn)行效果如下:?

?
8、Enter鍵圖標(biāo)的設(shè)置
軟鍵盤的Enter鍵默認(rèn)顯示的是“完成”文本,我們知道按Enter建表示前置工作已經(jīng)準(zhǔn)備完畢了,要去什么什么啦。比如,在一個(gè)搜索中,我們輸入要搜索的文本,然后按Enter表示要去搜索了,但是默認(rèn)的Enter鍵顯示的是“完成”文本,看著不太合適,不符合搜索的語義,如果能顯示“搜索”兩個(gè)字或者顯示一個(gè)表示搜索的圖標(biāo)多好。事實(shí)證明我們的想法是合理的,Android也為我們提供的這樣的功能。通過設(shè)置android:imeOptions來改變默認(rèn)的“完成”文本。這里舉幾個(gè)常用的常量值:
1.????actionUnspecified?未指定,對(duì)應(yīng)常量EditorInfo.IME_ACTION_UNSPECIFIED.效果:

2.????actionNone?沒有動(dòng)作,對(duì)應(yīng)常量EditorInfo.IME_ACTION_NONE?效果:

3.????actionGo?去往,對(duì)應(yīng)常量EditorInfo.IME_ACTION_GO?效果:

4.????actionSearch?搜索,對(duì)應(yīng)常量EditorInfo.IME_ACTION_SEARCH?效果:?

5.????actionSend?發(fā)送,對(duì)應(yīng)常量EditorInfo.IME_ACTION_SEND?效果:

6.????actionNext?下一個(gè),對(duì)應(yīng)常量EditorInfo.IME_ACTION_NEXT?效果:

7.????actionDone?完成,對(duì)應(yīng)常量EditorInfo.IME_ACTION_DONE?效果:

?下面已搜索為例,演示一個(gè)實(shí)例,修改main.xml如下:
Xml代碼??

1.?<?xml?version="1.0"?encoding="utf-8"?>??
2.?<LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
3.?????android:orientation="vertical"??
4.?????android:layout_width="fill_parent"??
5.?????android:layout_height="fill_parent"??
6.?????>??
7.?<EditText??
8.?????android:id="@+id/edit_text"????
9.?????android:layout_width="fill_parent"???
10.????android:layout_height="wrap_content"??
11.????android:imeOptions="actionSearch"/>??
12.</LinearLayout>??
??修改HelloEditText如下:
Java代碼??

1.?package?com.flysnow;??
2.???
3.?import?android.app.Activity;??
4.?import?android.os.Bundle;??
5.?import?android.view.KeyEvent;??
6.?import?android.widget.EditText;??
7.?import?android.widget.TextView;??
8.?import?android.widget.Toast;??
9.?import?android.widget.TextView.OnEditorActionListener;??
10.??
11.public?class?HelloEditText?extends?Activity?{??
12.????/**?Called?when?the?activity?is?first?created.?*/??
13.????@Override??
14.????public?void?onCreate(Bundle?savedInstanceState)?{??
15.????????super.onCreate(savedInstanceState);??
16.????????setContentView(R.layout.main);??
17.????????EditText?editText=(EditText)findViewById(R.id.edit_text);??
18.????????editText.setOnEditorActionListener(new?OnEditorActionListener()?{??
19.????????????@Override??
20.????????????public?boolean?onEditorAction(TextView?v,?int?actionId,?KeyEvent?event)?{??
21.????????????????Toast.makeText(HelloEditText.this,?String.valueOf(actionId),?Toast.LENGTH_SHORT).show();??
22.????????????????return?false;??
23.????????????}??
24.????????});??
25.????}??
26.}??
?運(yùn)行程序,點(diǎn)擊回車(也就是搜索圖標(biāo)軟鍵盤按鈕)會(huì)顯示該actionId.我們上面的每一個(gè)設(shè)置都會(huì)對(duì)應(yīng)一個(gè)常量,這里的actionId就是那個(gè)常量值。?
