2017-09-02 52 views
0

當我的碎片開始時 - 我想讓鑰匙走開!如何使鍵盤在一些片段的開始處不可見?

我試試這個(不工作..)

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_behavior, container, false); 


    // 
    if (view.requestFocus()) { 
     InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); 
     imm.showSoftInput(view, InputMethodManager.imm.showSoftInput(view, InputMethodManager.HIDE_IMPLICIT_ONLY); 

    } 

我也嘗試定義

 <activity android:name="com.your.package.ActivityName" 
    android:windowSoftInputMode="stateHidden" /> 

但也不能工作

請...任何幫助:(

謝謝

+0

你是如何加入片段到活動這個代碼?我的意思是在ViewPager,FrameLayout通過片段交易,從XML和你可以發佈您的片段的xml –

回答

1

嘗試內onCreateView()

try { 

    InputMethodManager input = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE); 

    input.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0); 

}catch(Exception e) { 
    e.printStackTrace(); 
} 
+0

無法正常工作..鍵盤仍然聚焦此片段時仍然起來 – Yanshof

+0

嘗試重建您的項目和檢查,因爲這個代碼在我的機器上工作,也不需要在清單文件中定義片段。 –

+0

重建沒有幫助 - 仍然不起作用 – Yanshof

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

不工作..鍵盤仍然聚焦此片段時仍然起來 – Yanshof

1
/***Just try the below code Snipet***/ 

Inside your onCreateView of Fragment 

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); 

And put the below piece of code inside your Main Fragment activity tag in manifest file 

android:windowSoftInputMode="stateVisible|adjustResize"  
+0

當keybaord被隱藏 - 在我的用戶界面上,我會看到一些按鈕更大 - 現在這個按鈕更大,但keybaord仍然顯示 – Yanshof