如何在Android中动态或静态设置文本字段属性

时间:2012-04-23 12:40:53

标签: android

如何在Android中为文本字段设置以下两个属性:

属性1 )如何设置包含某些文本的文本字段,例如“输入数字”但单击它时文本字段会自动变为空?

Property:2 )假设我的应用程序中有两个文本字段。我的第一个文本字段只接受整数,第二个文本字段接受“name,address”等文本。但是我错误地在我的第一个文本字段中输入了一些文本,如“apple”或“ball”,只接受数字。

如何设置第一个文本字段,以便当我点击第二个文本字段时,我的第一个文本字段应显示一个toast消息或更改输入数据的颜色以指示无效输入?

3 个答案:

答案 0 :(得分:0)

关于你的第一个问题......

  

如何设置包含某些文本的文本字段,例如“输入数字”,但是当我们单击文本字段时,文本应自动从文本字段中删除变为空。

您正在寻找EditText视图具有的提示属性。在此处阅读更多内容:http://developer.android.com/reference/android/widget/TextView.html#attr_android:hint

对于第二个问题,你不需要经历所有麻烦。只需为EditText设置 inputType 属性即可。这将强制键盘(虚拟或物理)仅使用字符串或数字,具体取决于您指定的输入类型。在此处阅读更多内容:http://developer.android.com/reference/android/widget/TextView.html#attr_android:inputType

答案 1 :(得分:0)

属性1)您可以通过向EditText添加ht来执行此操作:

<EditText

                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:hint="Your text here"
 />

物业2) 名称:

  <EditText

                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:hint="Your text here"
                        android:inputType="textPersonName"
     />

代表数字:

<EditText

                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:hint="Your text here"
                        android:inputType="number"
     />

为所有参数设置look here

答案 2 :(得分:0)

查看第二期的setError()方法。