Android WebView textarea 不弹出键盘

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3460915/
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 10:17:40  来源:igfitidea点击:

WebView textarea doesn't pop up the keyboard

androidtextareawebview

提问by Sana

When I display a WebView, I don't see the soft keyboard popping up. The hard keyboard also doesn't work!

当我显示 a 时WebView,我没有看到软键盘弹出。硬键盘也不行!

What are the usual shortcomings.

常见的缺点是什么。

The code which I use to access the WebViewis:

我用来访问的代码WebView是:

package com.example.blahblah;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class createAccount extends Activity {

private static final String LOG_TAG = "Create Account";
private WebView mWebView;
private static final String URL = blah_app.urlSelected+"createAccount.html";    
private Handler mHandler = new Handler();

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    Toast.makeText(createAccount.this, "URL = " +URL, Toast.LENGTH_SHORT).show();
    getWindow().requestFeature(Window.FEATURE_PROGRESS);
    setContentView(R.layout.webview);
    getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);

    mWebView = (WebView) findViewById(R.id.webview);

    WebSettings webSettings = mWebView.getSettings();
    webSettings.setSavePassword(true);
    webSettings.setSaveFormData(true);
    webSettings.setJavaScriptEnabled(true);
    webSettings.setSupportZoom(true);


    final Activity activity = this;
    mWebView.setWebChromeClient(new WebChromeClient() {
    public void onProgressChanged(WebView view, int progress) {

        activity.setProgress(progress * 1000);
      }
    });

    mWebView.addJavascriptInterface(new DemoJavaScriptInterface(), "demo");
    mWebView.clearCache(true);
    setProgressBarVisibility(true);
    mWebView.setWebViewClient(new WebViewClient() {
          public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
          }

           @Override  
           public void onPageFinished(WebView view, String url)  
           {  
               mWebView.loadUrl("javascript:(function () { " +
                       "setVariables("+blah_app.numberSelected+",'"+blah_app.urlSelected+"');" +
                       "})()");
           }
        });

    mWebView.loadUrl(URL);
}

final class DemoJavaScriptInterface {

    public void setData(String fname, String lname, String gacct, String phone) {

        SharedPreferences prefCreateAccount = PreferenceManager.getDefaultSharedPreferences(createAccount.this);
        SharedPreferences.Editor editor = prefCreateAccount.edit();
        editor.putString("FirstName", fname);
        editor.putString("LastName", lname);
        editor.putString("GmailAcct", gacct);
        editor.putString("Phone", phone);
        editor.commit();    

    }
    DemoJavaScriptInterface() {

    }

    /**
     * This is not called on the UI thread. Post a runnable to invoke
     * loadUrl on the UI thread.
     */
    public void clickOnAndroid() {
        mHandler.post(new Runnable() {
            public void run() {
                mWebView.loadUrl("javascript:wave()");
            }
        });
    }
}

/**
 * Provides a hook for calling "alert" from javascript. Useful for
 * debugging your javascript.
 */
final class MyWebChromeClient extends WebChromeClient {
    @Override
    public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
        Log.d(LOG_TAG, message);
        result.confirm();
        return true;
    }
}

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
        startActivity(new Intent(getApplication(), blah_app.class));
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
}

采纳答案by Sana

The problem was that webview wasn't getting focus when it was loaded hence using

问题是 webview 在加载时没有获得焦点,因此使用

webView.requestFocus(View.FOCUS_DOWN);

solved the problem.

解决了这个问题。

回答by Jeff Peiffer

The full solution is a bit more than what Sana had. It is documented as a bug over at the Android site ( http://code.google.com/p/android/issues/detail?id=7189):

完整的解决方案比 Sana 拥有的要多一些。它在 Android 站点 ( http://code.google.com/p/android/issues/detail?id=7189) 上被记录为一个错误:

webView.requestFocus(View.FOCUS_DOWN);
webView.setOnTouchListener(new View.OnTouchListener()
{
    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_UP:
                if (!v.hasFocus())
                {
                    v.requestFocus();
                }
                break;
        }
        return false;
    }
});

回答by RuslanK

I'm surprised that even on Android 4.1 (tested on SGS3) the problem is still present, and overriding onTouch() don't solve it for me.

