android - 以编程方式打开软键盘

标签 android android-softkeyboard

我有一个没有子小部件的 Activity ,相应的 xml 文件是,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusable="true"
>
</LinearLayout>

我想在 Activity 开始时以编程方式打开软键盘。到目前为止我尝试过的是,

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    if (inputMethodManager != null) {
        inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    }

给我一​​些指导。

最佳答案

我已经在onclick事件内部使用了以下几行手动显示软键盘,并且键盘是可见的。

InputMethodManager inputMethodManager =
    (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(
    linearLayout.getApplicationWindowToken(),
    InputMethodManager.SHOW_FORCED, 0);

但是当 Activity 打开时我仍然无法打开它,那么有什么解决方案吗?

关于android - 以编程方式打开软键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5593053/

相关文章:

Android 自定义键盘 - 如何获取浏览器 InputType?

android - 带有 EditText 的 AlertDialog,以 EditText 为焦点自动打开软键盘不起作用

Android InputMethodEditor 布局文件。 XML 中的非键/行元素?

java.util.Date null 与 ProGuard

java - 关闭并打开应用程序Webview后恢复上一页

Android自定义小部件膨胀异常

android - 如何将 EditText 滚动到布局的最顶部?

android - APK未优化

Android Constraint Layout - child 的对齐方式

android - 如何在 Android 中向 KeyboardView 添加填充?