Material Design -- TextInputLayout

  1. 導(dǎo)入Support Library

    想要使用TextInputLayout 控件迅腔,必須先在自己的項(xiàng)目中導(dǎo)入 Support Liarbry倍阐,如果沒有下載這個(gè)支持庫(kù)纵散,可以打開 SDK Manager 去下載

這里寫圖片描述

build.gradle 加入以下語(yǔ)句

compile 'com.android.support:design:23.2.0'

當(dāng)然這個(gè)代碼取決于你的Support Library 的版本竭鞍。

接下來我們就可以使用 TextInputLayout 控件了恋沃。

2.使用TextInputLayout創(chuàng)建一個(gè)簡(jiǎn)單的登錄界面

效果圖

這里寫圖片描述

首先介紹一下經(jīng)常用到幾個(gè)方法

setHint(CharSequence hint); //用于設(shè)置提示文字
setError(CharSequence error);//用于設(shè)置錯(cuò)誤提示信息
setErrorEnabled(boolean enabled);//用于設(shè)置是否將錯(cuò)誤信息顯示出來

接下來介紹如何在XML 文件中定義 TextInputLayout,它的簡(jiǎn)單使用方法如下:

<android.support.design.widget.TextInputLayout
        android:id="@+id/textInputLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp">
        
        <EditText
            android:id="@+id/edittext"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    
    </android.support.design.widget.TextInputLayout>

之后在Activity 中獲取到這個(gè)TextInputLayout顺呕,然后使用上面介紹的幾個(gè)函數(shù)枫攀,對(duì)其進(jìn)行相應(yīng)設(shè)置即可。

TextInputLayout mTextInputLayout = (TextInputLayout) findViewById(R.id.textInputLayout);
mTextInputLayout.setHint("UserName ");

好了株茶,介紹完它的簡(jiǎn)單使用方法来涨,下面就來實(shí)現(xiàn)LOGIN界面,實(shí)際上是很容易實(shí)現(xiàn)的启盛。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#e3e3e3"
    android:orientation="vertical">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.4"
        android:id="@+id/relative">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="48sp"
            android:text="Welcome"
            android:gravity="center"
            android:layout_centerInParent="true"/>
    </RelativeLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.6"
        android:orientation="vertical">
        <android.support.design.widget.TextInputLayout
            android:id="@+id/textInputLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp">
                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
        </android.support.design.widget.TextInputLayout>

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

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
        </android.support.design.widget.TextInputLayout>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:text="LOGIN"/>
     </LinearLayout>
</LinearLayout>



之后在主函數(shù)中進(jìn)行如下操作

public class MainActivity extends AppCompatActivity {

    private TextInputLayout mTextInputLayout,mTextInputLayout2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         mTextInputLayout = (TextInputLayout) findViewById(R.id.textInputLayout);
        mTextInputLayout2 = (TextInputLayout) findViewById(R.id.textInputLayout2);
        mTextInputLayout.setHint("UserName ");
        mTextInputLayout2.setHint("Password");

        EditText editText = mTextInputLayout.getEditText();
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.length() > 10){
                    mTextInputLayout.setError("The length more than 10");
                    mTextInputLayout.setErrorEnabled(true);
                } else {
                    mTextInputLayout.setErrorEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

如以上代碼示蹦掐,在Username 輸入框內(nèi)添加了 監(jiān)聽事件,當(dāng)輸入字符長(zhǎng)度 10 時(shí)僵闯,便將錯(cuò)誤顯示出來卧抗,Password輸入框我并沒有去實(shí)現(xiàn),因?yàn)槭窍嗤膶?shí)現(xiàn)方法鳖粟。在真正的登錄系統(tǒng)中社裆,我們還需要去檢驗(yàn)輸入的合法性,相應(yīng)檢驗(yàn)函數(shù)可以在LOGIN 按鈕的監(jiān)聽事件內(nèi)去執(zhí)行向图。

3.ActionBar 問題

為了美觀泳秀,在登陸界面一般都是將 ActionBar 進(jìn)行隱藏的,這里有兩種方法去隱藏张漂。

第一種方法十分簡(jiǎn)單晶默,直接設(shè)置一個(gè)不含ActionBar 的主題,比如我使用的是 Theme.AppCompat.DayNight.NoActionBar 主題航攒,所以自然不含有ActionBar磺陡。

第二種方法,

<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        
        // 隱藏ActionBar
        <item name="android:windowActionBar">false</item>
        <item name="windowActionBar">false</item>
        <item name="android:windowNoTitle">true</item>
        <item name="windowNoTitle">true</item>
    </style>

好了漠畜,通過以上兩種方法都可以隱藏ActionBar币他。

4.隱藏 KeyBoard

