標籤: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 輸入框鍵盤不彈出