【问题标题】:Android: EditText inside Scrollview: block softkeyboard autoshowAndroid:Scrollview内的EditText:阻止软键盘自动显示
【发布时间】:2015-07-29 06:07:25
【问题描述】:

当 Activity 在滚动视图中有 EditText 时,软键盘会自动显示。

但是EditText并不是Activity的主要特征。

如何防止SoftKeyboard在Activity创建时自动显示?

有些事情不能解决问题:

  • 添加代码以隐藏软键盘会导致重新显示时出现问题:

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

  • 或在清单中:android:windowSoftInputMode="stateAlwaysHidden"

【问题讨论】:

标签: android android-edittext scrollview android-softkeyboard


【解决方案1】:

您可以在 onCreate 方法中使用此代码...

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

【讨论】:

    【解决方案2】:

    你可以使用 edittext.clearFocus(); 或添加 android:windowSoftInputMode="stateHidden"

    【讨论】:

      【解决方案3】:

      您只需移除 EditText 焦点。

      1-将android:windowSoftInputMode="stateHidden"添加到清单中的活动标签

      2-使用this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

      3-使用edittext.clearFocus();

      4-套

          android:focusable="true"
          android:focusableInTouchMode="true" 
      

      换个角度看。

      注意:edittext.clearFocus();可能不起作用,因为它将焦点设置回活动中的另一个可聚焦视图,因此对于一个视图,它只是将焦点能力重置为同一视图。

      【讨论】:

      • 不,它实际上并没有清除焦点,只是将焦点设置在另一个视图上,当执行另一个 onClick 事件时,editText 将重新获得焦点能力,请查看文档:developer.android.com/reference/android/widget/EditText.html
      • 解决方案 1 和 2 导致重新显示软键盘时出现问题。
      • 你需要一个答案,或者你想给我一个哲学导论讲座。答案高于你接受它,因为它是对还是错,还有一件事你需要删除你的 cmets,因为它不相关和错误
      【解决方案4】:

      阻止软键盘自动显示的正确方法是防止它专注于EditText。

      对于布局中的每个编辑文本:

      onCreate()
          editText = (EditText)findViewById(R.id.edittext);
          editText.setFocusable(false);
      

      在您的 xml 布局文件中:

      <EditText
          ...
          android:onClick="enableFocusable"
      

      那么在java类中,一个方法……

      public void enableFocusable(View view) {
          editText.setFocusableInTouchMode(true);
          editText.requestFocus();
          if (view.requestFocus()) {
              InputMethodManager imm = (InputMethodManager)
                      getSystemService(Context.INPUT_METHOD_SERVICE);
              imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-04
        • 2013-08-23
        • 1970-01-01
        • 1970-01-01
        • 2023-03-08
        • 1970-01-01
        • 1970-01-01
        • 2021-05-22
        相关资源
        最近更新 更多