当 activity 开始时如何隐藏软键盘

我使用 Edittext 且在 Manifest 中配置了 android:windowSoftInputMode="stateVisible"。现在,当我开始这个 activity 时,键盘就会显示出来。怎么隐藏键盘?我不能使用 android:windowSoftInputMode="stateHidden,因为当键盘是可见的,然后最小化应用程序,恢复它的键盘应该是可见的。 我试过如下方法

InputMethodManager imm = (InputMethodManager) getSystemService (INPUT _ Method _ SERVICE) ; GetCurrentFocus () . getWindowToken () ,0) ;

但是没有成功。

221845 次浏览

使用以下函数显示/隐藏键盘:

/**
* Hides the soft keyboard
*/
public void hideSoftKeyboard() {
if(getCurrentFocus()!=null) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}


/**
* Shows the soft keyboard
*/
public void showSoftKeyboard(View view) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
view.requestFocus();
inputMethodManager.showSoftInput(view, 0);
}

AndroidManifest.xml:

<activity android:name="com.your.package.ActivityName"
android:windowSoftInputMode="stateHidden"  />

或者尝试

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)‌​;

请检查 这个

试试这个:

<activity
...
android:windowSoftInputMode="stateHidden|adjustResize"
...
>

更多细节请看 这个1。

这个也试试

Ed_Cat_Search = (EditText) findViewById(R.id.editText_Searc_Categories);


Ed_Cat_Search.setInputType(InputType.TYPE_NULL);


Ed_Cat_Search.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
Ed_Cat_Search.setInputType(InputType.TYPE_CLASS_TEXT);
Ed_Cat_Search.onTouchEvent(event); // call native handler
return true; // consume touch even
}
});

为了在新活动开始或者 onCreate()onStart()等时隐藏软键盘,你可以使用下面的代码:

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

将以下文本添加到 xml 文件中。

<!--Dummy layout that gain focus -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical" >
</LinearLayout>

将这段代码放入 java 文件,并将对象的参数传递到 edtext 中,

private void setHideSoftKeyboard(EditText editText){
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}

我就是这么做的:

yourEditText.setCursorVisible(false); //This code is used when you do not want the cursor to be visible at startup
yourEditText.setOnTouchListener(new View.OnTouchListener() {


@Override
public boolean onTouch(View v, MotionEvent event) {
v.onTouchEvent(event);   // handle the event first
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {


imm.hideSoftInputFromWindow(v.getWindowToken(), 0);  // hide the soft keyboard
yourEditText.setCursorVisible(true); //This is to display cursor when upon onTouch of Edittext
}
return true;
}
});

试试这个。

首先,在可搜索的 xml 中,字段(名称和提示等)放入 @string,而不是文字字符串。

然后方法 onCreateOptionsMenu,它必须有一个 ComponentName对象与您的软件包名称和您完整的类名称(与软件包名称)-如果活动,其中有 SearchView组件是相同的显示搜索结果使用 getComponentName(),正如谷歌 android 开发人员所说。

我尝试了很多解决方案,经过大量的工作,这个解决方案为我工作。

只需将两个属性添加到 edit Text 的父视图。

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

我希望这将工作,我尝试了很多方法,但这一个为我在 fragments工作。把这一行放在创建视图/init 中。

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

您可以在 AndroidManifest.xml 上设置配置

例如:

<activity
android:name="Activity"
android:configChanges="orientation|keyboardHidden"
android:theme="@*android:style/Theme.NoTitleBar"
android:launchMode="singleTop"
android:windowSoftInputMode="stateHidden"/>

将其放在 Activity 标记内的清单中

  android:windowSoftInputMode="stateHidden"

启动活动时,使用以下代码首次隐藏软键盘

getActivity().getWindow().setSoftInputMode(WindowManager.
LayoutParams.SOFT_INPUT_STATE_HIDDEN);

如果应用程序的目标是 Android API 级别21或更高,则有一个默认方法可用。

editTextObj.setShowSoftInputOnFocus(false);

确保在 EditText XML 标记中设置了以下代码。

<EditText
....
android:enabled="true"
android:focusable="true" />
Ed_Cat_Search = (EditText) findViewById(R.id.editText_Searc_Categories);


Ed_Cat_Search.setInputType(InputType.TYPE_NULL);


Ed_Cat_Search.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
Ed_Cat_Search.setInputType(InputType.TYPE_CLASS_TEXT);
Ed_Cat_Search.onTouchEvent(event); // call native handler
return true; // consume touch even
}
});


this one worked for me
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

会成功的

要在 New Activity start 或 onCreate ()、 onStart ()方法等时隐藏软键盘,请使用下面的代码:

getActivity().getWindow().setSoftInputMode(WindowManager.
LayoutParams.SOFT_INPUT_STATE_HIDDEN);

在按钮时隐藏软键盘是在活动中单击:

View view = this.getCurrentFocus();


if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
assert imm != null;
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

使用 AndroidManifest.xml

<activity android:name=".YourActivityName"
android:windowSoftInputMode="stateHidden"
/>

使用 爪哇咖啡

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

使用上面的解决方案,键盘隐藏,但编辑文本从焦点时,创建活动,但抓住它,当你触摸他们使用:

加入你的 编辑文本

<EditText
android:focusable="false" />

还要添加 EditText 的侦听器

youredittext.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.setFocusable(true);
v.setFocusableInTouchMode(true);
return false;
}});

使用 SOFT _ INPUT _ STATE _ ALWAYS _ HIDDEN 代替 SOFT _ INPUT _ STATE _ HIDDEN

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

将你的活动加入到清单中 这处房产

android:windowSoftInputMode="stateHidden"

如果不想使用 xml,可以创建一个 Kotlin 扩展来隐藏键盘

// In onResume, call this
myView.hideKeyboard()


fun View.hideKeyboard() {
val inputMethodManager = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
}

基于用例的替代方案:

fun Fragment.hideKeyboard() {
view?.let { activity?.hideKeyboard(it) }
}


fun Activity.hideKeyboard() {
// Calls Context.hideKeyboard
hideKeyboard(currentFocus ?: View(this))
}


fun Context.hideKeyboard(view: View) {
view.hideKeyboard()
}

如何使用 < em > show 软键盘

fun Context.showKeyboard() { // Or View.showKeyboard()
val inputMethodManager = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.toggleSoftInput(SHOW_FORCED, HIDE_IMPLICIT_ONLY)
}

当同时请求焦点在编辑文本上时,使用更简单的方法

myEdittext.focus()


fun View.focus() {
requestFocus()
showKeyboard()
}

额外的简化:

取消永远使用 getSystemService: 分裂图书馆的要求

// Simplifies above solution to just
inputMethodManager.hideSoftInputFromWindow(windowToken, 0)

以上答案也是正确的。我只想简单介绍一下在启动活动时隐藏键盘的两种方法,即在 Manif.xml 中隐藏键盘。 例如:

<activity
..........
android:windowSoftInputMode="stateHidden"
..........
/>
  • 以上方法总是在进入活动时隐藏它。

或者

<activity
..........
android:windowSoftInputMode="stateUnchanged"
..........
/>
  • 这里说不要更改它(例如,如果它还没有显示,就不要显示,但是如果它在进入活动时是打开的,就让它保持打开状态)。