首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >以编程方式打开软键盘

以编程方式打开软键盘
EN

Stack Overflow用户
提问于 2011-04-08 17:20:48
回答 21查看 149.9K关注 0票数 129

我有一个没有子部件的活动,相应的xml文件是,

代码语言:javascript
复制
<?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获取start.and,这是我之前尝试过的,

代码语言:javascript
复制
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);
    }

给我一些指导。

EN

回答 21

Stack Overflow用户

回答已采纳

发布于 2011-04-11 13:31:39

我使用以下几行代码在onclick事件中手动显示了软键盘,键盘是可见的。

代码语言:javascript
复制
InputMethodManager inputMethodManager =
    (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(
    linearLayout.getApplicationWindowToken(),
    InputMethodManager.SHOW_FORCED, 0);

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

票数 155
EN

Stack Overflow用户

发布于 2011-10-13 22:15:24

在清单文件中,尝试将以下内容添加到<activity>中,以便在活动启动时显示键盘:

android:windowSoftInputMode="stateVisible"

这将使键盘在活动启动时变为可见。

有关更多选项,请查看documentation

票数 126
EN

Stack Overflow用户

发布于 2011-04-11 14:49:42

请遵循以下代码。我相信你的问题会得到解决的。

代码语言:javascript
复制
if (imm != null){
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
   } 
票数 38
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5593053

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档