Android中使用showSoftInput无法显示软键盘?

43

我创建了一个简单的应用程序来测试以下功能。当我的活动启动时,需要打开软键盘。

我的代码不起作用?!

我已经尝试在清单中使用不同的“状态”设置以及在代码中使用InputMethodManager(imm)的不同标志。

我已经将设置包含在AndroidManifest.xml中,并在唯一活动的onCreate中显式调用。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.mycompany.android.studyIme"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".StudyImeActivity"
                  android:label="@string/app_name" 
                  android:windowSoftInputMode="stateAlwaysVisible">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

...主要布局(main.xml)...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello"
        />
    <EditText
        android:id="@+id/edit_sample_text"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:hint="@string/hello"
        android:inputType="textShortMessage"
    />
</LinearLayout>
...和代码...
public class StudyImeActivity extends Activity {
    private EditText mEditTextStudy;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mEditTextStudy = (EditText) findViewById(R.id.edit_study);
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(mEditTextStudy, InputMethodManager.SHOW_FORCED);
    }
}

嗯...我刚刚尝试了一下,在我的Sprint LG Optimus手机上,只使用默认的“HelloWorld”活动(即不包括SHOW_FORCED代码),它按预期工作。这个功能可能与设备(安装的操作系统)有关吗?当我回家后,我会在我的其他设备(HTC、G2和MyTouch)上再次测试。 - mobibob
请看我的回复,我已经尝试了下面提到的所有技术,但是这个有效:https://dev59.com/w3E95IYBdhLWcg3wDpig#37529370 - strangetimes
17个回答

63

当活动启动时,似乎键盘最初是显示的,但被其他东西隐藏了,因为以下方法有效(但实际上是一种不太好的解决方法):

第一种方法

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
editText.postDelayed(new Runnable()
{
    @Override
    public void run()
    {
        editText.requestFocus();
        imm.showSoftInput(editText, 0);
    }
}, 100);

第二种方法

在 onCreate 方法中启动它,以在活动创建时运行。

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() 
    {
    //  InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    //    inputMethodManager.toggleSoftInputFromWindow(EnterYourViewHere.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);

        if (inputMethodManager != null)
        {
            inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
        } 
    }
}, 200);

第三种方法 将给定的代码添加到Manifest中的activity标签中。这将在启动时显示键盘,并将焦点设置为你想要的视图。

android:windowSoftInputMode="stateVisible"

4
我有同样的问题。立即调用showSoftInput()没有可见的结果,但延迟一段时间再调用能正确地显示键盘。我最初也像你一样认为它正在被显示,然后很快被其他东西隐藏了。然而,在稍微深入挖掘后,我发现我可以传递一个ResultReceiver并记录结果。当我延迟发送showSoftInput()时,返回给我的接收器的结果代码是RESULT_SHOWN。当我不使用延迟时,我的接收器根本没有被调用。现在我怀疑它不是被隐藏了,而是出于某些原因根本没有显示出来。 - Matthew Pape
1
谢谢,使用第一种方法修复了键盘在首次启动带有EditText的对话框片段时未显示的问题。 - Ugo
7
对于调用postDelayed而不是post,我也赞同再次点赞 - 可能有其他默认的隐藏功能导致键盘首先被隐藏。显示和隐藏键盘是现存API中最糟糕的部分。 - rmirabelle
当在程序中切换布局和打开键盘时,您的第一个方法运行良好。但似乎有其他进程阻止了showSoftInput()的正确工作。 - Soren Stoutner

24

嗨,希望您还在寻找答案,因为当我测试我的代码时,我找到了答案。 这是代码:

InputMethodManager imm = (InputMethodManager)_context.getSystemService(Context.INPUT_METHOD_SERVICE);

imm.toggleSoftInput(0, 0);

以下是被回答的问题: android - show soft keyboard on demand


暴力破解来拯救!(我们有一个内部应用程序,仅支持一种平板电脑,因此我们的标准操作程序是使用该软键盘,所以我们没有理由让用户等待它) - Phlip

21

这对我使用带有硬键盘的手机有效:

editText1.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
          imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);

2
太好了,我忘记加入requestFocus(),这导致第一次无法弹出键盘。 - Ankit Bansal

19

这太微妙了,简直犯罪。这适用于没有硬件推出键盘的手机。有硬键盘的手机不会在此调用时自动打开。我的 LG 和旧 Nexus One 没有键盘,因此软键盘会在启动活动时打开(这就是我想要的),但是具有推出键盘的 MyTouch 和 HTC G2 手机在关闭硬键盘的情况下不会打开软键盘,直到我点击编辑字段。


