1. 程式人生 > >Android Design Support Library(一)--TextInputLayout簡析

Android Design Support Library(一)--TextInputLayout簡析

2015年google IO大會上介紹的Android Design Support Library庫中的八個新控制元件,今天學習了TextInputLayout的簡單使用。

在使用Android Design Support Library庫的時候,只是找到了如何在android sutido中新增庫,eclipse的使用方法還沒有找到。但是想想15年年底,google已經停止了對eclipse的更新了,所以本人也就不糾結這個。

新增Android Design Support Library庫,只需要在gradle中新增
compile ‘com.android.support:design:23.0.0即可

在佈局檔案中,TextInputLayout需要包裹一個EditText控制元件。


   <android.support.design.widget.TextInputLayout
        android:id="@+id/textinputlayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!--必須包含一個EditText-->
        <EditText
            android:id
="@+id/edittext" android:layout_width="match_parent" android:layout_height="wrap_content"/>
</android.support.design.widget.TextInputLayout>

在類中的呼叫為


        mTextInputLayout = (TextInputLayout) findViewById(R.id.textinputlayout);

        EditText editText = mTextInputLayout.getEditText();
        /************** 設定提示資訊 **************/
mTextInputLayout.setHint("請輸入。。。"); mTextInputLayout.setError("輸入錯誤!!"); mTextInputLayout.setErrorEnabled(true);

當對EditText新增監聽,可以實現不同的hint、error提示。在呼叫了

setError(),方法則必須在該方法之後呼叫setErrorEnabled(true)方法,這樣才能顯示error提示訊息。

在學習使用的時候,發現hint的顏色是紅色的,就想到是否能修改hint的字型顏色。查閱了相關的資料,發現在 styles檔案中的

    <item name="colorAccent">@color/colorAccent</item>

是可以修改hint字型的顏色的。

以上就是我學習TextInputLayout的一些記錄。