2012-10-23 58 views
3

是否可以阻止用戶關閉軟鍵盤。換句話說,當我的活動可見時,屏幕上始終可以使用鍵盤(始終)。這可能嗎?如果是這樣,你將如何實現它?Android阻止鍵盤關閉

+0

你試過了什麼? – Sieryuu

+0

將Android:windowSoftInputMode =「stateAlwaysVisible」添加到AndroidManifest.xml中的活動定義中。還嘗試過使用InputMethodManager的各種組合,沒有任何工作。 –

回答

0

你需要重寫onKeyUp()onKeyDown()方法,如下面,

@Override 
public boolean onKeyUp(int keyCode, KeyEvent event) { 
    if (keyCode == KeyEvent.KEYCODE_BACK)  
    { 
     ((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput 
       (InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY); 
    } 
    return super.onKeyUp(keyCode, event); 
} 

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if (keyCode == KeyEvent.KEYCODE_BACK)  
    { 
     ((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput 
       (InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY); 
    } 
    return super.onKeyDown(keyCode, event); 
} 

您也可以嘗試在兩種方式,通過使用OnEditorActionListener,看看下面的代碼,

EditText txtMyEdit = (EditText) findViewById(R.id.txtEdit); 
txtMyEdit.setOnEditorActionListener(new OnEditorActionListener() 
{ 
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) 
    { 
     if (actionId == EditorInfo.IME_ACTION_DONE) 
     { 
      // your additional processing... 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 
}); 
+1

這在某種程度上起作用,唯一的問題是焦點的變化仍然會導致鍵盤被解僱。我需要鍵盤始終保持在屏幕上。 –

0

這應該強制鍵盤顯示與焦點無關:

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
imm.hideSoftInputFromWindow(yourViewObject.getWindowToken(), 0); 
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);