2015-03-08 78 views
1

不知道,如果是相關的,但我使用的是LG G3採用Android 4.4.2顯示和隱藏軟接口的鍵盤,不工作

我有我想要的鍵盤顯示的片段,所以我做了以下:

txtContact = (EditText) rootView.findViewById(R.id.txtContact); 
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); 
imm.hideSoftInputFromWindow(txtContact.getWindowToken(), InputMethodManager.SHOW_IMPLICIT); 

和佈局是這樣的,包括requestFocus的對於相關領域:

<EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="textMultiLine" 
     android:ems="10" 
     android:lines="10" 
     android:layout_margin="10dp" 
     android:id="@+id/txtContact"> 

     <requestFocus /> 
    </EditText> 

當打開這個片段,光標是它應該是,但鍵盤是不存在的,只有當他們在田裏踢球時,他們就會出現。

另一個問題與第一個問題相反。在另一個片段中,無論發生什麼,片段打開時我都想隱藏鍵盤。原因是,用戶可以在關閉另一個關閉之後打開這個片段,其中顯示了鍵盤,因此我想確保它關閉,注意我不知道鍵盤是否顯示,我希望它顯示如果它保持關閉,或者如果它是開放的,則關閉。我只有在這個片段一個ListView,所以我做了以下內容:

lv = (ListView) rootView.findViewById(android.R.id.list); 
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); 
imm.hideSoftInputFromWindow(lv.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 

,但它拒絕讓鍵盤上的任何影響,它仍然開放。

我在做什麼錯?

+0

當你想要顯示鍵盤時,爲什麼要調用'hideSoftInputFromWindow()'?考慮使用'showSoftInput()'。 – biegleux 2015-03-08 17:58:18

+0

我將它改爲imm.showSoftInput(txtContact,InputMethodManager.SHOW_IMPLICIT);但它沒有效果,鍵盤沒有顯示 – Amos 2015-03-08 18:20:57

+0

你在哪裏叫它? – biegleux 2015-03-08 18:28:17

回答

1

嘗試撥打onResume()post()中的代碼,將其作爲Runnable。 如果您的FragmentDialogFragment,則在onCreateView()中調用getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);應該足夠。