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

我创建了一个简单的应用程序来测试以下功能。当我的活动启动时,需要在软键盘打开的情况下启动它。 我的代码不起作用?! 我已经在清单中尝试了各种“状态”设置,并且在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);
    }
}
    
已邀请:
当活动启动时似乎键盘最初显示但被其他东西隐藏,因为以下工作(但实际上是一个肮脏的解决方法): 第一种方法
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中添加给活动标记的代码。它将在启动时显示键盘,并将第一个焦点设置为您想要的视图。
android:windowSoftInputMode="stateVisible"
    
嘿,我希望你在测试我的代码时找到了答案。这是代码:
InputMethodManager imm = (InputMethodManager)_context.getSystemService(Context.INPUT_METHOD_SERVICE);

imm.toggleSoftInput(0, 0);
这是我回答的问题: android - 按需显示软键盘     
这与我在硬键盘手机上合作:
editText1.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
          imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
    
这是如此微妙,它是犯罪。这适用于没有硬键滑出式键盘的手机。带有硬键盘的手机无法通过此通话自动打开。我的LG和旧的Nexus One没有键盘 - 因此,当活动启动时软键盘会打开(这就是我想要的),但是带有滑出式键盘的MyTouch和HTC G2手机无法打开软键盘键盘直到我用硬键盘关闭触摸编辑字段。     
这个答案可能会晚,但对我来说效果很好。也许它有助于某人:)
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);
    
以下为我工作:
    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);
                    }
                }
            });
    
显示软键盘是一个大问题。我经常搜索以得出最终结论。感谢这个答案给出了一些线索:https://stackoverflow.com/a/16882749/5903344 问题: 通常我们在初始化视图时立即调用showSoftInput。在Activities中,这主要在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);
    }
}
要使用它,只需创建此类的实例即可。将它传递给父活动和targetView,后者将具有键盘输入和焦点。然后使用Handler发布实例。     
我的代码中有切换但没有postDelayed。我曾经尝试过postDelayed for showSoftInput但没有成功,我已经尝试了你建议的解决方案。在我决定增加延迟时间之前,我打算将它作为另一个失败的潜在解决方案丢弃。它适用于我一直到200毫秒,此时它不起作用,至少不在物理电话上。因此,在你糟糕的Android开发人员抛弃这个答案之前,尝试增加延迟以获得成功的解决方案。为较旧的较慢的手机添加一些可能是值得的。谢谢堆,正在努力工作几个小时。     
这对我有用:
public void requestFocusAndShowSoftInput(View view){
    view.requestFocus();
    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
    inputMethodManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
    
Xamarin开发人员的解决方案(_digit1 == EditText):
        var focussed = _digit1.RequestFocus();
        if (focussed)
        {
            Window.SetSoftInputMode(SoftInput.StateAlwaysVisible);
            var imm = (InputMethodManager)GetSystemService(InputMethodService);
            imm.ToggleSoftInput(ShowFlags.Forced, 0);
        }
    

要回复问题请先登录注册