Android InputType 布局参数 - 如何允许负小数? [英] Android InputType layout parameter - how to allow negative decimals?

查看:26
本文介绍了Android InputType 布局参数 - 如何允许负小数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个布局,其中包含三个用于输入三个地图坐标的字段.到现在为止还挺好.我在布局中使用 android:inputType="numberDecimal" .输入该字段时,用户将获得数字键盘.还好.

I have a layout which has three fields for the entry of three map coordinates. So far so good. I'm using android:inputType="numberDecimal" in the layout. When entering the field the user gets the numeric keypad. Still good.

然而,当需要输入负坐标时,没有明显的方法可以做到这一点.

However, when a negative coordinate needs to be entered, there is no apparent way to do this.

23.2342 工作正常.232.3421 工作正常.-11.23423 无法输入-无法输入前导负号,甚至无法将坐标包裹在 () 中.

23.2342 works fine. 232.3421 works fine. -11.23423 can not be entered - there is no way to enter the leading negative sign, or even wrap the coordinate in ().

我确定我可以将其更改为纯文本 inputType,然后使用正则表达式来验证输入的内容实际上是数字坐标,处理返回给用户的错误消息等.但是我宁愿不走那条路.

I'm sure I can go the route of changing this to straight text inputType, and then use a regular expression to validate that what was entered is in fact a numeric coordinate, handle error messaging back to the user, etc. But I'd rather not go that route.

我在谷歌上搜索了这个问题并在 Stackoverflow 上搜索了几个小时,但没有成功.有什么建议吗?

I have Googled and Stackoverflowed this question for a couple hours with no luck. Any suggestions?

推荐答案

最终对每个轴都使用了 Pattern 和 Matcher,保留了实际的文本输入格式.

Ended up using Pattern and Matcher for each of the axes, leaving the actual text input format open.

三个轴字段,提交按钮触发onSave.验证字段并进行插入,或者向提交者提出关于轴字段所需格式的错误消息.正确的格式是 1-3 位数字,可以选择以-"开头,也可以选择后跟."和其他数字.

Three axes fields, submit button triggers onSave. Fields are validated and either the insert occurs, or error message is raised to submitter about required format for the Axes fields. Proper format is 1-3 digits, optionally prefaced by '-', optionally followed by '.' and additional digits.

试试这个代码:

private View.OnClickListener onSave=new View.OnClickListener(){
    public void onClick(View v){
        Pattern xAxis = Pattern.compile("[-+]?([0-9]*\.)?[0-9]+");
        Matcher mForX = xAxis.matcher(xaxis.getText().toString());

        Pattern yAxis = Pattern.compile("[-+]?([0-9]*\.)?[0-9]+");
        Matcher mForY = yAxis.matcher(yaxis.getText().toString());

        Pattern zAxis = Pattern.compile("[-+]?([0-9]*\.)?[0-9]+");
        Matcher mForZ = zAxis.matcher(zaxis.getText().toString());

        //If Axis X and Axis Y and Axis Z are all valid entries, the proceed.
        if(mForX.find() && mForY.find() && mForZ.find()){
             //handle insert or update statement here
        }else{
             //give error message regarding correct Axis value format
        }
   }

}

这篇关于Android InputType 布局参数 - 如何允许负小数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