首页 > 代码库 > TextInputLayout
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。