Android 如何防止关注活动开始

Android 如何防止关注活动开始,android,android-softkeyboard,Android,Android Softkeyboard,我有一个活动,只有一个EdtiText。当该活动启动时,编辑文本将被聚焦并显示软键盘。这似乎发生在onResume之后,因为当我以编程方式将键盘隐藏在onResume中时,键盘不起作用。当我这样做时: @Override protected void onResume() { super.onResume(); new Handler().postDelayed(new Runnable() { @Override public void run

我有一个
活动
,只有一个
EdtiText
。当该
活动
启动时,
编辑文本
将被聚焦并显示软键盘。这似乎发生在
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
上的事件来防止键盘弹出吗?或者用其他方式阻止它的出现


更新
focusableintouch模式
不符合我的要求,因为当设置为
true
时,键盘会弹出,当设置为
false
时,键盘根本不可聚焦

对于父布局
android:focusableInTouchMode=“true”
对于父布局
android:focusableInTouchMode=“true”
您可以如下设置布局的属性

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

您可以设置布局的属性,如

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

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

在舱单中:

android:windowSoftInputMode="stateHidden|stateAlwaysHidden"
在布局中:

android:focusable="true"
android:focusableInTouchMode="true"
最后但并非最不重要的一点是,触摸listener设置为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);
    }
}

希望有帮助,而且我没有忘记任何事情:)

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

在舱单中:

android:windowSoftInputMode="stateHidden|stateAlwaysHidden"
在布局中:

android:focusable="true"
android:focusableInTouchMode="true"
最后但并非最不重要的一点是,触摸listener设置为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);
    }
}

希望有帮助,而且我没有忘记任何事情:)

如果这些问题有帮助,如果这些问题有帮助,这不起作用,请参阅我的更新。设置为
true
,键盘仍会弹出,当
false
不可聚焦时,键盘仍会弹出。这很奇怪。可能在清单中,您没有WindowsOfInputMode的默认实现,或者您的布局中有requestFocus。这不起作用,请参阅我的更新。设置为
true
,键盘仍会弹出,当
false
不可聚焦时,键盘仍会弹出。这很奇怪。可能在清单中您没有WindowsOfInputMode的默认实现,或者可能在布局xml中有requestFocus