how to make dynamically edittext field for accepting only double and float values?
如何动态编辑ext字段,只接受双值和浮点值?
4 个解决方案
#1
16
Use this method in your Activity:
在你的活动中使用这个方法:
EditText et = (EditText) findViewById(R.id.text01);
et.setInputType(0x00002002);
or
或
EditText et = (EditText) findViewById(R.id.text01) ;
et.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
here text01
is id of EditText
field in your R.java
file;
这里text01是您的R中EditText字段的id。java文件;
Reference:
参考:
http://developer.android.com/reference/android/widget/TextView.html#attr_android:inputType
http://developer.android.com/reference/android/widget/TextView.html attr_android:inputType
#2
4
Maybe a nicer solution would be using
也许有更好的解决办法
et.setInputType(InputType.TYPE_CLASS_NUMBER
| InputType.TYPE_NUMBER_FLAG_DECIMAL);
Edit: I have also added the flag InputType.TYPE_NUMBER_FLAG_DECIMAL
to accept dobules too.
编辑:我还在puputtype中添加了旗帜。TYPE_NUMBER_FLAG_DECIMAL可以接受dobules。
#3
0
You can also use the setInputType(int type)
function:
您还可以使用setInputType(int类型)函数:
EditText e
e.setInputType(InputType.Number) //Many other options are also available
#4
0
et.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
et.setRawInputType(InputType。TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
where setRawInputType it corresponds to the android:inputType attribute.
其中setRawInputType对应于android:inputType属性。
#1
16
Use this method in your Activity:
在你的活动中使用这个方法:
EditText et = (EditText) findViewById(R.id.text01);
et.setInputType(0x00002002);
or
或
EditText et = (EditText) findViewById(R.id.text01) ;
et.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
here text01
is id of EditText
field in your R.java
file;
这里text01是您的R中EditText字段的id。java文件;
Reference:
参考:
http://developer.android.com/reference/android/widget/TextView.html#attr_android:inputType
http://developer.android.com/reference/android/widget/TextView.html attr_android:inputType
#2
4
Maybe a nicer solution would be using
也许有更好的解决办法
et.setInputType(InputType.TYPE_CLASS_NUMBER
| InputType.TYPE_NUMBER_FLAG_DECIMAL);
Edit: I have also added the flag InputType.TYPE_NUMBER_FLAG_DECIMAL
to accept dobules too.
编辑:我还在puputtype中添加了旗帜。TYPE_NUMBER_FLAG_DECIMAL可以接受dobules。
#3
0
You can also use the setInputType(int type)
function:
您还可以使用setInputType(int类型)函数:
EditText e
e.setInputType(InputType.Number) //Many other options are also available
#4
0
et.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
et.setRawInputType(InputType。TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
where setRawInputType it corresponds to the android:inputType attribute.
其中setRawInputType对应于android:inputType属性。