android - Activity 启动时软键盘不显示

标签 android user-interface soft-keyboard

我已将 android:windowSoftInputMode="stateAlwaysVisible" 添加到我在 AndroidManifest.xml 中的 Activity,这是我的布局:

<?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">
    <EditText android:id="@+id/EditText01" android:layout_width="wrap_content"
        android:layout_height="wrap_content"></EditText>
    <EditText android:id="@+id/EditText02" android:layout_width="wrap_content"
        android:layout_height="wrap_content"></EditText>
    <Button android:id="@+id/Button01" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="Send"></Button>
</LinearLayout>

alt text http://img227.imageshack.us/img227/2006/18021414.png

Activity 启动时,EditText 获得焦点,但不显示软键盘。如果我点击 EditText,就会看到软键盘。 Activity 启动时是否需要设置其他参数来显示软键盘?

谢谢

最佳答案

解决方案 1:

在 Activity 的 onCreate() 方法中编写以下代码

InputMethodManager imm = (InputMethodManager)
    SearchActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);

if (imm != null){
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
}

解决方案2:

创建以下方法并从 onCreate() 调用

private void showVirturalKeyboard(){
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
         @Override
         public void run() {
              InputMethodManager m = (InputMethodManager) SearchActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);

              if(m != null){
                // m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                m.toggleSoftInput(0, InputMethodManager.SHOW_IMPLICIT);
              } 
         }

    }, 100);         
}

关于android - Activity 启动时软键盘不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2712822/

相关文章:

android - 在嵌套对象抖动中解析复杂的json

java - 如何组织一个 Swing GUI 应用程序?

Android 隐藏键盘不起作用 - 无法隐藏软键盘

java - 为什么我们需要 JSF 标签,如果我们有 HTML 标签

html - 如何在同一行中显示字段集的两个图例?

windows-phone-7 - 如何在 WP7 中隐藏软键盘?

android - 自定义 HTML 页面中的软键盘类型

android - 带有 Inputstream.read 的 Android 2.1 蓝牙 SPP 错误

java - 无法从我的 Android 模拟器连接到本地 Web 服务

android - 在 Phonegap 上恢复应用程序状态