首先,養(yǎng)個好習(xí)慣,只要使用EditText 就在XML文件中聲明要輸入的文本類型。官方文檔也說了 always

always.png
ok,回歸主題,軟鍵盤右下角按鈕,默認情況下是一個Enter符號,作用換行。

defult androidimeOptions.png
EditText提供了androidimeOptions屬性,控制該按鈕的顯示文字。(僅以搜狗為例,其他的界面顯示可能不同,大致意思是一樣的)
| android:imeOptions | 文字 |
|---|---|
| actionGo | 開始 |
| actionNext | 下一步 |
| actionSearch | 搜索 |
| actionSend | 發(fā)送 |
| actionDone | Enter符號 |
注意:要使android:imeOptions起作用,必須能加上android:inputType屬性,這也是一開始強調(diào)的 或者加上android:singleLine="true"也可以,但用android:maxLines="1"不可以
然后給右下角按鈕設(shè)置點擊監(jiān)聽事件,隱藏軟鍵盤
mActionSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
Toast.makeText(MainActivity.this, "搜索", Toast.LENGTH_SHORT).show();
handled = true;
/*隱藏軟鍵盤*/
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager.isActive()) {
inputMethodManager.hideSoftInputFromWindow(MainActivity.this.getCurrentFocus().getWindowToken(), 0);
}
}
return handled;
}
});
2017.1.13補充
我們會發(fā)現(xiàn)我們在EditText中輸入文字的時候,光標(biāo)會隨著你文字的增加一直往后移動,但是在有的android版本中,它并不會換行,其中有種有種可能是你把Enter鍵給設(shè)置成了”完成”, android:imeOptions=”actionDone” ,如果這是你需要換行的話,只要改變輸入的類型即可: android:inputType=”textMultiLine”(表示多行輸入)