-
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
-
android:orientation=“vertical”
-
android:layout_width=“fill_parent”
-
android:layout_height=“fill_parent”
-
>
-
<EditText
-
android:id="@+id/edit_text"
-
android:layout_width=“fill_parent”
-
android:layout_height=“wrap_content”
-
android:maxLength=“40”
-
android:hint=“请输入用户名…”/>
-
</LinearLayout>
运行应用就会看到如下的效果:
看看吧,简洁明了还不用新增一个TextView说明,也不影响用户操作。
- 上面列出了空白时的提示文字,有的人说了,我不想要这个灰色的提示文字,和我的应用整体风格不协调,那也行啊,我们可以换颜色,怎么换呢,就是通过android:textColorHint属性设置你想要的颜色。修改main.xml如下:
Xml代码 [](https://
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
blog.youkuaiyun.com/u014581901/article/details/50700686 “收藏这段代码”)
- <?xml version\="1.0" encoding\="utf-8"?>
-
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
-
android:orientation=“vertical”
-
android:layout_width=“fill_parent”
-
android:layout_height=“fill_parent”
-
>
-
<EditText
-
android:id="@+id/edit_text"
-
android:layout_width=“fill_parent”
-
android:layout_height=“wrap_content”
-
android:maxLength=“40”
-
android:hint=“请输入用户名…”
-
android:textColorHint="#238745"/>
-
</LinearLayout>
运行程序效果如下:
看到了吧,颜色已经变了。。
- 还有一个比较实用的功能,就是设置EditText的不可编辑。设置android:enabled="false"可以实现不可编辑,可以获得焦点。这时候我们看到EditText和一个TextView差不多:
- 实现类似html中Textarea的文本域。在Android中没有专门的文本域组件,但是可以通过设置EditText的高来实现同样的文本域功能。修改main.xml如下:
- <?xml version\="1.0" encoding\="utf-8"?>
-
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
-
android:orientation=“vertical”
-
android:layout_width=“fill_parent”
-
android:layout_height=“fill_parent”
-
>
-
<EditText
-
android:id="@+id/edit_text"
-
android:layout_width=“fill_parent”
-
android:layout_height=“200dip”/>
-
</LinearLayout>
运行程序效果如下: