如何将软键盘隐藏在片段中?
2022-08-31 10:57:25
						我有一个使用a来服务几个片段。每个都是具有以下布局的:FragmentActivityViewPagerListFragment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="8dp">
        <ListView android:id="@id/android:list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
        <EditText android:id="@+id/entertext"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>
启动活动时,将显示软键盘。为了解决这个问题,我在片段中做了以下操作:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    //Save the container view so we can access the window token
    viewContainer = container;
    //get the input method manager service
    imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    . . .
}
@Override
public void onStart() {
    super.onStart();
    //Hide the soft keyboard
    imm.hideSoftInputFromWindow(viewContainer.getWindowToken(), 0);
}
我将传入的参数保存为访问主活动的窗口令牌的一种方式。此操作运行时不会出现错误,但键盘不会从 对 中的调用中隐藏。ViewGroup containeronCreateViewhideSoftInputFromWindowonStart
最初,我尝试使用膨胀布局而不是,即:container
imm.hideSoftInputFromWindow(myInflatedLayout.getWindowToken(), 0);
但是这抛出了一个,大概是因为片段本身不是活动并且没有唯一的窗口令牌?NullPointerException
有没有办法从片段中隐藏软键盘,或者我应该在 中创建一个方法并从片段中调用它?FragmentActivity
 
					 
				 
				    		 
				    		 
				    		 
				    		