【问题标题】:How to prevent focus on Activity start [duplicate]如何防止关注活动启动[重复]
【发布时间】:2018-03-11 07:42:16
【问题描述】:

我有一个Activity,只有一个EdtiText。当Activity 启动时,EditText 被聚焦并显示软键盘。这似乎发生在onResume 之后,因为当我以编程方式将键盘隐藏在onResume 中时,它不起作用。当我这样做时:

@Override
protected void onResume() {
    super.onResume();

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
            //Find the currently focused view, so we can grab the correct window token from it.
            //If no view currently has focus, create a new one, just so we can grab a window token from it
            imm.hideSoftInputFromWindow(etBarcode.getWindowToken(), 0);
        }
    }, 500);
}

它会隐藏它(很快就会弹出)。

EditText 上是否有事件可以用来防止键盘弹出?或者其他阻止它显示的方法?

更新 focusableInTouchMode 没有做我想做的事,因为当设置为true 时,键盘会弹出,当设置为false 时,它根本无法聚焦。

【问题讨论】:

标签: android android-softkeyboard


【解决方案1】:

对于父布局android:focusableInTouchMode="true"

【讨论】:

  • 这不起作用,请参阅我的更新。设置为true,键盘依然弹起,false时不对焦。
  • 这很奇怪。也许在清单中你没有默认实现 windowSoftInputMode 或者你的布局 xml 中有 requestFocus
【解决方案2】:
// Add following code in activity onCreate
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

【讨论】:

    【解决方案3】:

    你可以像这样设置布局的属性

    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true"
    

    【讨论】:

      【解决方案4】:

      这个问题非常复杂,因为它是关于视图获得焦点,以及所有布局如何处理它,关于触摸模式是可聚焦的,最后但并非最不重要的是软键盘如何处理它。 但这对我有用:

      在清单中:

      android:windowSoftInputMode="stateHidden|stateAlwaysHidden"
      

      在布局中:

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

      最后但同样重要的是,将触摸侦听器设置为 EditText 以防止软键盘在触摸后显示:

              mMyText.setOnTouchListener(new View.OnTouchListener() {
              @Override
              public boolean onTouch(View v, MotionEvent event) {
                  // forward the touch event to the view (for instance edit text will update the cursor to the touched position), then
                  // prevent the soft keyboard from popping up and consume the event
                  v.onTouchEvent(event);
                  disableSoftKeyboard(MyActivity.this);
                  return true;
              }
          });
      

      而该方法或多或少地做了你已经在做的事情:

      public void disableSoftKeyboard(@NonNull Activity activity) {
          View view = activity.getCurrentFocus();
          if (view != null) {
              InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
              imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
          } else {
              activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
          }
      }
      

      希望它有所帮助,也希望我没有忘记任何事情:)

      【讨论】:

        猜你喜欢
        • 2013-04-14
        • 1970-01-01
        • 2020-12-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多