活动开始时不显示软键盘

Sar*_*tel 6 user-interface android 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>
Run Code Online (Sandbox Code Playgroud)

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

当Activity启动时,EditText会聚焦,但不会显示软键盘.如果我点击EditText,那么我会看到软键盘.我的Activity启动时是否需要设置aditional参数以显示软键盘?

谢谢

小智 15

解决方案1:

在onCreate()方法的活动中写下面的代码

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

if (imm != null){
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
}
Run Code Online (Sandbox Code Playgroud)

解决方案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);         
}
Run Code Online (Sandbox Code Playgroud)


Don*_*rty 7

尝试将此添加到活动onCreate()方法

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); 
Run Code Online (Sandbox Code Playgroud)