以上登錄界面,當(dāng)我們輸入完密碼之后憔狞,會(huì)有一個(gè)非常惱人的問題蝴悉,就是除非我們按后退鍵,否則輸入鍵盤不會(huì)消失瘾敢∨墓冢或許在某些機(jī)型上尿这,只許輕輕點(diǎn)擊一下Home 鍵就可以實(shí)現(xiàn)返回的功能,但是在另一些機(jī)型上庆杜,比如說我的調(diào)試機(jī) 魅族MX2射众,必須上劃 Home 鍵,才可以實(shí)現(xiàn)返回功能晃财,又由于叨橱,點(diǎn)擊Home鍵是返回主菜單,經(jīng)常出現(xiàn)按錯(cuò)的問題断盛,現(xiàn)在我們來實(shí)現(xiàn)一個(gè)功能罗洗,讓我們點(diǎn)擊屏幕上的其他控件,以隱藏輸入鍵盤钢猛。

在以上登陸界面內(nèi)伙菜,輸入鍵盤彈出時(shí),屏幕的絕大部分空間都被RelativeLayout控件占據(jù)厢洞,所以我為這個(gè)控件設(shè)置了一個(gè)監(jiān)聽事件仇让,當(dāng)用戶輸入完點(diǎn)擊這個(gè)控件,便去隱藏KeyBoard躺翻,主要添加代碼如下

RelativeLayout mRelativeLayout = (RelativeLayout) findViewById(R.id.relative);
        mRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hideKeyboard();
            }
        });


 private void hideKeyboard() {
            ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).
                    hideSoftInputFromWindow(mRelativeLayout.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }

好了,通過以上設(shè)置有解決了KeyBoard的問題卫玖,這個(gè)時(shí)候公你,我們發(fā)現(xiàn) TextInputLayouthint文字顏色為深紅色,那么我們能不能改變這個(gè)顏色呢假瞬?

5.改變 Hint 樣式

打開 values\styles.xml 陕靠,找到以下代碼

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

就算找不到這行代碼也沒關(guān)系,我們可以手動(dòng)添加進(jìn)去脱茉,只要改變這個(gè) colorAccent的值剪芥,就可以將 整個(gè) 樣式改變?yōu)槲覀兿胍念伾恕?/p>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市琴许,隨后出現(xiàn)的幾起案子税肪,更是在濱河造成了極大的恐慌,老刑警劉巖榜田,帶你破解...
    沈念sama閱讀 222,627評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件益兄,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡箭券,警方通過查閱死者的電腦和手機(jī)净捅,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,180評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來辩块,“玉大人蛔六,你說我怎么就攤上這事荆永。” “怎么了国章?”我有些...
    開封第一講書人閱讀 169,346評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵具钥,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我捉腥,道長(zhǎng)氓拼,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,097評(píng)論 1 300
  • 正文 為了忘掉前任抵碟,我火速辦了婚禮桃漾,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘拟逮。我一直安慰自己撬统,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,100評(píng)論 6 398
  • 文/花漫 我一把揭開白布敦迄。 她就那樣靜靜地躺著恋追,像睡著了一般。 火紅的嫁衣襯著肌膚如雪罚屋。 梳的紋絲不亂的頭發(fā)上苦囱,一...
    開封第一講書人閱讀 52,696評(píng)論 1 312
  • 那天,我揣著相機(jī)與錄音脾猛,去河邊找鬼撕彤。 笑死,一個(gè)胖子當(dāng)著我的面吹牛猛拴,可吹牛的內(nèi)容都是我干的羹铅。 我是一名探鬼主播,決...
    沈念sama閱讀 41,165評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼愉昆,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼职员!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起跛溉,我...
    開封第一講書人閱讀 40,108評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤焊切,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后倒谷,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蛛蒙,經(jīng)...
    沈念sama閱讀 46,646評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,709評(píng)論 3 342
  • 正文 我和宋清朗相戀三年渤愁,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了牵祟。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,861評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡抖格,死狀恐怖诺苹,靈堂內(nèi)的尸體忽然破棺而出咕晋,到底是詐尸還是另有隱情,我是刑警寧澤收奔,帶...
    沈念sama閱讀 36,527評(píng)論 5 351
  • 正文 年R本政府宣布掌呜,位于F島的核電站,受9級(jí)特大地震影響坪哄,放射性物質(zhì)發(fā)生泄漏质蕉。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,196評(píng)論 3 336
  • 文/蒙蒙 一翩肌、第九天 我趴在偏房一處隱蔽的房頂上張望模暗。 院中可真熱鬧,春花似錦念祭、人聲如沸兑宇。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,698評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)隶糕。三九已至,卻和暖如春站玄,著一層夾襖步出監(jiān)牢的瞬間枚驻,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,804評(píng)論 1 274
  • 我被黑心中介騙來泰國(guó)打工株旷, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留测秸,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,287評(píng)論 3 379
  • 正文 我出身青樓灾常,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親铃拇。 傳聞我的和親對(duì)象是個(gè)殘疾皇子钞瀑,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,860評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容