在Fragment中关闭软键盘

5

我发现了很多关于这个问题的答案,但是都对我无效。我在Fragment中有一个编辑文本,当应用程序启动时就会启动它。当这个Fragment打开时,软键盘也会弹出来。我该如何防止这种情况发生?以下是我在Fragment中的onCreateView方法中的代码....

        try {
        InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(
                Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(userName.getWindowToken(), 0);
    }catch(Exception e) {
        e.printStackTrace();
    }

请问您能否发布您的XML文件? - Swaminathan V
7个回答

10

请在 onCreateViewonActivityCreated 中尝试此操作。

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

1

我最近的项目中使用了以下代码来隐藏键盘布局,也许你可以尝试一下。(我是从WordPress-Android的源代码中学到的。)

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_template_add_doc, container, false);
    //hide the keyboard if it is visible
    InputMethodManager imm = (InputMethodManager) getActivity()
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
    return view;
}

1
尝试以下逻辑以防止键盘自动打开。
尝试将编辑文本放置在单独的线性布局中,并设置android:focusableInTouchMode="true"。这将自动避免键盘自动打开。
  <LinearLayout
     android:id = "@+id/layout"
     android:layout_width = "wrap_content"
     android:layout_height = "wrap_content"
     android:focusable = "true"
     android:focusableInTouchMode = "true">

    <EditText
       android:id = "@+id/edit_text"
       android:layout_width = "match_content"
       android:layout_height = "wrap_content"/>
  </LinearLayout>

如果上述方法失败,可以使用以下代码以编程方式隐藏。将其编写为单独的函数并在代码中调用。
在视图创建后,在片段中调用此方法,如下所示。
@Override
  public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
     hideKeyboard();
  }

public void hideKeyboard() {
    InputMethodManager inputMethodManager = (InputMethodManager) activity
            .getSystemService(android.content.Context.INPUT_METHOD_SERVICE);

    inputMethodManager.hideSoftInputFromWindow(
            activity.getCurrentFocus()
                    .getWindowToken(), 0);
  } // hideKeyboard

祝你好运..!


1
这对我有效,尝试这种方式。
 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


hideKeyboard(getActivity());
}

    public static void hideKeyboard( Context context ) {

            try {
                InputMethodManager inputManager = ( InputMethodManager ) context.getSystemService( Context.INPUT_METHOD_SERVICE );

                View view = ( (Activity) context ).getCurrentFocus();
                if ( view != null ) {
                    inputManager.hideSoftInputFromWindow( view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS );
                }
            } catch ( Exception e ) {
                e.printStackTrace();
            }
        }

1
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:background="@color/background_listview"
android:orientation="vertical">

在主布局中使用此代码,将其设置为可聚焦并启用触摸模式下的聚焦:

android:focusable="true"

android:focusableInTouchMode="true"


1

您还可以在 AndroidManifest.xml 中添加以下行,以针对片段活动。

要添加的行: android:windowSoftInputMode="stateHidden|adjustResize"

请参见下面的代码片段:

<activity android:name=".activity.FragmentActivity"
       android:windowSoftInputMode="stateHidden|adjustResize"/>

0
以下是在 Kotlin 中的工作内容
private fun Fragment.hideKeyboard() {
        view?.let { activity?.hideKeyboard(it) }
    }

    fun Activity.hideKeyboard() {
        hideKeyboard(currentFocus ?: View(this))
    }

    private fun Context.hideKeyboard(view: View) {
        val inputMethodManager =
            getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
        inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
    }

    private fun Fragment.showKeyboard(et: EditText) {
        view?.let { activity?.showKeyboard(et) }
    }

    private fun Context.showKeyboard(et: EditText) {
        et.requestFocus()
        et.setSelection(et.length()) // This line use to always put cursor at the end of line.
        val inputMethodManager =`enter code here`
            getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
        inputMethodManager.showSoftInput(et, 0)
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接