我有一个没有子部件的活动,对应的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>

我想在活动开始时以编程方式打开软键盘.到目前为止,我try 过的是,

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);

但我仍然无法在活动打开时打开它,所以有什么解决方案吗?

Android相关问答推荐

使用StateFlow从房间到UiState的流程列表

图像在Android Studio中显示,但不在设备中显示

如何禁用Android 34+版的TileService,但保留以前的版本?

Yarn 机器人导致活动未找到,但Gradlew Run工作正常

Jetpack Compose和Android Studio中的普通设计工具有什么不同?

在Jetpack Compose中的隐藏状态栏后面绘制

在 Compose 中,当用户持续向下滚动时,LazyColumn 不会显示新项目

看不到选项菜单栏

Android kotlin 中闪屏 API 执行完成后如何根据身份验证将用户导航到特定屏幕

在 kotlin 协同 routine 中,如何将数据范围限定为请求路径(以 MDC 为例)?

页面标题未显示在内容页面上

如何在Android Studio中禁用文件中的Github用户名引用?

如何在 React Native 中调试网络响应

如何在 Jetpack Compose 中设置行宽等于 TextField 的宽度?

找不到(包名称).在以下位置搜索:

如何在 compose 中使用 BottomSheetScaffold 为底页设置半展开高度?

Android build gradle 文件版本代码自动递增

TextField 溢出和软包装不适用于 Compose 约束布局

Android:为什么 ICICI iMobile Pay 应用程序在我的应用程序中显示root/jailbroken设备?

在 Android Studio 中替换字符串中的 "