活动开始时如何隐藏软键盘

Aju*_*Aju 146 android android-softkeyboard

android:windowSoftInputMode="stateVisible"在Manifest中有一个Edittext .现在,当我开始活动时,将显示键盘.如何隐藏它?我无法使用,android:windowSoftInputMode="stateHidden因为当键盘可见时,最小化应用程序并恢复它键盘应该是可见的.我试过了

InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);

但它不起作用.

Nee*_*enu 348

AndroidManifest.xml:

<activity android:name="com.your.package.ActivityName"
          android:windowSoftInputMode="stateHidden"  />
Run Code Online (Sandbox Code Playgroud)

或尝试

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)??;
Run Code Online (Sandbox Code Playgroud)

请检查

  • 感谢`android:windowSoftInputMode ="stateHidden"` (3认同)
  • 实际上,在防止重点放在编辑文本上也有很好的答案http://stackoverflow.com/questions/4668210/automatic-popping-up-keyboard-on-start-activity (2认同)

She*_*tib 202

使用以下功能显示/隐藏键盘:

/**
 * 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);
}
Run Code Online (Sandbox Code Playgroud)

  • 你可以试试这个.如果您从活动中调用它,它会起作用..getWindow()setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); (5认同)
  • Context.INPUT_METHOD_SERVICE用于片段中或不在主要活动中的人等. (4认同)
  • 在Nexus 6P Android 6.0.1上不起作用。 (2认同)

man*_*ani 41

只需将两个属性添加到editText的父视图即可.

android:focusable="true"
android:focusableInTouchMode="true"
Run Code Online (Sandbox Code Playgroud)


San*_*esh 35

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

  android:windowSoftInputMode="stateHidden"  
Run Code Online (Sandbox Code Playgroud)


Adn*_*nan 25

试试这个:

<activity
    ...
    android:windowSoftInputMode="stateHidden|adjustResize"
    ...
>
Run Code Online (Sandbox Code Playgroud)

请看一个了解更多细节.


小智 14

若要隐藏在新活动的时间softkeyboard启动或者onCreate(),onStart()等您可以使用下面的代码:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Run Code Online (Sandbox Code Playgroud)


Ati*_*min 9

使用AndroidManifest.xml

<activity android:name=".YourActivityName"
      android:windowSoftInputMode="stateHidden"  
 />
Run Code Online (Sandbox Code Playgroud)

使用Java

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Run Code Online (Sandbox Code Playgroud)

使用上面的解决方案键盘隐藏但edittext在创建activiy时从焦点开始,但是当你用它们触摸它时抓住它:

添加您的EditText

<EditText
android:focusable="false" />
Run Code Online (Sandbox Code Playgroud)

还添加EditText的监听器

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


Hit*_*its 7

将以下文本添加到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>
Run Code Online (Sandbox Code Playgroud)


Mub*_*har 6

我希望这会有效,我尝试了很多方法,但这个方法对我有用fragments.把这一行放在onCreateview/init上.

getActivity().getWindow().setSoftInputMode(
    WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Run Code Online (Sandbox Code Playgroud)


Gib*_*olt 6

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

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

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

基于用例的替代方案:

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()
}
Run Code Online (Sandbox Code Playgroud)

如何显示软键盘

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

同时请求关注编辑文本时的更简单方法

myEdittext.focus()

fun View.focus() {
    requestFocus()
    showKeyboard()
}
Run Code Online (Sandbox Code Playgroud)

奖金简化:

删除永远使用的要求getSystemServiceSplitties 库

// Simplifies above solution to just
inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
Run Code Online (Sandbox Code Playgroud)


Naj*_*ala 5

将此代码放入您的java文件并在edittext上传递对象的参数,

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


小智 5

要在“新活动”启动时或onCreate(),onStart()方法等时隐藏软键盘,请使用以下代码:

getActivity().getWindow().setSoftInputMode(WindowManager.
LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Run Code Online (Sandbox Code Playgroud)

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

View view = this.getCurrentFocus();

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


Bri*_*hod 5

使用SOFT_INPUT_STATE_ALWAYS_HIDDEN而不是SOFT_INPUT_STATE_HIDDEN

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Run Code Online (Sandbox Code Playgroud)


you*_*sef 5

在您的活动中添加此属性

android:windowSoftInputMode="stateHidden" 
Run Code Online (Sandbox Code Playgroud)


Suj*_*eet 5

以上答案也是正确的。我只想简要说明在启动 Activity 时有两种方法可以从 manifest.xml 中隐藏键盘。例如:

<activity
..........
android:windowSoftInputMode="stateHidden"
..........
/>
Run Code Online (Sandbox Code Playgroud)
  • 上述方式总是在进入活动时隐藏它。

或者

<activity
..........
android:windowSoftInputMode="stateUnchanged"
..........
/>
Run Code Online (Sandbox Code Playgroud)
  • 这个说不要改变它(例如,如果它尚未显示,则不要显示它,但如果它在进入活动时已打开,则将其保持打开状态)。


归档时间:

查看次数:

196101 次

最近记录:

6 年,4 月 前