【问题标题】:EditText request focus not workingEditText 请求焦点不起作用
【发布时间】:2013-07-09 07:05:28
【问题描述】:

我有一个EditText 和一个Button。单击按钮时,我想打开EditText 键盘,同时请求关注EditText,以便用户直接开始在键盘上输入,文本出现在EditText 中。 但在我的情况下,当我单击按钮时,它会打开键盘,但不会将焦点设置在EditText 上,因此用户必须再次单击EditText 才能在上面写字。什么问题。任何帮助或建议。

点击按钮时的代码

m_SearchEditText.requestFocus();
InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(m_SearchEditText.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);

【问题讨论】:

标签: android android-edittext


【解决方案1】:

确保编辑文本在触摸模式下可聚焦。你可以通过两种方式做到这一点。

在xml中:

android:focusableInTouchMode="true"

在 Java 中:

view.setFocusableInTouchMode(true);

我个人不相信这个参数的 XML 定义。我总是通过这两行代码来请求焦点:

view.setFocusableInTouchMode(true);
view.requestFocus();

键盘应该出现在自己身上,无需调用 InputMethodManager。

它适用于大多数情况。一旦它对我不起作用,因为我由于处理繁重而丢失了对该对象的引用 - 请确保这不是您的问题。

【讨论】:

  • 我有同样的问题,这对我不起作用。这个问题是一个看似没有通用解决方案的常见问题。我尝试了其他人建议的几种技巧,但似乎对我的情况没有任何作用。
  • 很惊讶这没有更多的赞成票。一整天都在查看 StackOverflow,这是唯一一个将 EditText 视图设置为可在我的 Activity 加载时聚焦的视图。现在我只需要弄清楚如何让键盘不加载,除非用户使用此解决方案单击它。
  • 这样一个投票不足的答案,但是一个 GEM!像魅力一样工作!
  • android:focusableInTouchMode="true" in xml 对我有用
【解决方案2】:

在我的情况下,它通过在您单击按钮并在另一个视图中设置焦点后添加一个处理程序来工作,焦点可以返回到您需要的视图。

只需将其放入您的代码中即可:

final Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        lastview.getEditText().clearFocus();
                        m_SearchEditText.requestFocus();
                        InputMethodManager mgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);


                        mgr.showSoftInput(mLastNameET, InputMethodManager.SHOW_IMPLICIT);

                    }
                }, 100);

希望对你有帮助

【讨论】:

  • 不需要创建一个新的Handler,只需要使用视图本身:lastview.getEditText().post(new Runnable....)
  • 什么是lastview?
  • @r3dm4n -- 它是相关 EditText 的变量名称。看看萨利赫回答的第 5 行就知道了。
【解决方案3】:

以下内容对我有用,应该会有所帮助:

EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);

【讨论】:

【解决方案4】:

在你的 ma​​nifest.xml 中写:

<activity android:name=".MainActivity"
android:label="@string/app_name"
android:windowSoftInputMode="stateAlwaysVisible" />

然后在oncreate() 中致电m_SearchEditText.requestfocus()
或者,
试试:

if(m_SearchEditText.requestFocus()) {
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}

