Android中Edittext的屬性

來源:互聯網
上載者:User

標籤:

//此為轉載別人的,挺不錯的

1、EditText輸入的文字為密碼形式的設定

(1)通過.xml裡設定:

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

(2)在代碼裡設定:

通過設定EditText的setTransformationMethod()方法來實現隱藏密碼或這顯示密碼。

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

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

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

3、EditText字數限制的設定

(1)在.xml中設定:android:maxLength=“50” 

(2)代碼中設定:   

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

4、EditText設定字型

android:typeface="monospace" //設定字型。字形有:normal, sans, serif,monospace

5、EditText是否可編輯

Android:editable // 是否可編輯

6、在EditText中軟鍵盤的調起、關閉

(1)EditText有焦點(focusable為true)阻止IME彈出

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

   editText.setOnTouchListener(new OnTouchListener(){  

         public boolean onTouch(View v, MotionEvent event){ 

            editText.setInputType(InputType.TYPE_NULL); //關閉軟鍵盤     

            return false;

         }

    });

(2)當EidtText無焦點(focusable=false)時阻止IME彈出

 InputMethodManager imm =

(InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); 

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

(3)調用數字鍵台並設定輸入類型和鍵盤為英文

etNumber.setInputType(InputType.TYPE_CLASS_NUMBER); //調用數字鍵台

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

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

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

 

// 關閉鍵盤(比如輸入結束後執行) 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的區別

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

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

8、android:padding和android:layout_margin區別

這兩個都可以設定邊距,但有細微的區別:

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

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

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

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

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

9、android:numeric//只接受數字

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

10、Enter鍵表徵圖的設定

軟 鍵盤的Enter鍵預設顯示的是“完成”文本,我們知道按Enter建表示前置工作已經準備完畢了,要去什麼什麼啦。比如,在一個搜尋中,我們輸入要搜尋 的文本,然後按Enter表示要去搜尋了,但是預設的Enter鍵顯示的是“完成”文本,看著不太合適,不符合搜尋的語義,如果能顯示“搜尋”兩個字或者 顯示一個表示搜尋的表徵圖多好。事實證明我們的想法是合理的,Android也為我們提供的這樣的功能。通過設定android:imeOptions來改 變預設的“完成”文本。這裡舉幾個常用的常量值:

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

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

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

(4)actionSearch 搜尋,對應常量EditorInfo.IME_ACTION_SEARCH效果: 

(5)actionSend 發送,對應常量EditorInfo.IME_ACTION_SEND效果:

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

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

 

11、使用android:imeOptinos可對Android內建的軟鍵盤進行一些介面上的設定:

android:imeOptions="flagNoExtractUi" // 使軟鍵盤不全螢幕顯示,只佔用一部分螢幕 同時,這個屬性還能控制項軟鍵盤右下角按鍵的顯示內容,預設情況下為斷行符號鍵 android:imeOptions="actionNone" //輸入框右側不帶任何提示 android:imeOptions="actionGo"   //右下角按鍵內容為‘開始‘ android:imeOptions="actionSearch" //右下角按鍵為放大鏡圖片,搜尋 android:imeOptions="actionSend"   //右下角按鍵內容為‘發送‘ android:imeOptions="actionNext"  //右下角按鍵內容為‘下一步‘ android:imeOptions="actionDone" //右下角按鍵內容為‘完成‘

12、限定edittext能輸入數字和字母,並且預設輸入為數字,如社會安全號碼碼

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

13、軟鍵盤的調起導致原來的介面被擠上去,或者導致介面下面的tab導航被擠上去,解決方案如下

解決方案:

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

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

14、edittext游標詳解 edittext.requestFocusFromTouch();//讓游標放入到點擊位置。 edittext.requestFocus();//預設獲得焦點
EditText editor = (EditText)getCurrentView();//游標處插入 int cursor = editor.getSelectionStart(); editor.getText().insert(cursor,delta);

 

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

 

android:cursorVisible="false" 隱藏游標

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

 

 

