EditText屬性詳解

Android開發(fā)之EditText屬性詳解

1、EditText輸入的文字為密碼形式的設(shè)置

(1)通過.xml里設(shè)置:

把該EditText設(shè)為:android:password="true" // 以”.”形式顯示文本

(2)在代碼里設(shè)置:

通過設(shè)置EditText的setTransformationMethod()方法來實現(xiàn)隱藏密碼或這顯示密碼。

editText.setTransformationMethod(PasswordTransformationMethod.getInstance());//設(shè)置密碼為不可見。

2、(1)EditText輸入的文字為電話號碼

Android:phoneNumber=”true”? //輸入電話號碼

3、EditText字?jǐn)?shù)限制的設(shè)置

(1)在.xml中設(shè)置:android:maxLength=“50”

(2)代碼中設(shè)置:?

editText.setFilters(new InputFilter[]{newInputFilter.LengthFilter(100)});

4、EditText設(shè)置字體

android:typeface="monospace" //設(shè)置字型。字形有:normal, sans, serif,monospace

5、EditText是否可編輯

Android:editable // 是否可編輯

6、在EditText中軟鍵盤的調(diào)起、關(guān)閉

(1)EditText有焦點(focusable為true)阻止輸入法彈出

editText=(EditText)findViewById(R.id.txtBody);

? editText.setOnTouchListener(new OnTouchListener(){?

? ? ? ? public boolean onTouch(View v, MotionEvent event){

? ? ? ? ? ? editText.setInputType(InputType.TYPE_NULL); //關(guān)閉軟鍵盤? ?

? ? ? ? ? ? return false;

? ? ? ? }

? ? });

(2)當(dāng)EidtText無焦點(focusable=false)時阻止輸入法彈出

InputMethodManager imm =

(InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);

imm.hideSoftInputFromWindow(editText.getWindowToken(),0);

(3)調(diào)用數(shù)字鍵盤并設(shè)置輸入類型和鍵盤為英文

etNumber.setInputType(InputType.TYPE_CLASS_NUMBER); //調(diào)用數(shù)字鍵盤

rlEditText.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);//設(shè)置輸入類型和鍵盤為英文 或者:android:inputType="textUri|textMultiLine"

(4)android:focusable="false"http://鍵盤永遠不會彈出

<activity android:name=".AddLinkman"android:windowSoftInputMode="adjustUnspecified|stateHidden"/>//不自動彈出鍵盤

//關(guān)閉鍵盤(比如輸入結(jié)束后執(zhí)行) InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(etEditText.getWindowToken(), 0);

//自動彈出鍵盤

