日常開發(fā)中,最繞不開的一個(gè)控件就是EditText,隨之避免不了的則是對(duì)其軟鍵盤事件的監(jiān)聽,隨著需求的不同對(duì)用戶輸入的軟鍵盤要求也不同,有的場(chǎng)景需要用戶輸入完畢后,有一個(gè)確認(rèn)按鈕,有的場(chǎng)景需要的是回車,有的場(chǎng)景需要用戶輸入后進(jìn)入下一項(xiàng)或者搜索,所幸的是,大部分需求場(chǎng)景通過修改原生設(shè)置就可滿足,只要極少情況下才需要去寫自定義鍵盤。而關(guān)于EditText喚起的軟鍵盤中回車的功能可以通過imeOptions的設(shè)定來(lái)進(jìn)行相應(yīng)的設(shè)置。
其使用方式僅通過在xml中聲明即可:
<EditText
...
android:imeOptions="actionSend"/>
常用屬性
如果不特殊聲明,右下角按鍵則為回車鍵。其常用屬性及相應(yīng)功能設(shè)置如下:
屬性 | 右下角按鍵顯示及常見應(yīng)用場(chǎng)景 |
---|---|
actionGo | 右下角按鍵顯示“開始” |
actionSearch | 右下角顯示放大鏡,對(duì)應(yīng)搜索功能場(chǎng)景 |
actionSend | 右下角按鍵內(nèi)容為"發(fā)送",一般用于即時(shí)聊天頁(yè)面 |
actionNext | 右下角按鍵內(nèi)容為“下一步”或者“下一項(xiàng)”,會(huì)跳到下一個(gè)EditText |
actionDone | 右下角按鍵內(nèi)容為“完成” |
actionNone | 無(wú)任何提示 |
flagNoExtractUi | 使軟鍵盤不全屏顯示,只占用一部分屏幕,右下角按鍵為默認(rèn)回車鍵在指定imeOptions后,還要添加android:inputType="text"屬性。 |
也可以通過代碼去設(shè)置對(duì)應(yīng)屬性,如下:
editText.setInputType(EditorInfo.TYPE_CLASS_TEXT);
editText.setImeOptions(EditorInfo.IME_ACTION_...);
其屬性與代碼中設(shè)置的常量關(guān)系為:
屬性 | 對(duì)應(yīng)常量 |
---|---|
actionGo | EditorInfo.IME_ACTION_GO |
actionSearch | EditorInfo.IME_ACTION_SEARCH |
actionSend | EditorInfo.IME_ACTION_SEND |
actionNext | EditorInfo.IME_ACTION_NEXT |
actionDone | EditorInfo.IME_ACTION_DONE |
actionNone | EditorInfo.IME_ACTION_NONE |
actionUnspecified(未指定) | EditorInfo.IME_ACTION_UNSPECIFIED |
監(jiān)聽
對(duì)應(yīng)的EditText可以設(shè)置相應(yīng)的監(jiān)聽,editText.setOnEditorActionListener,在監(jiān)聽的onEditorAction()中通過返回的actionId參數(shù)來(lái)判斷觸發(fā)的對(duì)應(yīng)事件。例如以下示例:
xml中簡(jiǎn)單設(shè)置一個(gè)EditText:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/edt_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:imeOptions="actionGo"
android:inputType="text"/>
</RelativeLayout>
對(duì)應(yīng)在Activity中對(duì)其進(jìn)行事件監(jiān)聽:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText mEdtView = findViewById(R.id.edt_view);
mEdtView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
switch (actionId){
case EditorInfo.IME_ACTION_DONE:
Toast.makeText(MainActivity.this, "IME_ACTION_DONE", Toast.LENGTH_SHORT).show();
break;
case EditorInfo.IME_ACTION_GO:
Toast.makeText(MainActivity.this, "IME_ACTION_GO", Toast.LENGTH_SHORT).show();
break;
case EditorInfo.IME_ACTION_NEXT:
Toast.makeText(MainActivity.this, "IME_ACTION_NEXT", Toast.LENGTH_SHORT).show();
break;
case EditorInfo.IME_ACTION_NONE:
Toast.makeText(MainActivity.this, "IME_ACTION_NONE", Toast.LENGTH_SHORT).show();
break;
case EditorInfo.IME_ACTION_PREVIOUS:
Toast.makeText(MainActivity.this, "IME_ACTION_PREVIOUS", Toast.LENGTH_SHORT).show();
break;
case EditorInfo.IME_ACTION_SEARCH:
Toast.makeText(MainActivity.this, "IME_ACTION_SEARCH", Toast.LENGTH_SHORT).show();
break;
case EditorInfo.IME_ACTION_SEND:
Toast.makeText(MainActivity.this, "IME_ACTION_SEND", Toast.LENGTH_SHORT).show();
break;
case EditorInfo.IME_ACTION_UNSPECIFIED:
Toast.makeText(MainActivity.this, "IME_ACTION_UNSPECIFIED", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return true;
}
});
}
其對(duì)應(yīng)效果為:
對(duì)應(yīng)吐司也驗(yàn)證了我們代碼的運(yùn)行,我們?cè)僭趚ml中刪除對(duì)應(yīng)屬性,用代碼的形式聲明試試。
xml中:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/edt_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:inputType="text"/>
</RelativeLayout>
可見,其余無(wú)變動(dòng),對(duì)應(yīng)activity的修改則是在設(shè)置監(jiān)聽前設(shè)置對(duì)應(yīng)屬性:
...
mEdtView.setImeOptions(EditorInfo.IME_ACTION_SEND);
...
其效果為:
可見,代碼中設(shè)置效果則一樣,也許你會(huì)疑問,為什么點(diǎn)擊右下角按鍵后還不能收起軟鍵盤,系統(tǒng)中是沒有這樣主動(dòng)行為的,需要我們自己來(lái)調(diào)用以下方法即可:
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(context.getWindow().getDecorView().getWindowToken(), 0);
重復(fù)響應(yīng)問題
這里需要注意的是,onEditorAction中,如果返回的是false,則onEditorAction中的代碼可能會(huì)調(diào)用兩次,原因不難理解,系統(tǒng)會(huì)首先判斷用戶實(shí)現(xiàn)的方法onEditorActionListener.onEditorAction(this, actionCode, null)的返回值,一旦返回true,會(huì)立即return,因此系統(tǒng)的處理被直接跳過。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-829877.html
設(shè)置無(wú)效問題
當(dāng)設(shè)置了android:maxLines=“1” 屬性時(shí),有可能出現(xiàn)設(shè)置無(wú)效問題,這里要改為android:singleLine="true"此屬性即可。當(dāng)然,有可能個(gè)別的機(jī)型還有其他適配問題,比如三星等等,有遇見的朋友可以留言互相交流。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-829877.html
到了這里,關(guān)于Android EditText關(guān)于imeOptions的設(shè)置和響應(yīng)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!