2011-11-14 199 views
7

我試圖表明我的鍵盤膨脹的LinearLayout和調用的setContentView等之後:如何強制鍵盤顯示/隱藏?

InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
mgr.showSoftInput(etContent, InputMethodManager.SHOW_FORCED); 
getContent.requestFocus(); 

它沒有工作。我也試過這個:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); 

但它也沒有工作。我如何強制鍵盤顯示/隱藏?我做錯了什麼?

+1

完全重複的http:/ /stackoverflow.com/questions/1109022/how-to-close-hide-the-android-soft-keyboard和http://stackoverflow.com/questions/2479504/forcing-the-soft-keyboard-open – Polynomial

回答

1

this link明確隱藏軟鍵盤。 顯示它您可以使用一個黑客 - 在任何地方創建一個EditText在佈局,layout_width和layout_height = 0dip,並在的onCreate做

yourEditText.requestFocus(); 
30

這應該工作

public class KeyBoard { 

    public static void toggle(Activity activity){ 
     InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE); 
     if (imm.isActive()){ 
      imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0); // hide 
     } else { 
      imm.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY); // show 
     } 
    }//end method 
}//end class 
+0

好的答案仍然值得 :) –