((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).toggleSoftInput(0,InputMethodManager.HIDE_NOT_ALWAYS);

etEditText.requestFocus();//讓EditText獲得焦點,但是獲得焦點并不會自動彈出鍵盤

7、android:layout_gravity和android:gravity的區(qū)別

(1)android:layout_gravity是本元素對父元素的重力方向。

(2)android:gravity是本元素所有子元素的重力方向。

8、android:padding和android:layout_margin區(qū)別

這兩個都可以設(shè)置邊距,但有細微的區(qū)別:

(1)android:padding是相對父view的邊距

(2)android:layout_margin是相對同一級View的邊距

例:LinearLayout是水平布局,下面有兩個按鈕,

(a)如果右邊的按鈕想距左邊的按鈕15px,因為這兩個按鈕是同一級的,應(yīng)該用android:layout_margin;

(b)如果右邊的按鈕想距左邊的距離為350px,應(yīng)該用android:padding

9、android:numeric//只接受數(shù)字

android:numeric來控制輸入的數(shù)字類型,一共有三種分別為integer(正整數(shù))、signed(帶符號整數(shù),有正負)和decimal(浮點數(shù))。

10、Enter鍵圖標(biāo)的設(shè)置

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

(1)actionUnspecified未指定,對應(yīng)常量EditorInfo.IME_ACTION_UNSPECIFIED效果:

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

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

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

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

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

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

11、使用android:imeOptinos可對Android自帶的軟鍵盤進行一些界面上的設(shè)置:

android:imeOptions="flagNoExtractUi" //使軟鍵盤不全屏顯示,只占用一部分屏幕 同時,這個屬性還能控件軟鍵盤右下角按鍵的顯示內(nèi)容,默認(rèn)情況下為回車鍵 android:imeOptions="actionNone" //輸入框右側(cè)不帶任何提示 android:imeOptions="actionGo"? //右下角按鍵內(nèi)容為'開始' android:imeOptions="actionSearch" //右下角按鍵為放大鏡圖片,搜索 android:imeOptions="actionSend"? //右下角按鍵內(nèi)容為'發(fā)送' android:imeOptions="actionNext"? //右下角按鍵內(nèi)容為'下一步' android:imeOptions="actionDone" //右下角按鍵內(nèi)容為'完成'

12、限定edittext能輸入數(shù)字和字母,并且默認(rèn)輸入為數(shù)字,如身份證號碼

android:inputType="number" android:digits="0123456789xyzXYZ"

13、軟鍵盤的調(diào)起導(dǎo)致原來的界面被擠上去,或者導(dǎo)致界面下面的tab導(dǎo)航被擠上去,解決方法如下

解決方法:

使用Manifest中的Activity的android:windowSoftInputMode的"adjustPan"屬性。

另外注意:有關(guān)軟鍵盤的問題可參考android:windowSoftInputMode中屬性。

14、edittext光標(biāo)詳解 edittext.requestFocusFromTouch();//讓光標(biāo)放入到點擊位置。 edittext.requestFocus();//默認(rèn)方式獲得焦點

EditText editor = (EditText)getCurrentView();//光標(biāo)處插入 int cursor = editor.getSelectionStart(); editor.getText().insert(cursor,delta);

讓光標(biāo)移到末端(這樣文字就會向前顯示) EditText et = ... String text = "text"; et.setText(text); et.setSelection(text.length());

android:cursorVisible="false" 隱藏光標(biāo)

android:background="#00000000"http://不要文本框背景

Android - 文本框的輸入法控制和默認(rèn)焦點設(shè)置

在開發(fā)中,必不可少的會使用到文本框(EditText)來進行數(shù)據(jù)錄入,也就會需要對輸入法進行一些控制。

先看下LAYOUT定義文件中的和輸入法有關(guān)的屬性:

屬性名

說明

android:inputType

指定輸入法的類型,int類型,可以用|選擇多個。取值可以參考:android.text.InputType類。取值包括:text,

? textUri, phone,number,等。

android:imeOptions

指定輸入法窗口中的回車鍵的功能,可選值為normal,

? actionNext,actionDone,actionSearch等。部分輸入法對此的支持可能不夠好。

下面的LAYOUT定義文件舉了一些例子說明inputType和imeOptions的使用。

<EditText

android:id="@+id/textNormal"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:hint="Normal

text"

android:inputType="text"

android:imeOptions="actionNext"

/>

<EditText

android:id="@+id/textInteger"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:hint="Integer

only"

android:inputType="number"

android:imeOptions="actionNext"

/>

<EditText

android:id="@+id/textPhone"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:hint="Phone

number"

android:inputType="phone"

android:imeOptions="actionNext"

/>

<EditText

android:id="@+id/textEmail"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:hint="Email"

android:imeOptions="actionSend"

android:inputType="textEmailAddress"

/>

<EditText

android:id="@+id/textSite"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:hint="Web

Site"

android:imeOptions="actionDone"

android:inputType="textUri"

/>

有時候也要對intent的默認(rèn)焦點進行設(shè)置,不至于在intent跳轉(zhuǎn)的時候默認(rèn)焦點(光標(biāo))在EditText上,導(dǎo)致進入intent就打開輸入法,影響界面美觀。

默認(rèn)焦點的順序是:從上倒下

從左到右第一個可以輸入的控件作為焦點

可以使用:

button.setFocusable(true);

button.requestFocus();

button.setFocusableInTouchMode(true);

也可以:

在EditText前面放置一個看不到的LinearLayout,讓他率先獲取焦點:

<LinearLayout

android:focusable="true"

android:focusableInTouchMode="true"

android:layout_width="0px"

android:layout_height="0px"/>

Android EditText 屬性匯總


Android EditText 屬性匯總


android:layout_gravity="center_vertical" 設(shè)置控件顯示的位置:默認(rèn)top,這里居中顯示,還有bottom android:hint="請輸入數(shù)字!"

設(shè)置顯示在空間上的提示信息 android:numeric="integer" 設(shè)置只能輸入整數(shù),如果是小數(shù)則是:

decimal android:singleLine="true" 設(shè)置單行輸入,一旦設(shè)置為true,則文字不會自動換行。

android:password="true" 設(shè)置只能輸入密碼

android:textColor = "#ff8c00" 字體顏色

android:textStyle="bold" 字體,bold, italic, bolditalic android:textSize="20dip" 大小

android:capitalize = "characters" 以大寫字母寫

android:textAlign="center" EditText沒有這個屬性,但TextView有 android:textColorHighlight="#cccccc" 被選中文字的底色,默認(rè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)重,控制控件之間的地位,在控制控件顯示的大小時蠻有用的。

android:textAppearance="?android:attr/textAppearanceLargeInverse" 文字外觀,這里引用的是系統(tǒng)自帶的一個外觀,?表示系統(tǒng)是否有這種外觀,否則使用默認(rèn)的外觀。不知道這樣理解對不對? 通過EditText的layout xml文件中的相關(guān)屬性來實現(xiàn):

1. 密碼框?qū)傩?android:password="true" 這條可以讓EditText顯示的內(nèi)容自動為星號,輸入時內(nèi)容會在1秒內(nèi)變成*字樣。

