如何在片段中隐藏软键盘? [英] How to hide the soft keyboard inside a fragment?

查看:32
本文介绍了如何在片段中隐藏软键盘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 FragmentActivity 使用 ViewPager 来提供多个片段.每个都是一个具有以下布局的 ListFragment:

I have a FragmentActivity using a ViewPager to serve several fragments. Each is a ListFragment with the following layout:

<?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>

开始活动时,软键盘显示.为了解决这个问题,我在片段中执行了以下操作:

When starting the activity, the soft keyboard shows. To remedy this, I did the following inside the fragment:

@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 容器 参数,作为访问主要活动的窗口令牌的一种方式.这运行没有错误,但键盘不会从 onStart 中对 hideSoftInputFromWindow 的调用中隐藏起来.

I save the incoming ViewGroup container parameter from onCreateView as a way to access the window token for the main activity. This runs without error, but the keyboard doesn't get hidden from the call to hideSoftInputFromWindow in onStart.

最初,我尝试使用膨胀布局而不是 container,即:

Originally, I tried using the inflated layout instead of container, i.e:

imm.hideSoftInputFromWindow(myInflatedLayout.getWindowToken(), 0);

但是这抛出了一个 NullPointerException,大概是因为片段本身不是一个活动并且没有唯一的窗口令牌?

but this threw a NullPointerException, presumably because the fragment itself isn't an activity and doesn't have a unique window token?

有没有办法在片段中隐藏软键盘,还是应该在 FragmentActivity 中创建一个方法并从片段中调用它?

Is there a way to hide the soft keyboard from within a fragment, or should I create a method in the FragmentActivity and call it from within the fragment?

推荐答案

只要你的 Fragment 创建了一个视图,你就可以在它被附加后使用该视图中的 IBinder(窗口令牌).例如,您可以在 Fragment 中覆盖 onActivityCreated:

As long as your Fragment creates a View, you can use the IBinder (window token) from that view after it has been attached. For example, you can override onActivityCreated in your Fragment:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
}

这篇关于如何在片段中隐藏软键盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