Android Design Support Library(2)- TextInputLayout的使用

來源:互聯網
上載者:User

標籤:span   print   support   width   ring   edittext   javascrip   示範   XML   

原創文章,轉載請註明 http://blog.csdn.net/leejizhou/article/details/50494634

這篇文章介紹下Android Design Support Library中的TextInputLayout的使用,假設你還不知道怎麼使用這個Design Library請參考 http://blog.csdn.net/leejizhou/article/details/50479934,TextInputLayout使你的EditText更具有Material Design的感覺。能夠便捷的把EditText的提示資訊挪到上方而且能夠方便的進行錯誤資訊提示。

廢話不多說。看效果 :)

控制項定義  <android.support.design.widget.TextInputLayout        android:id="@+id/tl_password"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_margin="8dp">        <EditText android:id="@+id/password"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:inputType="textEmailAddress"            android:hint="Password"         ></EditText>    </android.support.design.widget.TextInputLayout>

TextInputLayout是一個父容器控制項。包裹了EditText,也沒什麼特別的屬性,使用非常easy,切記它一定是和EditText一起搭配使用的。

TextInputLayout的經常用法

tl_password.setHint("Username"); //EditText獲得焦點後在上面顯示的文字tl_password.setErrorEnabled(true); //開啟錯誤提醒tl_password.setError("密碼不可為空!"); //錯誤提醒的文字tl_password.setErrorEnabled(false); //關閉錯誤提醒

示範效果GIF的具體原始碼

Layout

<?xml version="1.0" encoding="utf-8"?

><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.leejz.textinputlayout.MainActivity"> <android.support.design.widget.TextInputLayout android:id="@+id/tl_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8dp"> <EditText android:id="@+id/username" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Username" ></EditText> </android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout android:id="@+id/tl_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8dp"> <EditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Password" ></EditText> </android.support.design.widget.TextInputLayout> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="18dp" android:text="Click" android:id="@+id/button" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Blog:http://blog.csdn.net/leejizhou" /></LinearLayout>

Activity

import android.support.design.widget.TextInputLayout;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.text.TextUtils;import android.view.View;import android.widget.EditText;public class MainActivity extends AppCompatActivity {    private EditText username;    private EditText password;    TextInputLayout tl_username;    TextInputLayout tl_password;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        tl_username=(TextInputLayout) findViewById(R.id.tl_username);        tl_username.setHint("Username");        tl_password=(TextInputLayout) findViewById(R.id.tl_password);        tl_password.setHint("Password");        //兩種得到EditText對象的方法       // username=(EditText)findViewById(R.id.username);       // password=(EditText)findViewById(R.id.password);        username=tl_username.getEditText();        password=tl_password.getEditText();        //Button Click        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {               if(TextUtils.isEmpty(password.getText().toString())){                   tl_password.setErrorEnabled(true);                   tl_password.setError("密碼不可為空。");               }else{                   tl_password.setErrorEnabled(false);               }            }        });    }}

Ok,有疑問的地方的能夠在下方留言,感謝。

Android Design Support Library(2)- TextInputLayout的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.