如何从片段内隐藏软键盘?

Wil*_*all 80 java android android-softkeyboard android-fragments

我有一个FragmentActivity使用a 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>
Run Code Online (Sandbox Code Playgroud)

启动活动时,软键盘会显示.为了解决这个问题,我在片段中做了以下内容:

@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);
}
Run Code Online (Sandbox Code Playgroud)

我保存传入的ViewGroup container参数onCreateView作为访问主要活动的窗口令牌的方式.这样运行没有错误,但键盘不会从调用hideSoftInputFromWindow中隐藏onStart.

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

imm.hideSoftInputFromWindow(myInflatedLayout.getWindowToken(), 0);
Run Code Online (Sandbox Code Playgroud)

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

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

Ian*_*ton 172

只要您的Fragment创建了一个View,就可以在连接使用该视图中的IBinder(窗口标记).例如,您可以覆盖片段中的onActivityCreated:

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

  • 我将此添加到我的项目中.但是当我点击另一个选项卡时它会崩溃. (9认同)
  • 对于那些正在执行此操作并使用NullPointerException进行崩溃的人,只需在片段onCreateView方法中使用InputMethodManager.这样做,您将获得您的视图,您可以使用已膨胀到imm.hideSoftInputFromWindow(view.getWindowToken(),0)的视图更改最后一行; (4认同)

Sha*_*zal 78

除了以下代码行之外,我只能为我工作:

getActivity().getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Run Code Online (Sandbox Code Playgroud)

  • 也是唯一对我有用的东西! (7认同)
  • `SOFT_INPUT_STATE_HIDDEN` 也对我有用,尽管我不知道它和 `SOFT_INPUT_STATE_ALWAYS_HIDDEN' 之间有什么区别。 (2认同)
  • 第一个答案没有用,这个就行了.谢谢 (2认同)
  • 只有这个解决方案适合我. (2认同)

Eri*_*enz 20

如果将以下属性添加到活动的清单定义中,则会在活动打开时完全禁止键盘弹出.希望这有助于:

(添加到Activity的清单定义中):

android:windowSoftInputMode="stateHidden"
Run Code Online (Sandbox Code Playgroud)


小智 12

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_my, container,
                false);
        someClass.onCreate(rootView);
        return rootView;
    }
Run Code Online (Sandbox Code Playgroud)

在我的班级中保留我的根视图的实例

View view;

public void onCreate(View rootView) {
    view = rootView;
Run Code Online (Sandbox Code Playgroud)

使用视图隐藏键盘

 public void removePhoneKeypad() {
    InputMethodManager inputManager = (InputMethodManager) view
            .getContext()
            .getSystemService(Context.INPUT_METHOD_SERVICE);

    IBinder binder = view.getWindowToken();
    inputManager.hideSoftInputFromWindow(binder,
            InputMethodManager.HIDE_NOT_ALWAYS);
}
Run Code Online (Sandbox Code Playgroud)


mac*_*Jun 9

DialogFragment但是,例外情况是Dialog必须隐藏嵌入式的焦点,而不是EditText嵌入式中的第一个Dialog

this.getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Run Code Online (Sandbox Code Playgroud)

  • 如果你有DialogFragment,这是隐藏键盘的唯一方法. (4认同)

Kes*_*era 6

在Fragemnt中使用此代码

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Run Code Online (Sandbox Code Playgroud)


Naz*_*dat 5

从您喜欢的任何地方(活动/片段)使用此静态方法。

public static void hideKeyboard(Activity activity) {
    try{
        InputMethodManager inputManager = (InputMethodManager) activity
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        View currentFocusedView = activity.getCurrentFocus();
        if (currentFocusedView != null) {
            inputManager.hideSoftInputFromWindow(currentFocusedView.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }catch (Exception e){
        e.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)

如果你想使用片段只需调用hideKeyboard(((Activity) getActivity())).