android-fragments 正在关闭片段中的软键盘

7rtdyuoh  于 2022-11-14  发布在  Android
关注(0)|答案(7)|浏览(213)

我找到了这个问题的许多答案,但是没有一个对我有效。我的Fragment中有一个Edit text,它在应用程序启动时启动。当这个Fragment打开时,软键盘也会弹出。我如何防止这种情况发生?这是我Fragment中onCreateView方法中的内容...

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

v1uwarro1#

请在onCreateViewonActivityCreated中尝试此操作。

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

wgeznvg72#

我最近的项目我用下面的代码来隐藏键盘布局,也许你可以试试.(我从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;
}
hec6srdp

hec6srdp3#

尝试以下逻辑以隐藏键盘,使其不自动打开。
尝试将编辑文本放在单独的线性布局中,并设置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

祝您好运!

idv4meu8

idv4meu84#

这对我很有效,试试这样

@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();
            }
        }
4zcjmb1e

4zcjmb1e5#

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

在“主布局”上部使用此设置,将focusable设置为true,将android:focusableInTouchMode设置为true
android:可聚焦=“true”
安卓系统:可聚焦触控模式=“true”

t8e9dugd

t8e9dugd6#

您也可以将AndroidManifest.xml中的这一行添加到您的片段Activity中。
要添加的行:- android:窗口软输入模式=“状态隐藏|调整调整大小”
请参阅以下代码片段:-

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

x0fgdtte7#

下面是在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)
    }

相关问题