怎么在Android中使用TextInputLayout创建一个登陆页面

  介绍

怎么在Android中使用TextInputLayout创建一个登陆页面?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

<强> 1。实现TextInputLayout

<强>第一步:强创建一个新的项目

在Android工作室中选择新比;新项目。填入所需的信息然后创建项目。我的例子的目标api是17岁,这是设计支持库支持的最小api版本。这个级别api的基本上已经支持绝大多数设备了。我把命主活动名为LoginActivity,它的布局文件命名为activity_login。xml。

创建完项目之后,在主活动中把Android工作室自动产生的onCreateOptionsMenu和onOptionsItemSelected方法删掉。我们要创建的登陆界面不需要菜单所以删掉这些方法是ok的。记得也删掉res/菜单目录中xml的菜单文件。

<强>第二步:强导入支持库

要使用TextInputLayout控件,你需要导入两个图书馆。第一个是appcompat-v7,它确保材料风格可以向后兼容第。二个是设计支持库。在你的构建。gradle文件中,添加如下依赖:

,, dependencies  {   ,,,compile  fileTree (dir: & # 39;填词# 39;,,包括:,(& # 39;* . jar # 39;])      ,,,compile  & # 39; com.android.support:设计:22.2.0& # 39;   ,,,compile  & # 39; com.android.support: appcompat-v7:22.2.0& # 39;   以前,,}

如果Gradle没有自动询问同步项目,选择构建菜单中的使模块的应用# 39;,或者按F9。这样Android工作室编译系统会自动获取必要的资源,然后你就能够使用需要的类了。

<强>第三步:设计用户界面

这个项目的用户界面非常简单。它显示了一个“欢迎“文字(可以很容易替换成标志什么的)与两个EditText元素,一个是为用户名准备的,一个是为密码准备的。布局中还包含了一个触发登陆流程的按钮。背景颜色是扁平风格的灰色。

另一个重要的细节是记得正确设置EditText的inputType属性。第一个EditText的inputType应该设置成textEmail,而第二个应该设置成textPassword。下面是布局的样子:

& lt; LinearLayout  xmlns: Android=癶ttp://schemas.android.com/apk/res/android"   ,,,xmlns:工具=癶ttp://schemas.android.com/tools"   ,,,android:背景=? e3e3e3"   ,,,android: layout_width=癿atch_parent"   ,,,android: layout_height=癿atch_parent"   ,,,android:填充=癅dimen/activity_horizontal_margin"   ,,,工具:上下文=?LoginActivity"   ,,,android:取向=皏ertical"比;      ,,,& lt;使用   ,,,,,android: layout_width=癿atch_parent"   ,,,,,android: layout_height=皐rap_content"   ,,,,,android: layout_weight=?.5”;   ,,,,,android:取向=皏ertical"比;      ,,,,,& lt; TextView   ,,,,,,,android: layout_width=癿atch_parent"   ,,,,,,,android: layout_height=皐rap_content"   ,,,,,,,android: layout_centerInParent=皌rue"   ,,,,,,,android:重力=癱enter"   ,,,,,,,android:文本=癢elcome"   ,,,,,,,android: textSize=?0 sp"   ,,,,,,,android:输入textColor=? 333333“/比;      ,,,& lt;/RelativeLayout>         ,,,& lt; LinearLayout   ,,,,,android: layout_width=癿atch_parent"   ,,,,,android: layout_height=皐rap_content"   ,,,,,android: layout_weight=?.5”;   ,,,,,android:取向=皏ertical"比;      ,,,,EditText & lt;   ,,,,,,,android: id=癅 + id/username"   ,,,,,,,android: layout_width=癿atch_parent"   ,,,,,,,android: layout_height=皐rap_content"   ,,,,,,,android: inputType=皌extEmailAddress"/比;      ,,,,EditText & lt;   ,,,,,,,,,android: id=癅 + id/password"   ,,,,,,,,,android: layout_width=癿atch_parent"   ,,,,,,,,,android: layout_height=皐rap_content"   ,,,,,,,,,android: inputType=皌extPassword"/比;      ,,,,,& lt;按钮   ,,,,,,,android: id=癅 + id/btn"   ,,,,,,,android: layout_marginTop=? dp"   ,,,,,,,android: layout_width=癿atch_parent"   ,,,,,,,android: layout_height=皐rap_content"   ,,,,,,,android:文本=癓ogin"/比;      ,,,& lt;/LinearLayout>      & lt;才能/LinearLayout>

你可能还想去掉应用酒吧,也就是过去说的actionbar,编辑风格。null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

怎么在Android中使用TextInputLayout创建一个登陆页面