Android EditText的设置
1、輸入法Enter鍵圖標(biāo)的設(shè)置:
軟件盤的界面替換只有一個(gè)屬性android:imeOptions,這個(gè)屬性的可以取的值有normal,actionUnspecified,actionNone,actionGo,actionSearch,actionSend,actionNext,actionDone,例如當(dāng)值為actionNext時(shí)enter鍵外觀變成一個(gè)向下箭頭,而值為actionDone時(shí)enter鍵外觀則變成了“完成”兩個(gè)字。?
我們也可以重寫enter的事件
?
軟鍵盤的Enter鍵默認(rèn)顯示的是“完成”文本,通過(guò)設(shè)置android:imeOptions來(lái)改變默認(rèn)的“完成”文本。這里舉幾個(gè)常用的常量值:
actionUnspecified? 未指定,對(duì)應(yīng)常量EditorInfo.IME_ACTION_UNSPECIFIED.??
actionNone 沒(méi)有動(dòng)作,對(duì)應(yīng)常量EditorInfo.IME_ACTION_NONE?
actionGo 去往,對(duì)應(yīng)常量EditorInfo.IME_ACTION_GO
actionSearch 搜索,對(duì)應(yīng)常量EditorInfo.IME_ACTION_SEARCH ???
actionSend 發(fā)送,對(duì)應(yīng)常量EditorInfo.IME_ACTION_SEND ??
actionNext 下一個(gè),對(duì)應(yīng)常量EditorInfo.IME_ACTION_NEXT ??
actionDone 完成,對(duì)應(yīng)常量EditorInfo.IME_ACTION_DONE ?
?
(EditorInfo.inputType & EditorInfo.TYPE_CLASS_MASK)可以是許多不同的值,包括:?
TYPE_CLASS_NUMBER?
TYPE_CLASS_DATETIME?
TYPE_CLASS_PHONE?
TYPE_CLASS_TEXT
?
2、事件捕捉處理:
可以通過(guò)setOnEditorActionListener設(shè)置事件處理。
final EditText input = new EditText(this); input.setSingleLine(true); //android:singleLine=”true” input.setImeOptions(EditorInfo.IME_ACTION_SEND); input.setInputType(InputType.TYPE_CLASS_TEXT |InputType.TYPE_TEXT_VARIATION_PASSWORD); input.setOnEditorActionListener(new TextView.OnEditorActionListener() { public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { Log.d(TAG, ""+actionId+","+event); if (actionId==EditorInfo.IME_ACTION_SEND ||(event!=null&&event.getKeyCode()== KeyEvent.KEYCODE_ENTER)) { //do something; return true; } return false; } });3、editor密碼隱藏,怎么寫?
有2種方法處理:
代碼方法:input.setInputType(InputType.TYPE_CLASS_TEXT |InputType.TYPE_TEXT_VARIATION_PASSWORD);
layout配置方法:android:inputType="textPassword"
?
4、activity加載完成后,edit輸入框會(huì)自動(dòng)彈出輸入法,可以通過(guò)以下代碼屏蔽:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
5、設(shè)置EditText始終不彈出軟件鍵盤?
例:EditText edit=(EditText)findViewById(R.id.edit);?
?????? edit.setInputType(InputType.TYPE_NULL);
?
6、讓 EditText失去焦點(diǎn),使用EditText的clearFocus方法?
例如:EditText edit=(EditText)findViewById(R.id.edit);?
?????????? edit.clearFocus();
?
7、EditText默認(rèn)不彈出軟件鍵盤
在 AndroidMainfest.xml中選擇activity,設(shè)置windowSoftInputMode屬性為 adjustUnspecified|stateHidden
< activity android:name=".Main" android:label="@string/app_name" android:windowSoftInputMode="adjustUnspecified|stateHidden" android:configChanges="orientation|keyboardHidden"> < intent-filter> < action android:name="android.intent.action.MAIN" /> < category android:name="android.intent.category.LAUNCHER" /> < /intent-filter> < /activity>8、設(shè)置光標(biāo)到指定位置
EditText et = (EditText) findViewById(R.id.etTest); et.setSelection(2); //設(shè)置光標(biāo)不顯示,但不能設(shè)置光標(biāo)顏色et.setCursorVisible(false);
//獲得焦點(diǎn)時(shí)全選文本
et.setSelectAllOnFocus(true);
et.requestFocus();?//請(qǐng)求獲取焦點(diǎn)
et.clearFocus();?//清除焦點(diǎn)
使用EditText的setError提示
et.setError("郵箱");
自定義圖標(biāo)的setError提示
Drawable dr = getResources().getDrawable(R.drawable.ic_launcher); dr.setBounds(0, 0, 10, 10); //必須設(shè)置大小,否則不顯示 et.setError("有錯(cuò)誤提示", dr); et.setInputType(InputType.TYPE_CLASS_PHONE);//只能輸入電話號(hào)碼 et.setInputType(InputType.TYPE_CLASS_NUMBER);//只能輸入數(shù)字 et.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);//只能輸入郵箱地址 et.setInputType(InputType.TYPE_NULL); // 禁止輸入(不彈出輸入法) XML實(shí)現(xiàn)案例
<EditText android:id="@+id/etTest" android:inputType="number"android:layout_width="wrap_content" android:layout_height="wrap_content"/>
8、EditText相關(guān)屬性
EditText繼承關(guān)系:View-->TextView-->EditText。?
EditText的屬性很多,這里介紹幾個(gè):?
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:password="true"?
設(shè)置只能輸入密碼?
android:textColor = "#ff8c00"?
字體顏色?
android:textStyle="bold"?
字體,bold, italic, bolditalic?
android:textSize="20dip"?
大小?
android:capitalize = "characters"?
以大寫字母寫?
android:textAlign="center"?
EditText沒(méi)有這個(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"?
空間背景,這里沒(méi)有,指透明?
android:layout_weight="1"?
權(quán)重,控制控件之間的地位,在控制控件顯示的大小時(shí)蠻有用的。?
android:textAppearance="?android:attr/textAppearanceLargeInverse"
總結(jié)
以上是生活随笔為你收集整理的Android EditText的设置的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 获取iOS设备的型号
- 下一篇: 网络请求的基本知识《极客学院 --AFN