1、EditText輸入的文字爲密碼形式的設置android
(1)經過.xml裏設置:git
把該EditText設爲:android:password="true" // 以」.」形式顯示文本web
(2)在代碼裏設置:api
經過設置EditText的setTransformationMethod()方法來實現隱藏密碼或這顯示密碼。ide
editText.setTransformationMethod(PasswordTransformationMethod.getInstance());//設置密碼爲不可見。佈局
Android:phoneNumber=」true」 //輸入電話號碼spa
(1)在.xml中設置:android:maxLength=「50」 code
(2)代碼中設置:
editText.setFilters(new InputFilter[]{newInputFilter.LengthFilter(100)});
四、EditText設置字體
android:typeface="monospace" //設置字型。字形有:normal, sans, serif,monospace
Android:editable // 是否可編輯
六、在EditText中軟鍵盤的調起、關閉
(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); //關閉軟鍵盤
return false;
}
});
(2)當EidtText無焦點(focusable=false)時阻止輸入法彈出
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得到焦點,可是得到焦點並不會自動彈出鍵盤
七、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
九、android:numeric//只接受數字
android:numeric來控制輸入的數字類型,一共有三種分別爲integer(正整數)、signed(帶符號整數,有正負)和decimal(浮點數)。
十、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效果:
十一、使用android:imeOptinos可對Android自帶的軟鍵盤進行一些界面上的設置:
android:imeOptions="flagNoExtractUi" //使軟鍵盤不全屏顯示,只佔用一部分屏幕 同時,這個屬性還能控件軟鍵盤右下角按鍵的顯示內容,默認狀況下爲回車鍵 android:imeOptions="actionNone" //輸入框右側不帶任何提示 android:imeOptions="actionGo" //右下角按鍵內容爲'開始' android:imeOptions="actionSearch" //右下角按鍵爲放大鏡圖片,搜索 android:imeOptions="actionSend" //右下角按鍵內容爲'發送' android:imeOptions="actionNext" //右下角按鍵內容爲'下一步' android:imeOptions="actionDone" //右下角按鍵內容爲'完成'
十二、限定edittext能輸入數字和字母,而且默認輸入爲數字,如身份證號碼
android:inputType="number" android:digits="0123456789xyzXYZ"
1三、軟鍵盤的調起致使原來的界面被擠上去,或者致使界面下面的tab導航被擠上去,解決方法以下
解決方法:
使用Manifest中的Activity的android:windowSoftInputMode的"adjustPan"屬性。
另外注意:有關軟鍵盤的問題可參考android:windowSoftInputMode中屬性。
1四、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"//不要文本框背景
在開發中,必不可少的會使用到文本框(EditText)來進行數據錄入,也就會須要對輸入法進行一些控制。
先看下LAYOUT定義文件中的和輸入法有關的屬性:
屬性名 |
說明 |
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的默認焦點進行設置,不至於在intent跳轉的時候默認焦點(光標)在EditText上,致使進入intent就打開輸入法,影響界面美觀。
默認焦點的順序是:從上倒下 從左到右第一個能夠輸入的控件做爲焦點
可使用:
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: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" 這條可讓輸入法自動變爲數字輸入鍵盤,同時僅容許0-9的數字輸入
3. 僅容許 android:capitalize="cwj1987" 這樣僅容許接受輸入cwj1987,通常用於密碼驗證 下面是一些擴展的風格屬性
android:editable="false" 設置EditText不可編輯
android:singleLine="true" 強制輸入的內容在單行
android:ellipsize="end" 自動隱藏尾部溢出數據,通常用於文字內容過長一行沒法所有顯示時