2010-11-20 182 views

回答

1

您可以尝试在布局的另一个元素上执行SetFocus()

如果你正在谈论的“输入/ OK /返回”键盘本身的按钮,您可能必须建立在EditText控制KeyListener为了知道什么时候到另一个元件上SetFocus()

+0

我明白,这是很多人给出的解决方案,但我不喜欢它,因为而不是input.clearFocus()简单地工作(即从那个输入中清除焦点),你现在必须把焦点放在其他东西上!?这看起来反直觉。 – marienke 2017-04-12 13:45:30

11

在布局XML文件中,指定您的EditText的imeOption:

android:imeOptions="actionGo" 

接下来,在活动的Java文件

mYourEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
      if (actionId == EditorInfo.IME_ACTION_GO) { 
       // hide virtual keyboard 
       InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
       imm.hideSoftInputFromWindow(mYourEditText.getWindowToken(), 0); 
       return true; 
      } 
      return false; 
     } 
    }); 

凡mYourEditText是一个EditText添加动作监听到你的EditText对象

+0

恩,谢谢。但即时通讯suposed编写imeOpt在布局或编辑文本? 和我在EditorInfo上有错误,有什么想法? – carefacerz 2010-11-22 16:11:56

+0

你能解释一下EditorInfo吗? – carefacerz 2010-11-22 18:54:44

+0

这将关闭键盘,但不会在所有情况下移除焦点。 IE浏览器将要求重点关注可以关注的第一个可用视图。 – lostintranslation 2017-01-31 17:20:05

12
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
imm.hideSoftInputFromWindow(editTextField.getWindowToken(), 0); 
+1

这只会隐藏键盘。它不会消除焦点。 – lostintranslation 2017-01-31 17:20:34

2
private void hideDefaultKeyboard() { 
    activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); 
    //you have got lot of methods here 
} 
1

确保您的EditText XML有:

android:id="@+id/myEditText"  
android:imeOptions="actionDone" 

然后设置监听到你的EditText(与科特林,并从片段):

myEditText.setOnEditorActionListener({ v, actionId, event -> 
      if (actionId == EditorInfo.IME_ACTION_DONE) { 
       myEditText.clearFocus() 
       val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager 
       imm.hideSoftInputFromWindow(view!!.windowToken, 0)  
      } 
      false 
     })