【讨论】:

    【解决方案5】:

    作为this answer 的扩展(由于声誉原因,我没有将其添加为评论...)。

    如果您想将延迟时间减少到零,请改用 handler.post()。 完整代码:

    final Handler handler = new Handler();
    handler.post(new Runnable() {
        @Override
        public void run() {
            lastview.getEditText().clearFocus();
            m_SearchEditText.requestFocus();
            InputMethodManager mgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    
    
            mgr.showSoftInput(mLastNameET, InputMethodManager.SHOW_IMPLICIT);
        }
    });
    

    【讨论】:

    • @Matis Olocco mLastNameET 到底是什么,它是像 EditText 这样的视图对象类型,还是来自 xml 布局 ID 的编辑文本?因为这个方法只接受 int , int 参数。 \
    • @Khay 您应该询问原始答案的作者。如果您阅读了我的答案,那只是它的扩展。我没有写是作为评论,因为当时我没有足够的积分。
    【解决方案6】:

    极简的 Kotlin 扩展版本,因为我们应该假设这些对系统服务的钝调用是不必要的:

    fun EditText.requestKeyboardFocus() {
        val inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        inputMethodManager.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
    }
    

    【讨论】:

      【解决方案7】:

      根据saleh sereshki 的回答和pauminku 的评论,我正在使用:

      m_SearchEditText.post(new Runnable() {
                  @Override
                  public void run() {
                      m_SearchEditText.requestFocus();
                  }
              });
      

      在 Kotlin 中相当于:

      m_SearchEditText.post { m_SearchEditText.requestFocus() }
      

      【讨论】:

      • 完美地工作,.post 允许您将任何视图集中在屏幕上。大多数其他答案可能已经从 android 给予焦点以正常的级联方式“起作用”
      【解决方案8】:

      你还需要两个 xml 属性来实现这一点:

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

      将它们添加到 EditText 以及父布局(这些视图所在的任何布局)。默认情况下,这些都是 false,因此焦点不会放在请求的视图上。

      来源:https://developer.android.com/reference/android/view/View.html#attr_android:focusable

      根据复选框选择显示 EditText 后,在代码中动态添加下一个和上一个焦点。

      希望这会有所帮助。

      【讨论】:

      • 在 API 19 上我只有 android:focusableInTouchMode="true" 和 但键盘没有出现。添加 android:focusable="true" 修复它。
      【解决方案9】:

      你可以使用这个功能来集中注意力

      editText.requestFocus()
      

      EditText 可能有两个不同的问题。一个是EditText 将被聚焦但键盘不会打开,所以正如其他答案所说,你必须手动打开键盘。另一个问题是EditText 根本不专注意味着什么都没发生。

      对于打开键盘,您可以在requestFocus 之后执行此操作:

      val manager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager
      manager?.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
      

      如果EditText 根本没有关注,请检查此属性。 EditText 必须是可聚焦、可见、启用、focusableInTouchMode。在调用requestFocus之前启用所有这些。

      this.isFocusable = true
      this.isFocusableInTouchMode = true
      this.visibility = View.VISIBLE
      this.isEnabled = true
      

      最后你可以使用这个 kotlin 扩展来为你做这一切:

      fun EditText.focus() {
          this.isFocusable = true
          this.isFocusableInTouchMode = true
          this.visibility = View.VISIBLE
          this.isEnabled = true
          this.isCursorVisible = true
          this.post {
              (this.context as? Activity)?.runOnUiThread {
                  this.requestFocus()
                  val manager =
                      this.context.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager
                  manager?.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
              }
          }
      }
      

      【讨论】:

        【解决方案10】:

        在我的情况下,上述答案都不起作用(例如,它们在 Nox 中不起作用)。要将焦点设置为 EditText您可以欺骗它认为用户实际点击了它

        所以我使用MotionEvent,像这样:

        // simulate a click, which consists of ACTION_DOWN and ACTION_UP
        MotionEvent eventDown = MotionEvent.obtain(System.currentTimeMillis(), System.currentTimeMillis(), MotionEvent.ACTION_DOWN, 0, 0, 0);
        editText.dispatchTouchEvent(eventDown);
        eventDown.recycle();
        
        MotionEvent eventUp = MotionEvent.obtain(System.currentTimeMillis(), System.currentTimeMillis(), MotionEvent.ACTION_UP, 0, 0, 0);
        editText.dispatchTouchEvent(eventUp);
        eventUp.recycle();
        
        // To be on the safe side, also use another method
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        editText.requestFocusFromTouch();
        

        【讨论】:

          【解决方案11】:

          如果启用 EditText,上述解决方案将起作用。

          在我的代码中,我曾一度禁用了视图,并试图稍后在不启用的情况下请求焦点。 如果上述方法均无效,请启用 EditText,然后继续执行上述解决方案。

          就我而言,

           etpalletId.isEnabled = true
           etpalletId.text!!.clear()
           etpalletId.requestFocus()
           etpalletId.isCursorVisible = true
          

          【讨论】:

            【解决方案12】:

            我正在结合一些答案并找到以下解决方案:

            fun Fragment.focusEditText(editText: EditText) {
            Timer("Timer", false).schedule(50) {
                requireActivity().runOnUiThread(java.lang.Runnable {
                    editText.isFocusableInTouchMode = true
                    editText.requestFocus()
                    val manager =
                        requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                    manager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
                })
            }
            

            计时器延迟确保焦点请求正常工作并且手动打开键盘,因为它对我没有隐式工作。

            【讨论】:

              【解决方案13】:

              对于我的代码中的一些特殊情况,我会这样做:

              later {
                  showKeyboard()
                  titleEdit.requestFocus()
              }
              

              在正常代码中应该是这样的:

              view.post {
                  (view.context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager)
                      .toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,
                          InputMethodManager.HIDE_IMPLICIT_ONLY)
                  titleEdit.requestFocus()
              }
              

              说明:在某些情况下,toggleSoftInput 似乎是唯一的方法,但不知何故它会窃取焦点,并且在某些情况下它仅在初始化后的代码 tun 中工作,因此必须发布以供以后执行。

              【讨论】:

                【解决方案14】:

                确保您的视图可聚焦,否则将其设置为可聚焦:

                yourView.setFocusable(true);
                yourView.setFocusableInTouchMode(true);
                

                然后设置焦点如下:

                yourView.requestFocus() 
                

                如果这不起作用,请使用:

                yourView.post(new Runnable() {
                @Override
                public void run() {
                    yourView.requestFocus();
                }
                });
                

                这将起作用,其背后的原因是没有加载整个 UI,以便将焦点设置在特定的 UI 元素上。在这种情况下,可能会忽略您的明确请求的系统会覆盖焦点。 因此,如果您使用后台线程(是的,我的意思是使用 Thread 或 Runnable 类)请求焦点,它应该可以解决问题。

                欲了解更多信息,请访问此链接: https://medium.com/@saishaddai/til-requestfocus-not-working-22760b417cf9

                【讨论】:

                  猜你喜欢
                  • 2011-12-26
                  • 1970-01-01
                  • 1970-01-01
                  • 2019-07-08
                  • 1970-01-01
                  • 1970-01-01
                  • 2017-02-14
                  • 2012-10-11
                  • 2013-06-08
                  相关资源
                  最近更新 更多