怎么在edittext设置光标颜色用代码设置内容

EditText想要让默认输入类型为数字,并且可以切换到其他输入类型
[问题点数:40分]
EditText想要让默认输入类型为数字,并且可以切换到其他输入类型
[问题点数:40分]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
本帖子已过去太久远了,不再提供回复功能。15:48 提问
动态的设置 EditText 的数字值
我想动态的设置一个 EditText的数字值。使用下面的代码:
weightInput.setInputType(InputType.TYPE_CLASS_PHONE);
weightInput.setKeyListener(DigitsKeyListener.getInstance());
同时,也希望能够包括一个小数位 (.)。如何设置呢?
按赞数排序
[android]EditText的一些设置
如果是要输入数字和小数点,可以在xml中设置:
android:digits="."
或者在代码中设置监听器:
editText.setKeyListener(new NumberKeyListener(){
protected char[] getAcceptedChars(){
char[] mychar = {'a','b','c','1','2','3'};//这里是可以输入的字符
android:inputType="number"
android:digits="."
weightInput.setKeyListener(DigitsKeyListener.getInstance("."));
。。这个好像不行,没有单独的一点。如果非要在代码中实现的话,可以用weightInput.addTextChangedListener(TextWatcher)
用watcher来监听。。
21142关注|1809收录
其他相似问题【整理】Android中EditText中的InputType类型含义与如何定义
经过一些Android中EditText方面的折腾:
然后对于EditText(或TextView)中的InputType的值的含义和类型,以及如何定义,有了个更清晰点的认识。
现在整理如下:
EditText的InputType属性,可以在代码中设置,也可以预先在xml中定义
设置EditText的InputType属性,最简单省事的办法就是在定义EditText的xml中直接设置。
想要设置一个可编辑的文本框的输入内容为只能输入数字,则就可以:
(1)xml中定义InputType为number
android:id=&@+id/variableValue&
android:inputType=&number& /&
(2)代码中设置InputType为TYPE_CLASS_NUMBER | TYPE_NUMBER_VARIATION_NORMAL
EditText variableValueView = (EditText) variableLayout.findViewById(R.id.variableValue);
int inputType = InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL;
variableValueView.setInputType(inputType);
这样的话,之后界面中生成的EditText,当点击后要输入内容的时候,弹出的输入法,自动变成那种只能输入数字的小键盘类型的了:
另外,附上,正常的普通字符串,即:
android:inputType=&text&
或代码中:
someEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL);
时,显示出来的输入法键盘的效果:
EditText的InputType属性对应的xml定义有哪些,以及代码中设置的InputType类型有哪些
知道了设置EditText的InputType属性值,既可以通过xml中定义,也可以在代码中设置为InputType的某种值,但是到底这些值有哪些,以及分别对应的含义是啥,则可以参考官网:
中的完整的列表:
Description
There is no content type. The text is not editable.
Just plain old text. Corresponds to
textCapCharacters
Can be combined with text and its variations to request capitalization of all characters. Corresponds to .
textCapWords
Can be combined with text and its variations to request capitalization of the first character of every word. Corresponds to .
textCapSentences
Can be combined with text and its variations to request capitalization of the first character of every sentence. Corresponds to .
textAutoCorrect
Can be combined with text and its variations to request auto-correction of text being input. Corresponds to .
textAutoComplete
Can be combined with text and its variations to specify that this field will be doing its own auto-completion and talking with the input method appropriately. Corresponds to .
textMultiLine
Can be combined with text and its variations to allow multiple lines of text in the field. If this flag is not set, the text field will be constrained to a single line. Corresponds to .
textImeMultiLine
Can be combined with text and its variations to indicate that though the regular text view should not be multiple lines, the IME should provide multiple lines if it can. Corresponds to .
textNoSuggestions
Can be combined with text and its variations to indicate that the IME should not show any dictionary-based word suggestions. Corresponds to .
Text that will be used as a URI. Corresponds to
textEmailAddress
Text that will be used as an e-mail address. Corresponds to
textEmailSubject
Text that is being supplied as the subject of an e-mail. Corresponds to
textShortMessage
Text that is the content of a short message. Corresponds to
textLongMessage
Text that is the content of a long message. Corresponds to
textPersonName
Text that is the name of a person. Corresponds to
textPostalAddress
Text that is being supplied as a postal mailing address. Corresponds to
textPassword
Text that is a password. Corresponds to
textVisiblePassword
Text that is a password that should be visible. Corresponds to
textWebEditText
Text that is being supplied as text in a web form. Corresponds to
textFilter
Text that is filtering some other data. Corresponds to
textPhonetic
Text that is for phonetic pronunciation, such as a phonetic name field in a contact entry. Corresponds to
textWebEmailAddress
Text that will be used as an e-mail address on a web form. Corresponds to
textWebPassword
Text that will be used as a password on a web form. Corresponds to
A numeric only field. Corresponds to
numberSigned
Can be combined with number and its other options to allow a signed number. Corresponds to
numberDecimal
Can be combined with number and its other options to allow a decimal (fractional) number. Corresponds to
numberPassword
A numeric password field. Corresponds to
For entering a phone number. Corresponds to .
For entering a date and time. Corresponds to
For entering a date. Corresponds to
For entering a time. Corresponds to
如此,就可以自己去在xml或代码中,分别试试,每种不同的InputType对应的都是什么效果了。
注意:通过代码给InputType赋值时,不是设置TYPE_XXX_VARIATION_YYY,而是要设置TYPE_CLASS_XXX | TYPE_XXXX_VARAITION_YYY
之前在代码中给InputType设置值,错写成:
inputType = InputType.TYPE_DATETIME_VARIATION_TIME;
导致,EditText点击后,不显示输入法键盘,改为正确的:
inputType = InputType.TYPE_CLASS_DATETIME | InputType.TYPE_DATETIME_VARIATION_TIME;
就可以正常的显示键盘了。
而后,也注意到官网
的解释中的示例:
A password field with with the password visible to the user:
inputType = TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
A multi-line postal address with automatic capitalization:
inputType = TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_POSTAL_ADDRESS | TYPE_TEXT_FLAG_MULTI_LINE
A time field:
inputType = TYPE_CLASS_DATETIME | TYPE_DATETIME_VARIATION_TIME
中提示的:
“Must be one or more (separated by ‘|’) of the following constant values.”
需要一个或多个值,中间通过竖杠&|&去抑或(按位或)的。
共享此文章:
免费的格式化Javascript源码的网站
查询Unicode字符,且还带Oct,Decimal,Hex,HTML Entity
HTML和Javascript都支持,很好用。android edittext 设置只允许输入整数,(设置输入类型)
1. 其实通过XML进行配置特别简单,直接在xml中来一句,android:inputtype=number(整数), 然后你想设置带小数点的,可以number改为numberDecimal 通过xml比较简单,此处不再多说。主要是在代码里面设置 今天干活也比较墨迹,然后,查了一上午这个小东西,才
1. 其实通过XML进行配置特别简单,直接在xml中来一句,android:inputtype=&number&(整数), 然后你想设置带小数点的,可以number改为numberDecimal
通过xml比较简单,此处不再多说。主要是在代码里面设置
今天干活也比较墨迹,然后,查了一上午这个小东西,才弄出来。
android:inputType对应的方法为setRawInputType(int),
public void setRawInputType (int type)
因为我是把edittext重新封装了一下,有的地方是只允许整数,有的地方是即小数,然后,最初我使用的是
int inputType = InputType.TYPE_CLASS_NUMBER ;
dataText.setRawInputType(inputType);
但是特别恶心的一点,设置了之后,居然键盘怎么也出不来了,当时就以为是setRawInputType这个方法不行,肯定有bug,后来我又改成了设置事件
dataText.setKeyListener(new DigitsKeyListener(false, true));
但是这样设置了之事,几乎对我没起作用,因为我只允许输入整数,但这个事件设置了,我还是可以照样可以输入小数点,于我又是翻过去研究了一下setRawInputType这个方法,而后改成了
int inputType = InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL;
dataText.setRawInputType(inputType);
这次键盘到是出来了,但还是能够输入小数点,监于今天本来就比较墨迹,结果通过xml一句话能搞定的,我试了三种方法都不行之后,我就歇了半个小时,得瑟了一会,回过头来,又试了一下那个事件,改成了:
dataText.setKeyListener(DigitsKeyListener.getInstance(&&));
顺利通过。。。。
当时就觉得吧心里小美了一下,不过要细心,要耐心吧,如果简单的一个东西,我居然浪费了这么长时间。。。严重鄙视我自己,然后就是一定要多看源码啊,源码里面真的有黄金屋
你最喜欢的& EditText MaxLength设置
EditText MaxLength设置
我们可以在xml中设定EditText的最大长度,如下:
android:layout_width = "fill_parent"android:layout_height = "wrap_content"android:id = "@+id/mEdit"android:maxLength = "10"/&
可是如何在代码中设置呢?如下所示:
EditText mEdit = (EditText)findViewById(R.id.mEdit);InputFilter[] filters = {new InputFilter.LengthFilter(9)};mEdit.setFilters(filters);
本文目前尚无任何 trackbacks 和 pingbacks.

我要回帖

更多关于 edittext设置光标位置 的文章

 

随机推荐