TextInputLayout
Posted 火热火热7
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TextInputLayout相关的知识,希望对你有一定的参考价值。
1.TextInputLayout是一种嵌套格式,首先将你要输入信息的Views放置在嵌套格式里,每个View分别嵌套
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.design.widget.TextInputLayout>
2.调整好UI之后,使用各种method来使用
setError(String),设置error hint
出现error后使用requestFocus()来设置焦点
3.完整的layout如下:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mazingtec.myapplication.LoginActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="32dp"
android:text="Login"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.design.widget.TextInputLayout
android:id="@+id/ad1"
android:layout_width="146dp"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView">
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="60dp"
android:ems="10"
android:hint="Email"
android:inputType="textEmailAddress"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/ad2"
android:layout_width="146dp"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ad1">
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="32dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.5" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="Log in"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ad2" />
</android.support.constraint.ConstraintLayout>
4.完整的代码如下:
1 package com.mazingtec.myapplication; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.text.TextUtils; 6 import android.view.View; 7 import android.widget.Button; 8 import android.widget.EditText; 9 import android.widget.Toast; 10 11 public class LoginActivity extends AppCompatActivity implements View.OnClickListener{ 12 13 private EditText editText; 14 private EditText editText2; 15 private Button button2; 16 17 @Override 18 protected void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_login); 21 22 editText = (EditText) findViewById(R.id.editText); 23 editText2 = (EditText) findViewById(R.id.editText2); 24 button2 = (Button) findViewById(R.id.button2); 25 26 button2.setOnClickListener(this); 27 28 } 29 30 @Override 31 public void onClick(View v) { 32 if(v == button2) { 33 onLonin(); 34 } 35 36 } 37 38 private void onLonin() { 39 String email = editText.getText().toString(); 40 String password = editText2.getText().toString(); 41 42 if(TextUtils.isEmpty(email)) { 43 editText.setError("Please enter your email"); 44 editText.requestFocus(); 45 return; 46 } 47 48 if(TextUtils.isEmpty(password)) { 49 editText2.setError("Please enter your password"); 50 editText2.requestFocus(); 51 return; 52 } 53 54 Toast.makeText(this, "Done!", Toast.LENGTH_LONG).show(); 55 56 57 } 58 }
以上是关于TextInputLayout的主要内容,如果未能解决你的问题,请参考以下文章
TextInputLayout 和 TextInputEditText 的简单介绍以及使用
自定义 TextInputLayout 在后按时设置错误的值
如何在android中动态设置样式为TextInputLayout?
如何将 TextInputLayout 与 Theme.material 一起使用?