EditText的功能與用法

2022-02-23 06:10:50 字數 3742 閱讀 7760

edittext與textview非常相似,它甚至與textview共用了絕大部分xml屬性和方法。edittext和textview的最大區別在於:edittext可以接受使用者輸入。

edittext元件最重要的屬性是inputtype,該屬性相當於html的元素的type屬性,用於edittext為指定型別的輸入元件。inputtype能接受的屬性值非常豐富,而且隨著androd版本的公升級,該屬效能接受的型別還會增加。

edittext還派生了如下兩個類。

例項:使用者友好的輸入介面

對於乙個使用者友好的輸入介面而言,接受使用者輸入的文字框你缺省會提示使用者如何輸入;當使用者把焦點切換到輸入框時,輸入框自動選中其中已輸入的內容,避免使用者刪除已有內容;當使用者把焦點切換到只接受**號碼的輸入框時,輸入法自動切換到數字鍵盤。

下面程式的輸入介面完成了以上功能,輸入介面的介面布局如下。

<

tablelayout

xmlns:android

=""android:orientation

="vertical"

android:layout_width

="fill_parent"

android:layout_height

="fill_parent"

android:stretchcolumns

="1"

>

<

tablerow

>

<

textview

android:layout_width

="fill_parent"

android:layout_height

="wrap_content"

android:text

="使用者名稱:"

android:textsize

="16sp"

/>

<

edittext

android:layout_width

="fill_parent"

android:layout_height

="wrap_content"

android:hint="請填寫登入帳號"

android:selectallonfocus="true" />

tablerow

>

<

tablerow

>

<

textview

android:layout_width

="fill_parent"

android:layout_height

="wrap_content"

android:text

="密碼:"

android:textsize

="16sp"

/>

<

edittext

android:layout_width

="fill_parent"

android:layout_height

="wrap_content"

android:inputtype="numberpassword"/>

tablerow

>

<

tablerow

>

<

textview

android:layout_width

="fill_parent"

android:layout_height

="wrap_content"

android:text

="年齡:"

android:textsize

="16sp"

/>

<

edittext

android:layout_width

="fill_parent"

android:layout_height

="wrap_content"

android:inputtype="number"/>

tablerow

>

<

tablerow

>

<

textview

android:layout_width

="fill_parent"

android:layout_height

="wrap_content"

android:text

="生日:"

android:textsize

="16sp"

/>

<

edittext

android:layout_width

="fill_parent"

android:layout_height

="wrap_content"

android:inputtype="date"/>

tablerow

>

<

tablerow

>

<

textview

android:layout_width

="fill_parent"

android:layout_height

="wrap_content"

android:text

="**號碼:"

android:textsize

="16sp"

/>

<

edittext

android:layout_width

="fill_parent"

android:layout_height

="wrap_content"

android:hint

="請填寫您的**號碼"

android:selectallonfocus="true"

android:inputtype="phone" />

tablerow

>

<

button

android:layout_width

="wrap_content"

android:layout_height

="wrap_content"

android:text

="註冊"

/>

tablelayout

>

上面的介面布局中第乙個文字框通過android:hint指定了文字框的提示資訊:請填寫登入賬號——這是該文字框預設的提示。當使用者還沒輸入時,該文字框內預設顯示這段資訊;

第二個文字框通過android:inputtype="numberpassword"設定這是乙個密碼框,而且只能接受數字密碼,使用者在該文字框輸入的字元會以點號代替;第三個輸入框通過android:inputtype="number"設定為只能接受數值的輸入框;第四個輸入框通過android:inputtype="date"指定它是乙個日期輸入框;第5個輸入框通過android:inputtype=「phone」設定為乙個**號碼輸入框。

使用activity顯示上面的介面布局將可以看到如圖2.19所示的介面。

圖2.19 友好的輸入介面

EditText的功能與用法

edittext與textview 非常相似,它甚至與textview 共用了絕大部分xml屬性與方法。edittext 與 textview的最大區別在於 edittext 可以接受使用者輸入。edittext元件最重要的屬性是inputtype,該屬性相當於html的元素的type屬性,用於將e...

session cookie 功能與用法

session 1.儲存在伺服器,每個人存乙份 2.可以儲存任何型別的資料 3.有乙個預設過期的時間 相對安全 但是對伺服器造成的壓力大 注意 在所有使用session的頁面最頂端要開啟session session start session start 開啟session 儲存 session ...

git diff 的功能與用法

在git提交環節,存在三大部分 working tree,index file,commit 這三大部分中 working tree 就是你所工作在的目錄,每當你在 中進行了修改,working tree的狀態就改變了。index file 是索引檔案,它是連線working tree和commit...