在Android&;上查看网站时强制输入数字;网间网操作系统
我需要一个将保持在Android&;上查看网站时强制输入数字;网间网操作系统,android,ios,html,Android,Ios,Html,我需要一个将保持type=“text”,但将在Android和iOS设备上打开数字键盘的输入 这是因为输入字段仍将包含一些字符,例如和,,这在type=“number”或type=“tel”中是不可能的 我发现可以使用模式=“\d*”在iOS上强制使用数字键盘,但这对Android没有任何作用 以下是我到目前为止的情况: <input type="text" inputmode="numeric" pattern="\d*" value="£2,000,000" /> 我使用
type=“text”
,但将在Android和iOS设备上打开数字键盘的输入
这是因为输入字段仍将包含一些字符,例如
和,
,这在type=“number”
或type=“tel”
中是不可能的
我发现可以使用模式=“\d*”
在iOS上强制使用数字键盘,但这对Android没有任何作用
以下是我到目前为止的情况:
<input type="text" inputmode="numeric" pattern="\d*" value="£2,000,000" />
我使用:
没有其他必要
在iPhone上运行非常好,尤其是在使用新键盘类型时。使用默认键和快捷键
此外,这适用于所有Android设备
更新:
试一试:
<input type="text" step="0.01" pattern="[0-9]*">
<input type="text" min="0" max="100" pattern="[0-9]*">
Or
<input type="text" pattern="\-?\d+(\.\d{0,})?">
或
我也在做类似的事情
看看我的例子:
编辑(如果评论已清理)此答案是在指定问题之前编写的,它适用于移动web vs移动应用程序。然而,这个答案将帮助那些在Android上使用WebView时寻找解决方案的人
在Android上,用于显示页面的WebView可以覆盖onCreateInputConnection。使用此方法,您可以更改imeOptions,尽管您仍然限于相同的数字类型。或者你也可以继续工作
例如:
@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)" ... />
当文本更改时调用验证
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)" ... />
函数验证(值){
//我很久没有写js了,你肯定需要调整一下
//要么这样做
var valid=appValidator.isValid(值);
如果(有效){
//做SOEMTHING
} ...
//或者试试这个
var validText=appValidator.getValid(值);
$(“#theField”).value(validText);//确实不确定这是否有效
}
//另外,请注意,您可能需要使用keyUp或除change以外的其他东西
//如果要在键入时替换文本,请使用getValid方法
如果您只想禁用验证,可以使用“novalidate”属性。@pcagica这与验证无关,我只需要在type=text
输入时打开数字键盘即可。请阅读问题…所以我不理解问题对不起,你为什么不输入文本?如你所说,你可以使用模式强制数字键盘。我使用此模式=“[0-9]*”而不是“\d*”,可在ios和Android上使用,因为它是一个用符号输入货币的字段,例如(
)和,
无法输入到type=number
或type=tel
输入中这是我已经在使用的模式(参见问题),并且pattern=“\d*”
在Android上不起作用,但会很快完成。为什么必须是“text”
?它必须是文本才能允许使用(
)和,
等字符,因为这是用于“money”输入的。我刚刚测试了你的更新,它们都不能在Android上工作:不过没有“money”键盘。我建议您坚持使用文本,因为我在发给您的提琴上发现了一些奇怪的行为(仅限Android)。如果你开始键入数字,它将不允许你键入任何字母,但如果你最初键入字母,它将不起作用。让我们看看如何解决这个问题。我刚刚发现,我认为这正是你所追求的:首先,你在测试什么版本的android?KitKat我认为,4.4.2抱歉,找不到在数字和符号(货币符号)键盘之间切换的方法。我想用一种不同的方式来实现你想要的,但不确定你会喜欢它。有没有可能得到这样一个活生生的例子?我从未使用过Andriod的WebView,所以我不知道从哪里开始。这是一个网站上需要的,这是为了一个应用程序吗?是的,这将是一个android应用程序中的网络视图(因为我认为你标记了平台,没有提到网站-我刚刚意识到你没有添加这些标记),我已经奖励这个答案,因为它是唯一有效的(但不符合我当前的需要,即需要在普通网站上工作)。