Android WebView 輸入框鍵盤不彈出

來源:互聯網
上載者:User

標籤:android

問題在Android中使用內嵌的WebView載入HTML網頁時,如果html頁面中存在輸入框。那麼在有些手機裝置中,當輸入框擷取焦點時,系統IME鍵盤無法正確彈出,從而無法完成正常的輸入要求
在做APP時,自己也遇到了這個問題,以下是自己解決的方法,有可能不適合大家所遇到的情況,但值得借鑒~
WebView設定問題有些時候我們設計的html頁面並不能夠很好的適應WebView,尤其我們的html頁面是為PC瀏覽器設計的時候,當使用WebView來載入時,介面很可能會發生錯亂,當input輸入框被其他元素(例如:div)覆蓋,即使input能夠顯示和選中,但IME依然不會彈出。在這種情況下我們能做到的只有
     1、WebView.getSettings().setUseWideViewPort(true),使WebView能自適應手機螢幕大小,自由縮放,使得WebView和PC瀏覽器更相似,避免介面布局錯亂,減少問題出現的機率     2、為html提供移動版本
代碼失誤有些時候我們需要自訂WebView來滿足特定的需求,比如我們想為WebView添加一個載入進度條,來統一WebView進度條的顯示,那麼就有可能出現如下的代碼
public class ProgressBarWebView extends WebView{

    public ProgressBarWebView(Context context) {
        this(context, null);
    }
    public ProgressBarWebView(Context context, AttributeSet attrs){
        this(context, attrs, 0);
    }
    public ProgressBarWebView(Context context, AttributeSet attrs, int defStyle){
        super(context, attrs, defStyle);
        // TODO
        // your code
    }

}

這段代碼會有什麼問題呢?在使用ProgressBarWebView的時候,如果在layout中不指定style的話,那麼將會進入到
    public ProgressBarWebView(Context context, AttributeSet attrs){
        this(context, attrs, 0);
    }
構造方法中,這裡我們指定為0,如果去查看Android WebView的原始碼,是如下定義的
public WebView(Context context, AttributeSet attrs) {
     this(context, attrs, com.android.internal.R.attr.webViewStyle);
}
必須為WebView附加com.android.internal.R.attr.webViewStyle樣式,不然WebView不能正常的工作
總結以上就是自己總結的一些方法,希望對大家有協助~




Android WebView 輸入框鍵盤不彈出

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.