i在下一个视图上具有带有EditText元素的应用程序。这意味着,当我的应用程序加载时,每个默认值出现软键盘。
我用什么代码在Intellij上隐藏此键盘?
更新
最简单的方法
在您的onCreate()
this.getWindow().setSoftInputMode
(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
有时这行不通。因此,您可以做的是将editText
-> focusable
作为false
内的CC_3。
用于隐藏键盘,您可以有两种方法:
要么:(这将隐藏应用程序/活动启动上的键盘)
将此android:windowSoftInputMode="adjustPan"
放入subtest.xml中相关活动的活动标签中:
<activity
android:name=".MainActivity"
android:windowSoftInputMode="adjustPan" />
或:
将此方法放入您的活动中,并在您必须隐藏键盘时致电。
@SuppressWarnings("ConstantConditions")
public void hideKeyBoard(View view) {
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
并将其称为hideKeyBoard(view);
。
记住,您必须传递视图以隐藏键盘上述。
这是我显示和隐藏键盘的方法,从活动或片段中调用
public void hideKeyboard(final boolean hide) {
// Check if no view has focus:
View view = this.getCurrentFocus();
if (view == null) {
return;
}
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (hide) {
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
} else {
new Handler().postDelayed(() -> imm.showSoftInput(view, 0), 50);
}
}
在明显的put android中:windowsoftinputmode =" ratchpan"在您的活动下您要隐藏软键盘的活动,例如:
<activity android:name=".Viewschedule"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan"></activity>
或
InputMethodManager imgr = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imgr.showSoftInput(view, 0);