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

查看:100
本文介绍了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.

我用Google搜索和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.字段经过验证,或者发生插入,或者引发错误消息以向提交者提供有关Axes字段所需格式的信息.正确的格式是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天全站免登陆