码迷,mamicode.com
首页 > 其他好文 > 详细

TextInputLayout

时间:2017-04-10 17:10:14      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:ext.get   address   tin   rri   please   click   ext2   cte   textview   

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

标签:ext.get   address   tin   rri   please   click   ext2   cte   textview   

原文地址:http://www.cnblogs.com/mazing/p/6689431.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!