2016-01-22 136 views
3

我發現了一些這個問題的答案,但他們都沒有爲我工作。我在我的Fragment中有一個編輯文本,它在應用程序啓動時啓動。當這個片段打開時,軟鍵盤也會彈出。我如何防止這種情況發生?這是我在我在我的片段onCreateView方法....關閉片段中的軟鍵盤

 try { 
     InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(
       Context.INPUT_METHOD_SERVICE); 
     imm.hideSoftInputFromWindow(userName.getWindowToken(), 0); 
    }catch(Exception e) { 
     e.printStackTrace(); 
    } 
+0

do y ou把它修好了 –

+0

可以請你發佈你的XML嗎? –

回答

5

onCreateViewonActivityCreated試試這個。

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
1

我最近的項目,我使用的代碼如下隱藏鍵盤佈局,也許你可以試試。(我學習它從Wordpress-android源代碼)

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View view = inflater.inflate(R.layout.fragment_template_add_doc, container, false); 
    //hide the keyboard if it is visible 
    InputMethodManager imm = (InputMethodManager) getActivity() 
      .getSystemService(Context.INPUT_METHOD_SERVICE); 
    imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0); 
    return view; 
} 
1

請嘗試以下邏輯來隱藏鍵盤自動打開。

嘗試將您的編輯文本置於單獨的線性佈局中並設置android:focusableInTouchMode="true"。這將自動避免鍵盤自動打開。

<LinearLayout 
    android:id = "@+id/layout" 
    android:layout_width = "wrap_content" 
    android:layout_height = "wrap_content" 
    android:focusable = "true" 
    android:focusableInTouchMode = "true"> 

    <EditText 
     android:id = "@+id/edit_text" 
     android:layout_width = "match_content" 
     android:layout_height = "wrap_content"/> 
    </LinearLayout> 

或者如果上述失敗,使用下面的代碼以編程方式隱藏。把它寫成一個單獨的函數並在代碼中調用它。

在創建視圖後,在您的片段中調用此方法,如下所示。

@Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 
    hideKeyboard(); 
    } 

public void hideKeyboard() { 
    InputMethodManager inputMethodManager = (InputMethodManager) activity 
      .getSystemService(android.content.Context.INPUT_METHOD_SERVICE); 

    inputMethodManager.hideSoftInputFromWindow(
      activity.getCurrentFocus() 
        .getWindowToken(), 0); 
    } // hideKeyboard 

好運..!

0

這個工作對我來說,嘗試這樣

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 


hideKeyboard(getActivity()); 
} 

    public static void hideKeyboard(Context context) { 

      try { 
       InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); 

       View view = ((Activity) context).getCurrentFocus(); 
       if (view != null) { 
        inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
0
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:focusable="true" 
android:focusableInTouchMode="true" 
android:background="@color/background_listview" 
android:orientation="vertical"> 

在主上佈局使用此 設置可聚焦的真實和android:focusableInTouchMode真正

機器人:可調焦= 「真」

機器人:focusableInTouchMode =」 true「