2. 純數(shù)字 android:numeric="true" 這條可以讓輸入法自動變?yōu)閿?shù)字輸入鍵盤,同時僅允許0-9的數(shù)字輸入

3. 僅允許 android:capitalize="cwj1987" 這樣僅允許接受輸入cwj1987,一般用于密碼驗證 下面是一些擴展的風(fēng)格屬性

android:editable="false" 設(shè)置EditText不可編輯

android:singleLine="true" 強制輸入的內(nèi)容在單行

android:ellipsize="end" 自動隱藏尾部溢出數(shù)據(jù),一般用于文字內(nèi)容過長一行無法全部顯示時

監(jiān)聽事件

editText.addTextChangedListener(new TextWatcher() {

? ? @Override

? ? ? public void onTextChanged(CharSequence text, int start, int before, int count) {

? ? ? //text? 輸入框中改變后的字符串信息

? ? ? //start 輸入框中改變后的字符串的起始位置

? ? ? //before 輸入框中改變前的字符串的位置 默認(rèn)為0

? ? ? //count 輸入框中改變后的一共輸入字符串的數(shù)量

? ? ? ? ? ?

? ? ? }

? ? ? ? ? ?

? ? ? @Override

? ? ? public void beforeTextChanged(CharSequence text, int start, int count,int after) {

? ? ? //text? 輸入框中改變前的字符串信息

? ? ? //start 輸入框中改變前的字符串的起始位置

? ? ? //count 輸入框中改變前后的字符串改變數(shù)量一般為0

? ? ? //after 輸入框中改變后的字符串與起始位置的偏移量

? ? ? ? ?

? ? ? }

? ? ? ? ? ?

? ? ? @Override

? ? ? public void afterTextChanged(Editable edit) {

? ? ? //edit? 輸入結(jié)束呈現(xiàn)在輸入框中的信息

? ? ? ?

? ? ? }

});

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。