我有一个使用webview的android应用。
我使用的几种形式都将自动聚焦在第一个元素上。
我想要的是相关的键盘在加载表单时显示。活动中的代码为:

public class MyActivity extends DroidGap {
/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //load the main app
    loadUrl("file:///android_asset/www/index.html");
}

@Override
public void init() {
    final String version = getVersion();
    CordovaWebView webView = new CordovaWebView(this);
    init(webView, new CordovaWebViewClient(this, webView) {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            super.onPageFinished(view, url);
            boolean redirected = false;
            //If the page you're accessing is the login page
            if (url.endsWith("/Login")) {
                //redirect to root page with query string
                appView.loadUrl(url + "?version=" + version));
                redirected = true;
            }

            return redirected;
        }
    }, new CordovaChromeClient(this, webView));
    }
}


基本上,加载了应用程序索引页面上方的代码中发生的事情。当我单击登录按钮时,会将应用程序版本附加到已加载的url(出于不相关的原因)上,并显示登录页面。
页面加载后,我可以在该字段内看到光标,但是直到按下该字段后键盘才会显示。

我看过this question,但不知道如何应用答案。
非常感谢您的任何帮助。

最佳答案

我最终使用

                @Override
                public void onPageFinished(WebView view, String url) {
                    super.onPageFinished(view, url);
                    // Display the keyboard automatically when relevant
                    if (view.getTitle().equals("Login")) {
                        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.showSoftInput(view, 0);
                    }
                }

10-07 19:35
查看更多