Android Activity 启动时不显示软键盘

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2712822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:02:52  来源:igfitidea点击:

Soft keyboard does not show when Activity starts

androiduser-interfacesoft-keyboard

提问by Sarp Centel

I have added android:windowSoftInputMode="stateAlwaysVisible"to my Activity in AndroidManifest.xmland here's my layout:

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

替代文字 http://img227.imageshack.us/img227/2006/18021414.png

When the Activity starts, the EditText is focused, but soft keyboard isn't displayed. If I click on the EditText, then I see the soft keyboard. Do I need to set aditional parameters to display soft keyboard when my Activity starts?

当 Activity 启动时,EditText 被聚焦,但不显示软键盘。如果我单击 EditText,则会看到软键盘。当我的 Activity 启动时,我需要设置额外的参数来显示软键盘吗?

Thanks

谢谢

回答by rakeshsoni

solution 1 :

解决方案1:

write following code inside onCreate() method of activity

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

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

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

solution 2 :

解决方案2:

create following method and call from onCreate()

创建以下方法并从 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);         
}

回答by Donal Rafferty

Try adding this to the activities onCreate() method

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

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);