【发布时间】:2011-12-17 22:51:49
【问题描述】:
我有一个FragmentActivity 使用ViewPager 来服务多个片段。每个都是ListFragment,布局如下:
<?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);
}
我保存来自onCreateView 的传入ViewGroup container 参数作为访问主要活动的窗口令牌的一种方式。这运行没有错误,但在onStart 中对hideSoftInputFromWindow 的调用不会隐藏键盘。
最初,我尝试使用膨胀布局而不是container,即:
imm.hideSoftInputFromWindow(myInflatedLayout.getWindowToken(), 0);
但这抛出了一个NullPointerException,大概是因为片段本身不是一个活动并且没有唯一的窗口令牌?
有没有办法在片段中隐藏软键盘,或者我应该在FragmentActivity 中创建一个方法并在片段中调用它?
【问题讨论】:
标签: java android kotlin android-fragments android-softkeyboard