1
注意:我已经尝试了很多关于EditText和InputMethodManager的实验,试图在设备有硬键盘时强制打开软键盘,但都没有成功。 - mobibob
1
对于在Visual Studio中进行Xamarin开发的任何人,在AVD管理器中,您可以编辑您的AVD,并有一个标记为“硬件键盘存在”的设置。取消选中此选项将允许软输入显示。 - Will Custode
@arbitur -- 你可以放心了。这在谷歌的时间里已经过去了一百年。(你甚至知道有人制造可滑动的硬键盘吗--哈哈)。那时候,只有我对这个问题和解决方案感兴趣,所以我试图做到完整。 - mobibob

12

这个答案可能晚了,但对我来说完美地解决了问题。也许能帮到其他人 :)

public void showSoftKeyboard(View view) {
    if (view.requestFocus()) {
        InputMethodManager imm = (InputMethodManager)
                getSystemService(Context.INPUT_METHOD_SERVICE);
        boolean isShowing = imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
        if (!isShowing)
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    }
}

根据您的需要,您可以使用其他标志。

InputMethodManager.SHOW_FORCED
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

在显示键盘之前请求焦点很有效(我之前是反过来的) - Shujito

10

弹出软键盘是一个大问题。我搜索了很多才得出最终结论。感谢这个回答提供了很多线索:https://stackoverflow.com/a/16882749/5903344

问题:

通常我们在初始化视图时就立即调用showSoftInput。在Activities中,这通常是在onCreate中,在Fragments中则是在onCreateView中。为了显示键盘,IMM需要将focusedView激活。可以使用IMM的isActive(view)方法来检查这一点。如果我们在创建视图时调用showSoftInput,很有可能该视图对于IMM不会处于活动状态。这就是为什么有时需要延迟50-100毫秒的showSoftInput的原因。然而,即使过了100毫秒,也不能保证视图会变成活动状态。所以在我的理解中,这又是一种hack。

解决方案:

我使用以下类。它会每隔100毫秒运行一次,直到成功显示键盘。它在每次迭代中执行各种检查。某些检查可以停止可运行项,某些检查则会在100毫秒后再次发布它。

public class KeyboardRunnable extends Runnable
{
    // ----------------------- Constants ----------------------- //
    private static final String TAG = "KEYBOARD_RUNNABLE";

    // Runnable Interval
    private static final int INTERVAL_MS = 100;

    // ----------------------- Classes ---------------------------//
    // ----------------------- Interfaces ----------------------- //
    // ----------------------- Globals ----------------------- //
    private Activity parentActivity = null;
    private View targetView = null;

    // ----------------------- Constructor ----------------------- //
    public KeyboardRunnable(Activity parentActivity, View targetView)
    {
        this.parentActivity = parentActivity;
        this.targetView = targetView;
    }

    // ----------------------- Overrides ----------------------- //
    @Override
    public void run()
    {
        // Validate Params
        if ((parentActivity == null) || (targetView == null))
        {
            Dbg.error(TAG, "Invalid Params");
            return;
        }

        // Get Input Method Manager
        InputMethodManager imm = (InputMethodManager) parentActivity.getSystemService(Context.INPUT_METHOD_SERVICE);

        // Check view is focusable
        if (!(targetView.isFocusable() && targetView.isFocusableInTouchMode()))
        {
            Dbg.error(TAG, "Non focusable view");
            return;
        }
        // Try focusing
        else if (!targetView.requestFocus())
        {
            Dbg.error(TAG, "Cannot focus on view");
            Post();
        }
        // Check if Imm is active with this view
        else if (!imm.isActive(targetView))
        {
            Dbg.error(TAG, "IMM is not active");
            Post();
        }
        // Show Keyboard
       else if (!imm.showSoftInput(targetView, InputMethodManager.SHOW_IMPLICIT))
        {
            Dbg.error(TAG, "Unable to show keyboard");
            Post();
        }
    }

    // ----------------------- Public APIs ----------------------- //
    public static void Hide(Activity parentActivity)
    {
        if (parentActivity != null)
        {
            InputMethodManager imm = (InputMethodManager) parentActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(parentActivity.findViewById(android.R.id.content).getWindowToken(), 0);
        }
        else
        {
            Dbg.error(TAG, "Invalid params to hide keyboard");
        }
    }

    // ----------------------- Private APIs ----------------------- //
    protected void Post()
    {
        // Post this aftr 100 ms
        handler.postDelayed(this, INTERVAL_MS);
    }
}

要使用它,只需创建此类的实例。将其传递给父活动和目标视图,该目标视图之后将具有键盘输入和焦点。然后使用Handler发布实例。


