Android的InputType布局参数 – 如何允许负小数?

我有一个布局,有三个字段的input三个地图坐标。 到现在为止还挺好。 我在布局中使用android:inputType =“numberDecimal”。 input字段时,用户将获得数字键盘。 还好。

但是,当需要input负值坐标时,没有明显的方法来做到这一点。

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

我敢肯定,我可以去改变这个直接的文本inputType的路线,然后使用正则expression式来validationinput的内容实际上是一个数字坐标,处理错误消息传回给用户,但我想而不是走那条路。

我已经Google和Stackoverflow这个问题几个小时没有运气。 有什么build议么?

除非我错过了这些,这是你所需要的

android:inputType="numberDecimal|numberSigned" 

请使用此代码

  et.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_CLASS_NUMBER); 

您必须在布局的EditText标记(* .xml)中添加numberSigned

  <EditText android:inputType="numberDecimal|numberSigned"></EditText> 

祝你好运!

EditText.setInputType设置InputType.TYPE_NUMBER_FLAG_SIGNED

我认为创build您自己的自定义keylistener可能是您最好的select。 我做了类似的事情。 我创build了一个像这样的SignedDecimalKeyListener:

 public class SignedDecimalKeyListener extends NumberKeyListener { private char[] mAccepted; private static SignedDecimalKeyListener sInstance; private static final char[] CHARACTERS = new char[] { '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.' }; @Override protected char[] getAcceptedChars() { return mAccepted; } private SignedDecimalKeyListener() { mAccepted = CHARACTERS; } public static SignedDecimalKeyListener getInstance() { if(sInstance != null) return sInstance; sInstance = new SignedDecimalKeyListener(); return sInstance; } public int getInputType() { return InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL; } @Override public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { CharSequence out = super.filter(source, start, end, dest, dstart, dend); if(out != null) { source = out; start = 0; end = out.length(); } //Only allow a '-' to be the very first char //and don't allow '.' to be the first char if(dstart > 0 && source.equals("-") || dstart == 0 && source.equals(".")) { SpannableStringBuilder stripped = null; stripped = new SpannableStringBuilder(source, start, end); stripped.delete(start, end); if(stripped != null) return stripped; } else if(source.equals(".")) { for(int lo = dend-1; lo > 0; lo--) { char c = dest.charAt(lo); if(source.equals(String.valueOf(c))) { SpannableStringBuilder stripped = null; stripped = new SpannableStringBuilder(source, start, end); stripped.delete(start, end); if(stripped != null) return stripped; } } } if(out != null) return out; else return null; } } 

然后使用你的edittext这个自定义的keylistener像这样:

 EditText myEditText = (EditText)findViewById(R.id.myedittext); myEditText.setKeyListener(SignedDecimalKeyListener.getInstance()); 

我不知道inputtypes,但你总是可以创build自己的文本filter,并将其附加到editText字段。 那个很简单 你可以检查每个字符,只允许数字,点或你喜欢的任何自定义字符。

结束对每个轴使用模式和匹配器,保持实际的文本input格式打开。

三个轴的字段,提交button触发onSave。 字段进行validation,并且发生插入,或者向提交者提出有关“轴”字段所需格式的错误消息。 正确的格式是1-3位数字,可以以“ – ”开头,后面可以select“。”。 和额外的数字。

试试这个代码:

 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 } } 

}

这是为我工作。

etbox.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_CLASS_NUMBER);