今天 我来分享一下这两个方法的使用:setInputType 简单来说 就是设置EditText的输入法和输入内容限制:
/**
* Set the type of the content with a constant as defined for {@link EditorInfo#inputType}. This
* will take care of changing the key listener, by calling {@link #setKeyListener(KeyListener)},
* to match the given content type. If the given content type is {@link EditorInfo#TYPE_NULL}
* then a soft keyboard will not be displayed for this text view.
*
* Note that the maximum number of displayed lines (see {@link #setMaxLines(int)}) will be
* modified if you change the {@link EditorInfo#TYPE_TEXT_FLAG_MULTI_LINE} flag of the input
* type.
*
* @see #getInputType()
* @see #setRawInputType(int)
* @see android.text.InputType
* @attr ref android.R.styleable#TextView_inputType
*/
public void setInputType(int type)
设置会改变输入内容的格式,可以达到限制输入类型的要求,设置的格式主要在
InputType 中。
/**
* Bit definitions for an integer defining the basic content type of text
* held in an {@link Editable} object. Supported classes may be combined
* with variations and flags to indicate desired behaviors.
*
* <h3>Examples</h3>
*
* <dl>
* <dt>A password field with with the password visible to the user:
* <dd>inputType = TYPE_CLASS_TEXT |
* TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
*
* <dt>A multi-line postal address with automatic capitalization:
* <dd>inputType = TYPE_CLASS_TEXT |
* TYPE_TEXT_VARIATION_POSTAL_ADDRESS |
* TYPE_TEXT_FLAG_MULTI_LINE
*
* <dt>A time field:
* <dd>inputType = TYPE_CLASS_DATETIME |
* TYPE_DATETIME_VARIATION_TIME
* </dl>
*/
public interface InputType
可以看到 这里面主要有三种输入格式:TEXT, NUMBER, DATETIME 通过这三种基本的输入格式 然后做一些变换:连接、密码、小数点、校验 等等
我们再来看看setRawInputType 的作用:
/**
* Directly change the content type integer of the text view, without
* modifying any other state.
* @see #setInputType(int)
* @see android.text.InputType
* @attr ref android.R.styleable#TextView_inputType
*/
public void setRawInputType(int type)
这个方法只改变 TextView的输入法 格式,不改变输入法内容的限制,什么意思呢,也就是说可以通过改变这个方法 达到改变 键盘的输入显示,比如数字键盘,字符键盘,但是这个方法不改变你原有的 设置的字符限制。这个方法充分的发挥了输入的变化作用了。比如我可以设置 可以输入字符,但是我希望默认弹出的键盘为数字键盘就可以这样进行设置:
就可以达到效果了。setInputType(InputType.TYPE_CLASS_TEXT); setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
这里面有一个要注意的地方InputType中:TYPE_CLASS_TEXT
TYPE_CLASS_NUMBER
TYPE_CLASS_DATETIME
TYPE_CLASS_PHONE
这几个都是最基本的类型,TYPE_NUMBER_FLAG_SIGNED 这个类型是基于TYPE_CLASS_NUMBER 这个类型的,所以在使用的时候,一定是
需要先使用基本的类型,然后再是使用扩展的例如:
不然单纯的设置 InputType.TYPE_NUMBER_FLAG_DECIMAL 是不起作用的setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);