7

对于那些想要一种可靠且尽可能简短的方式在2022年显示键盘的人,我发现了这篇博客 「可靠地显示 Android 键盘」。该博客提供的答案对我而言非常有效,并且比 postDelay 100 毫秒的方法不那么 hacky,因为它利用 OnWindowFocusChangeListener 来做这个事。

它非常容易使用(这应该是 Google 内置的功能!):

editText.focusAndShowKeyboard()

在Kotlin中添加这个扩展方法,就可以在任何View上使用它!
fun View.focusAndShowKeyboard() {
   /**
    * This is to be called when the window already has focus.
    */
   fun View.showTheKeyboardNow() {
       if (isFocused) {
           post {
               // We still post the call, just in case we are being notified of the windows focus
               // but InputMethodManager didn't get properly setup yet.
               val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
               imm.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
           }
       }
   }

   requestFocus()
   if (hasWindowFocus()) {
       // No need to wait for the window to get focus.
       showTheKeyboardNow()
   } else {
       // We need to wait until the window gets focus.
       viewTreeObserver.addOnWindowFocusChangeListener(
           object : ViewTreeObserver.OnWindowFocusChangeListener {
               override fun onWindowFocusChanged(hasFocus: Boolean) {
                   // This notification will arrive just before the InputMethodManager gets set up.
                   if (hasFocus) {
                       this@focusAndShowKeyboard.showTheKeyboardNow()
                       // It’s very important to remove this listener once we are done.
                       viewTreeObserver.removeOnWindowFocusChangeListener(this)
                   }
               }
           })
   }
}

你可能会想,调用showSoftInput时为什么要写这些代码?我认为这对一些人有效,但对另一些人无效的原因是,大多数人在普通的活动或片段中测试了它,而那些报告不起作用的人(比如我)可能是在对话框片段或某种类型的片段中测试的。所以这个解决方案更加稳妥,可以在任何地方使用。


Android 12(至少在Pixel 7上):这不起作用了。由于Android 12,现在这个答案也没有改变任何东西。似乎onWindowFocusChanged永远不会被调用。不确定如何继续修复,因为我还没有找到任何答案。只有1个IssueTracker帖子。 - Merthan Erdem
一个担忧是,如果:
  1. 在调用 "requestFocus()" 之后,
  2. 进入 if 语句,并且 hasWindowFocus() 返回 false,
  3. 但在添加焦点变化监听器之前,视图已经获得了焦点。
在这种情况下,监听器会错过事件,键盘将无法打开。
- IChung
作为一种替代方案,如果我们:
  1. 添加焦点监听器
  2. 请求焦点
在这种情况下,事件将永远不会被错过。虽然可能会有一些性能损失,但用户很难察觉到几毫秒的延迟。
- IChung
这是正确可靠的做法,没有延迟和竞态条件! - undefined

7

以下对我有效:

    mEditTextStudy.requestFocus();
    mEditTextStudy.post(
            new Runnable() {
                @Override
                public void run() {
                    InputMethodManager imm =
                            (InputMethodManager)
                                    getActivity()
                                            .getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (imm != null) {
                        imm.showSoftInput(mEditTextStudy, SHOW_FORCED);
                    }
                }
            });

1

Xamarin开发者的解决方案(_digit1 == EditText):

        var focussed = _digit1.RequestFocus();
        if (focussed)
        {
            Window.SetSoftInputMode(SoftInput.StateAlwaysVisible);
            var imm = (InputMethodManager)GetSystemService(InputMethodService);
            imm.ToggleSoftInput(ShowFlags.Forced, 0);
        }

1
类似的问题,但解决方案不同,所以分享出来以便他人参考。
问题并非出在我的代码和使用方式上:
inputMethodManager.showSoftInput(kbdInput, InputMethodManager.SHOW_IMPLICIT);

最近的SDK编译出现了问题。我无法将上述内容应用于隐藏的字段。现在似乎必须让字段可见且大于0,才能显示键盘。我之前这样做是因为我的应用程序更像是一个使用键盘输入图像的游戏。所以我所需要做的就是改变:

<EditText
        android:id="@+id/kb_input"
        android:layout_width="0dp"
        android:layout_height="0dp"
      />

<EditText
        android:id="@+id/kb_input"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        android:textColorHighlight="@color/black"
        android:backgroundTint="@color/black"
        android:cursorVisible="false"
      />

我的背景是黑色的,所以虽然EditText现在可见,但在黑色背景上看起来像是不可见的。


我已经苦苦挣扎了一段时间,现在终于解决了! - Dimon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接