EditText修改软键盘输入法的Enter键的按钮文字

布局文件:

  1. <EditText
  2. android:id= "@+id/et_drug_weight"
  3. android:layout_width= "0dp"
  4. android:layout_height= "match_parent"
  5. android:layout_marginLeft= "20dp"
  6. android:layout_weight= "1"
  7. android:gravity= "center"
  8. android:singleLine= "true"
  9. android:imeOptions= "actionDone"
  10. android:hint= "0"
  11. android:inputType= "number"
  12. android:maxLength= "4"
  13. android:textSize= "14sp" />

imeOptions

imeOptions表示要设置的行为模式,常用的有以下几种:

actionUnspecified  未指定,对应常量EditorInfo.IME_ACTION_UNSPECIFIED.

actionNone 没有动作,对应常量EditorInfo.IME_ACTION_NONE 

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

但是,不同的输入法会用不同的方式实现以上行为,比如,有的actionSearch会是“搜索”文字,有的会是一个放大镜图标,有的actionDone会是“完成”文字,有的会是一个回车图标。

imeActionLabel

imeActionLabel不是用于自定义按钮文字的。
例如你设置android:imeActionLabel="添加",android:imeOptions="actionDone",则有些软键盘的Enter键会显示“添加”而不是“完成”或其它。 但是这种设置不是在所有手机上都有效果,一般手机自带的软键盘有效果的可能性很大,第三方软键盘有效果的可能性很小。
而且即使显示了“添加”也是有问题的。总之,不要试着用imeActionLabel自定义文字

如果你用了 android:imeOptions但是没有效果,则应该加上android:singleLine="true"。
editText.setImeOptions(EditorInfo.IME_ACTION_SEND);
当然,无论是imeOptions还是imeActionLabel等,都可以通过java代码实现,不一定要在布局文件中设置。

二:在代码中监听按键

以监听actionDone为例:
  1. editText.setOnEditorActionListener( new TextView.OnEditorActionListener() {
  2. @Override
  3. public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
  4. if (actionId == EditorInfo.IME_ACTION_DONE) {
  5. //TODO:你自己的业务逻辑
  6. return true;
  7. }
  8. return false;
  9. }
  10. });

etSearch.setOnKeyListener(new View.OnKeyListener() {

@Override

public boolean onKey(View v, int keyCode, KeyEvent event) {

if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {

//先隐藏键盘

((InputMethodManager) getActivity().getSystemService(INPUT_METHOD_SERVICE))

.hideSoftInputFromWindow(getActivity().getCurrentFocus()

.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

//其次再做相应操作

inputContent = etSearch.getText().toString();

if (StringUtils.isBlank(inputContent)) {

} else {

//做相应的操作

}

}

return false;

}

});






猜你喜欢

转载自blog.csdn.net/kdsde/article/details/81000703