2010-03-12 84 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。