Android - 文字框的IME控制和預設焦點設定
在開發中,必不可少的會使用到文字框(EditText)來進行資料錄入,也就會需要對IME進行一些控制。
先看下LAYOUT定義檔案中的和IME有關的屬性:
屬性名稱
說明
android:inputType
指定IME的類型,int類型,可以用|選擇多個。取值可以參考:android.text.InputType類。取值包括:text,  textUri, phone,number,等。
android:imeOptions
指定IME視窗中的斷行符號鍵的功能,可選值為normal,  actionNext,actionDone,actionSearch等。部分IME對此的支援可能不夠好。
下面的LAYOUT定義檔案舉了一些例子說明inputType和imeOptions的使用。
<EditTextandroid:id="@+id/textNormal"
android:layout_width="fill_parent"android:layout_height="wrap_content"
android:hint="Normaltext"
android:inputType="text"
android:imeOptions="actionNext"
/>
<EditTextandroid:id="@+id/textInteger"
android:layout_width="fill_parent"android:layout_height="wrap_content"
android:hint="Integeronly"
android:inputType="number"
android:imeOptions="actionNext"
/>
<EditTextandroid:id="@+id/textPhone"
android:layout_width="fill_parent"android:layout_height="wrap_content"
android:hint="Phonenumber"
android:inputType="phone"
android:imeOptions="actionNext"
/>
<EditTextandroid:id="@+id/textEmail"
android:layout_width="fill_parent"android:layout_height="wrap_content"
android:hint="Email"
android:imeOptions="actionSend"
android:inputType="textEmailAddress"
/>
<EditTextandroid:id="@+id/textSite"
android:layout_width="fill_parent"android:layout_height="wrap_content"
android:hint="WebSite"
android:imeOptions="actionDone"
android:inputType="textUri"
/>有時候也要對intent的預設焦點進行設定,不至於在intent跳轉的時候預設焦點(游標)在EditText上,導致進入intent就開啟IME,影響介面美觀。
預設焦點的順序是:從上倒下從左至右第一個可以輸入的控制項作為焦點
可以使用:
button.setFocusable(true);
button.requestFocus();
button.setFocusableInTouchMode(true);
也可以:
在EditText前面放置一個看不到的LinearLayout,讓他率先擷取焦點:<LinearLayoutandroid:focusable="true"android:focusableInTouchMode="true"android:layout_width="0px"android:layout_height="0px"/>
Android EditText 屬性匯總

 

Android EditText 屬性匯總

 

android:layout_gravity="center_vertical" 設定控制項顯示的位置:預設top,這裡置中顯示,還有bottom android:hint="請輸入數字!"

設定顯示在空間上的提示資訊 android:numeric="integer" 設定只能輸入整數,如果是小數則是:

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

android:password="true" 設定只能輸入密碼

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

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

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

android:textAlign="center" EditText沒有這個屬性,但TextView有 android:textColorHighlight="#cccccc" 被選中文字的底色,預設為藍色

android:textColorHint="#ffff00" 設定提示資訊文字的顏色,預設為灰色 android:textScaleX="1.5" 控制字與字之間的間距

android:typeface="monospace" 字型,normal, sans, serif, monospace android:background="@null" 空間背景,這裡沒有,指透明

android:layout_weight="1" 權重,控制控制項之間的地位,在控制控制項顯示的大小時蠻有用的。

android:textAppearance="?android:attr/textAppearanceLargeInverse" 文字外觀,這裡引用的是系統內建的一個外觀,?表示系統是否有這種外觀,否則使用預設的外觀。不知道這樣理解對不對? 通過EditText的layout xml檔案中的相關屬性來實現:

1. 密碼框屬性 android:password="true" 這條可以讓EditText顯示的內容自動為星號,輸入時內容會在1秒內變成*字樣。

2. 純數字 android:numeric="true" 這條可以讓IME自動變為數字輸入鍵盤,同時僅允許0-9的數字輸入

3. 僅允許 android:capitalize="cwj1987" 這樣僅允許接受輸入cwj1987,一般用於密碼驗證 下面是一些擴充的風格屬性

android:editable="false" 設定EditText不可編輯

android:singleLine="true" 強制輸入的內容在單行

android:ellipsize="end" 自動隱藏尾部溢出資料,一般用於文字內容過長一行無法全部顯示時

Android中Edittext的屬性

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.