編輯(如果評論被清除)這個答案寫在指定的問題之前,它是移動網絡與移動應用程序。但是,這個答案將有助於那些在Android上使用WebView時尋找解決方案的人。
在Android上,用於顯示頁面的WebView可以覆蓋onCreateInputConnection。使用這種方法,您可以更改ime選項,儘管您仍然限於相同的數字類型。或者,您可以在creating your own ime type上工作。
一個例子:
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
InputConnection inputConnection = super.onCreateInputConnection(outAttrs);
outAttrs.inputType = outAttrs.inputType | InputType.TYPE_NUMBER_VARIATION_NORMAL;
return inputConnection;
}
不過,雖然我敢肯定,上述將解決許多問題的人民,我想你需要的東西多一點複雜。我的建議是,無論鍵盤類型你用,你用Java來驗證和格式化輸入,這需要幾個步驟:
設置JavaScript接口
webView.addJavascriptInterface(new ValidationInterface(), "appValidator");
創建接口類
public class ValidationInterface {
//This method would only tell you if a char is invalid
@JavascriptInterface
public boolean isValid(String text){
int len = text.length();
//replace everything thats NOT 0-9, $, £, comma or dot
text = text.replaceAll("[^0-9$£,\.", "");
//Its valid if the length didnt change (because nothing needs removing)
return text.length() == len;
}
//This method would strip out invalid text as you go
@JavascriptInterface
public String getValid(String text){
//replace everything thats NOT 0-9, $, £, comma or dot
return text.replaceAll("[^0-9$£,\.", "");
}
}
呼叫當文本改變了驗證
function validate(value) {
//ive not written js in a long time, you'll definitely need to tweak this
//EITHER do this
var valid = appValidator.isValid(value);
if (valid) {
//DO SOEMTHING
} ...
//OR try this
var validText = appValidator.getValid(value);
$('#theField').value(validText); //really not sure this is valid
}
//also just be aware you might need to use keyUp or somethign other than change
//if you are replacing the text as you type, using getValid method
<input id="theField" type="text" onChange="validate(this.value)" ... />
如果您只想禁用驗證,則可以使用「novalidate」屬性。 – pcagica
@pcagica它與驗證無關,我只需要在'''type = text'''輸入中打開數字鍵盤即可。請閱讀這個問題... –
所以我不明白這個問題對不起,你爲什麼不想輸入文本呢?正如你所說,你可以用數字鍵強制數字鍵盤。 – pcagica