2010-03-12 87 views
27

当我的用户按在虚拟的android“user validate entry!”上输入。 keybord我的钥匙保持可见! (为什么?)

android设置隐藏键盘上的输入(在EditText中)

这里我的Java代码...

private void initTextField() { 
    entryUser = (EditText) findViewById(R.id.studentEntrySalary); 
    entryUser.setOnKeyListener(new OnKeyListener() { 
     public boolean onKey(View v, int keyCode, KeyEvent event) { 
      if (event.getAction() == KeyEvent.ACTION_DOWN) { 
       switch (keyCode) { 
        case KeyEvent.KEYCODE_DPAD_CENTER: 
        case KeyEvent.KEYCODE_ENTER: 
         userValidateEntry(); 
         return true; 
       } 
      } 

      return true; 
     } 
    }); 
} 

private void userValidateEntry() { 
    System.out.println("user validate entry!"); 
} 

...这里我查看

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> 
      <EditText android:id="@+id/studentEntrySalary" android:text="Foo" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 
</LinearLayout> 

也许有些不对劲我的虚拟设备上?

去除死ImageShack的链接

回答

66

这应做到:

yourEditTextHere.setOnEditorActionListener(new OnEditorActionListener() { 

     @Override 
     public boolean onEditorAction(TextView v, int actionId, 
       KeyEvent event) { 
      if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) { 
       InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 

       // NOTE: In the author's example, he uses an identifier 
       // called searchBar. If setting this code on your EditText 
       // then use v.getWindowToken() as a reference to your 
       // EditText is passed into this callback as a TextView 

       in.hideSoftInputFromWindow(searchBar 
         .getApplicationWindowToken(), 
         InputMethodManager.HIDE_NOT_ALWAYS); 
       userValidateEntry(); 
       // Must return true here to consume event 
       return true; 

      } 
      return false; 
     } 
    }); 
+2

您的属性! (更改searchBar购买您的编辑文本文件) – 2010-03-12 19:10:49

+1

1.如果(事件!= null &&(event.getKeyCode()== KeyEvent.KEYCODE_D))因此,输入表单不会隐藏。 – Samir 2010-07-14 09:23:42

+5

对于任何看到Samir评论的人来说,这是因为此代码设置了“OnEditorActionListener”,仅在按Enter之类按键时才会调用该按钮,而不是常规字符键。 – 2013-03-17 21:40:59

5

如果使文本框单行(我相信属性为在布局XML文件称为单线),它会退出键盘上输入的了。

在这里你去:http://developer.android.com/reference/android/R.styleable.html#TextView_singleLine

+2

否定。它仍然是与android相同的问题:singleLine =“true” – 2010-03-12 17:51:22

+0

这对我的Android 4(Tablet 4.0.3,构建目标4.2),如果它是依赖于Android版本(我还没有尝试过其他版本)。 – Mick 2013-06-03 14:40:26

18

保持单线= “true”,并添加imeOptions = “actionDone” 的EditText上。 然后在OnEditorActionListener检查actionId == EditorInfo.IME_ACTION_DONE,像这样(但将其更改为您的实现):

if (actionId == EditorInfo.IME_ACTION_DONE) { 

       if ((username.getText().toString().length() > 0) 
         && (password.getText().toString().length() > 0)) { 
        // Perform action on key press 
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
        imm.hideSoftInputFromWindow(username.getWindowToken(), 
          0); 
        doLogin(); 
       } 
      } 
+6

就我而言,'imeOptions =“actionDone”'就够了,不需要代码。 – 1615903 2015-10-22 09:43:29

1

我是谁创建延伸AutoCompleteTextView,就像在下面的例子中的自定义组件:

public class PortugueseCompleteTextView extends AutoCompleteTextView { 
... 
@Override 
public boolean onKeyPreIme(int keyCode, KeyEvent event) { 
    if (event != null && (event.getKeyCode() == KeyEvent.KEYCODE_BACK)) { 
     InputMethodManager inputManager = 
       (InputMethodManager) getContext(). 
         getSystemService(Context.INPUT_METHOD_SERVICE); 
     inputManager.hideSoftInputFromWindow(
       this.getWindowToken(), 
       InputMethodManager.HIDE_NOT_ALWAYS); 
    } 
    return super.onKeyPreIme(keyCode, event); 
} 

我在AlertDialog.Builder中使用此代码,但可以使用到Activity。