Android显示软键与showSoftInput无法正常工作?

mob*_*bob 26 android show android-softkeyboard android-input-method

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

我的代码不起作用?!

我已经在清单中尝试了各种"状态"设置,并且在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>
Run Code Online (Sandbox Code Playgroud)

...主要布局(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>
Run Code Online (Sandbox Code Playgroud)

......和代码......

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

Xar*_*mer 34

当活动启动时似乎键盘最初显示但被其他东西隐藏,因为以下工作(但实际上是一个肮脏的解决方法):

第一种方法

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

第二种方法

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

第三种方法 ADD给定Manifest中活动标签的代码.它将在启动时显示键盘,并将第一个焦点设置为您想要的视图.

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

  • 另一个用于调用`postDelayed`与`post`的投票 - 可能还有一些其他默认隐藏功能导致键盘首先被隐藏.显示和隐藏键盘是现存最严重破坏的API. (5认同)
  • 我也有同样的问题。立即调用`showSoftInput()`没有可见的结果,但是延迟发布会导致键盘正确显示。最初,我像您一样认为它已被显示,然后立即被其他东西隐藏。经过一番挖掘之后,我发现我可以传入`ResultReceiver`并记录结果。当我延迟发布`showSoftInput()`时,返回给接收者的结果代码是`RESULT_SHOWN`。当我不使用延迟时,根本不会调用我的接收器。现在我怀疑它不是被隐藏,而是由于某种原因根本没有显示出来。 (3认同)

dro*_*ter 20

嘿,我希望你在测试我的代码时找到了答案.这是代码:

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

imm.toggleSoftInput(0, 0);
Run Code Online (Sandbox Code Playgroud)

这是我回答的问题: android - 按需显示软键盘


小智 19

这与我在硬键盘手机上合作:

editText1.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
          imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
Run Code Online (Sandbox Code Playgroud)

  • 太棒了,我缺少requestFocus(),该请求停止了第一次显示键盘。 (2认同)

mob*_*bob 17

这是如此微妙,它是犯罪.这适用于没有硬键滑出式键盘的手机.带有硬键盘的手机无法通过此通话自动打开.我的LG和旧的Nexus One没有键盘 - 因此,当活动启动时,软键盘会打开(这就是我想要的),但是带有滑出式键盘的MyTouch和HTC G2手机无法打开软键盘键盘直到我用硬键盘关闭触摸编辑字段.


Anh*_*Mai 9

这个答案可能会晚,但对我来说效果很好.也许它有助于某人:)

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

取决于您的需要,您可以使用其他标志

InputMethodManager.SHOW_FORCED
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Run Code Online (Sandbox Code Playgroud)


Sid*_*arg 8

显示软键盘是一个大问题。我查了很多资料才得出最后的结论。感谢这个给出了一些线索的答案:https : //stackoverflow.com/a/16882749/5903344

问题:

通常,我们在初始化视图后立即调用 showSoftInput。在活动中,这主要是在 onCreate 中,在 Fragments onCreateView 中。为了显示键盘,IMM 需要激活 focsedView。这可以使用 IMM 的 isActive(view) 方法进行检查。如果我们在创建视图时调用 showSoftInput,则视图很可能不会对 IMM 处于活动状态。这就是有时延迟 50-100 毫秒的 showSoftInput 有用的原因。但是,这仍然不能保证 100 毫秒后视图将变为活动状态。所以在我看来,这又是一次黑客攻击。

解决方案:

我使用以下课程。这将每 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

要使用它,只需创建此类的一个实例。将父 Activity 和 targetView 传递给它,之后会有键盘输入和焦点。然后使用处理程序发布实例。


Bru*_*uce 7

对于那些想要在 2022 年使用一种不那么老套且尽可能短且可靠的解决方案来显示键盘的人,我发现这篇博客可靠地显示 Android 键盘,其中的答案对我来说非常有效,而且它更简单比 postDelay 100ms 更hacky,因为它利用 OnWindowFocusChangeListener 来实现这一目的。

\n

它使用起来非常简单(这样的东西应该由 Google 内置!):

\n
editText.focusAndShowKeyboard()\n
Run Code Online (Sandbox Code Playgroud)\n

在 Kotlin 中添加此扩展方法以在任何视图上使用它!

\n
fun View.focusAndShowKeyboard() {\n   /**\n    * This is to be called when the window already has focus.\n    */\n   fun View.showTheKeyboardNow() {\n       if (isFocused) {\n           post {\n               // We still post the call, just in case we are being notified of the windows focus\n               // but InputMethodManager didn\'t get properly setup yet.\n               val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager\n               imm.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)\n           }\n       }\n   }\n\n   requestFocus()\n   if (hasWindowFocus()) {\n       // No need to wait for the window to get focus.\n       showTheKeyboardNow()\n   } else {\n       // We need to wait until the window gets focus.\n       viewTreeObserver.addOnWindowFocusChangeListener(\n           object : ViewTreeObserver.OnWindowFocusChangeListener {\n               override fun onWindowFocusChanged(hasFocus: Boolean) {\n                   // This notification will arrive just before the InputMethodManager gets set up.\n                   if (hasFocus) {\n                       this@focusAndShowKeyboard.showTheKeyboardNow()\n                       // It\xe2\x80\x99s very important to remove this listener once we are done.\n                       viewTreeObserver.removeOnWindowFocusChangeListener(this)\n                   }\n               }\n           })\n   }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

您可能会想,为什么在我的测试期间调用 showSoftInput 时所有这些代码都有效?我认为它对某些人有效但对其他人无效是因为大多数人在正常活动或片段中测试了它,而报告不起作用的人(像我)可能在对话片段或某种形式中测试它。所以这个解决方案更加简单,可以在任何地方使用。

\n


Sag*_*gar 6

以下为我工作:

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