我很惊讶即使在 Android 4.1(在 SGS3 上测试)问题仍然存在,并且覆盖 onTouch() 并不能为我解决它。

Test code:

测试代码:

String HTML = "<html><head>TEST</head><body><form>";
HTML += "<INPUT TYPE=TEXT SIZE=40 NAME=user value=\"your name\">";
HTML += "</form></body></html>";

WebView wv = new WebView(getContext());
wv.loadData(HTML, "text/html", null);

final AlertDialog.Builder adb = new AlertDialog.Builder(getContext());
adb.setTitle("TEST").setView(wv).show();

My complex solution is replace WebView with MyWebView:

我的复杂解决方案是用 MyWebView 替换 WebView:

private static class MyWebView extends WebView
{
    public MyWebView(Context context)
    {
        super(context);
    }

    // Note this!
    @Override
    public boolean onCheckIsTextEditor()
    {
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev)
    {
        switch (ev.getAction())
        {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_UP:
                if (!hasFocus())
                    requestFocus();
            break;
        }

        return super.onTouchEvent(ev);
    }
}

回答by rahul

one line answer and it is working nicely

一行回答,它运行良好

// define webview for browser
wb = (WebView) findViewById(R.id.webView1);
wb.requestFocusFromTouch();

where wb is my object of webView

其中 wb 是我的对象 webView

回答by Panzenbaby

Had the same issue and non of the above solutions worked. This woked for me

有同样的问题,上述解决方案都没有奏效。这让我醒来

    <CustomWebView
    android:id="@+id/webView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:focusable="true"
    android:focusableInTouchMode="true" />

回答by elBradford

For those of you looking for a solution when the WebViewis in an AlertDialog, the answer here solved this problem for me: Show soft keyboard in AlertDialog with a WebView inside (Android)

对于那些在 中寻找解决方案WebView的人AlertDialog,这里的答案为我解决了这个问题:在 AlertDialog 中显示软键盘,其中包含一个 WebView (Android)

回答by Kevin Grant

I had the same problem on Jelly Bean, but none of the above solutions worked for me. This solution worked for me on JellyBean

我在 Jelly Bean 上遇到了同样的问题,但上述解决方案都不适合我。这个解决方案在 JellyBean 上对我有用

WebView webView = new WebView(getActivity(), null, android.R.attr.webViewStyle);

Sometimes the android.R.attr.webViewStyle is not applied by default. This ensures that the style is always applied.

有时默认情况下不应用 android.R.attr.webViewStyle。这可确保始终应用该样式。

回答by Prasham

I had a similar problem.... and later i found that the text box on the web page that my web view shown was disabled.

我有一个类似的问题......后来我发现我的网络视图显示的网页上的文本框被禁用。

Check this if the page has same problem in browser?

检查这个页面是否在浏览器中有同样的问题?

回答by gumbercules

I was having the exact same problem none of the above solutions worked for me. After ages of trying i finally found the problem.

我遇到了完全相同的问题,上述解决方案都不适合我。经过多年的尝试,我终于找到了问题所在。

Some of the various web pages I was rendering with WebView didn't fit properly into the Web view and as a result a div (or some other html component ) were being invisibly laid over the input fields. Although the input fields appeared selected when touched, they would not allow text input (even if i did manage to get the soft keyboard up using the track ball).

我使用 WebView 呈现的各种网页中的一些不适合 Web 视图,因此一个 div(或其他一些 html 组件)被无形地放置在输入字段上。尽管输入字段在触摸时显示为选中状态,但它们不允许文本输入(即使我确实设法使用轨迹球启动了软键盘)。

So the solution.

所以解决方案。

WebView.getSettings().setUseWideViewPort(true);

This won't completely stop the issue, but makes the view more like a PC browser which sites are designed for. Less change of the overlay.

这不会完全解决问题,但会使视图更像是为网站设计的 PC 浏览器。覆盖层变化较少。

Hope this helps you.

希望这对你有帮助。

回答by Manisha

try this --> the firstname is the name of the field and make sure it dose not have autofocus attribute.

试试这个 --> firstname 是字段的名称,并确保它没有 autofocus 属性。

    mWebView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            mWebView.loadUrl("javascript:document.getElementById(\"firstname\").focus();");
        }
    });