2017-09-02 55 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