Android原生控件之--TextInputLayout研底、TextInputEditText

TextInputLayout是22.2.0新添加的控件焕数, 要和EditText(或EditText的子類)結(jié)合使用驴党,并且只能包含一個(gè)EditText(或EditText的子類)辆它。

TextInputLayout繼承關(guān)系如下:


java.lang.Object
   ?    android.view.View
       ?    android.view.ViewGroup
           ?    android.widget.LinearLayout
               ?    android.support.design.widget.TextInputLayout

TextInputLayout基本用法

  1. 首先要引入design和appcompat-v7兼容包:
compile 'com.android.support:design:25.2.0'
compile 'com.android.support:appcompat-v7:25.2.0'
  1. 在布局文件添加如下代碼
<android.support.design.widget.TextInputLayout
    android:id="@+id/til_account"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/pd_10">

    <EditText
        android:id="@+id/et_account"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/form_username"/>

</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
    android:id="@+id/til_password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/pd_10">

    <android.support.design.widget.TextInputEditText
        android:id="@+id/tiet_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/form_password"
        android:inputType="textPassword"/>

</android.support.design.widget.TextInputLayout>

一般情況下誊薄,EditText獲得光標(biāo)的時(shí)候hint會(huì)自動(dòng)隱藏,這樣不是很友好娩井。這時(shí)候TextInputLayout就派上用場(chǎng)了暇屋,TextInputLayoutLinearLayout的子類,用于輔助顯示提示信息洞辣。當(dāng)EditText獲取得光標(biāo)的時(shí)候咐刨,EditText的hint會(huì)自己顯示在上方,并且有動(dòng)畫(huà)過(guò)渡扬霜。

2017-04-13 15_22_21.gif

TextInputLayout錯(cuò)誤提示

TextInputLayout還可以處理錯(cuò)誤并給出相應(yīng)提示定鸟,比如在上面的其他上我們添加一個(gè)登錄按鈕,點(diǎn)擊登錄按鈕的時(shí)候要驗(yàn)證密碼長(zhǎng)度為6-18個(gè)字符著瓶。

  1. 首先在布局上添加一個(gè)登錄按鈕:
    <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/pd_10"
        android:layout_margin="@dimen/pd_10"
        android:text="@string/login"/>
  1. 添加一個(gè)顯示錯(cuò)誤提示并獲取焦點(diǎn)的方法:
    /**
     * 顯示錯(cuò)誤提示联予,并獲取焦點(diǎn)
     * @param textInputLayout
     * @param error
     */
    private void showError(TextInputLayout textInputLayout,String error){
        textInputLayout.setError(error);
        textInputLayout.getEditText().setFocusable(true);
        textInputLayout.getEditText().setFocusableInTouchMode(true);
        textInputLayout.getEditText().requestFocus();
    }
  1. 添加驗(yàn)證用戶名和密碼方法:
    /**
     * 驗(yàn)證用戶名
     * @param account
     * @return
     */
    private boolean validateAccount(String account){
        if(StringUtils.isEmpty(account)){
            showError(til_account,"用戶名不能為空");
            return false;
        }
        return true;
    }

    /**
     * 驗(yàn)證密碼
     * @param password
     * @return
     */
    private boolean validatePassword(String password) {
        if (StringUtils.isEmpty(password)) {
            showError(til_password,"密碼不能為空");
            return false;
        }

        if (password.length() < 6 || password.length() > 18) {
            showError(til_password,"密碼長(zhǎng)度為6-18位");
            return false;
        }

        return true;
    }
  1. 給登錄按鈕設(shè)置點(diǎn)擊事件,在觸發(fā)點(diǎn)擊事件的時(shí)候獲取用戶名和密碼材原,并驗(yàn)證用戶名和密碼格式:
private Button btn_login;
private TextInputLayout til_account;
private TextInputLayout til_password;


btn_login = (Button) findViewById(R.id.btn_login);
btn_login.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String account = til_account.getEditText().getText().toString();
        String password = til_password.getEditText().getText().toString();

        til_account.setErrorEnabled(false);
        til_password.setErrorEnabled(false);

        //驗(yàn)證用戶名和密碼
        if(validateAccount(account)&&validatePassword(password)){
            Toast.makeText(TextInputLayoutActivity.this,"登錄成功",Toast.LENGTH_LONG).show();
        }
    }
});

這個(gè)示例簡(jiǎn)單判斷了用戶名非空和密碼非空和長(zhǎng)度判斷沸久,并給出相應(yīng)提示。


2017-04-13 17_02_26.gif

TextInputEditText

TextInputEditText繼承關(guān)系如下:

java.lang.Object
   ?    android.view.View
       ?    android.widget.TextView
           ?    android.widget.EditText
               ?    android.support.v7.widget.AppCompatEditText
                   ?    android.support.design.widget.TextInputEditText

由繼承關(guān)系可以看出TextInputEditTextEditText的一個(gè)子類余蟹。上面的例子中卷胯,你會(huì)看到用戶輸入控件使用的是的EditText,而密碼輸入控件則使用了TextInputEditText威酒,這里是為了對(duì)比一下兩者的區(qū)別窑睁。

官方文檔是這樣描述的:

A special sub-class of EditText
designed for use as a child of TextInputLayout.
Using this class allows us to display a hint in the IME when in 'extract' mode.

大概意思為:TextInputEditText作為EditText的子類,為TextInputLayout設(shè)計(jì)的一個(gè)子容器葵孤。輸入法在'extract'模式的時(shí)候担钮,使用TextInputEditText類允許顯示提示。

還是上面的例子尤仍,我們把手機(jī)設(shè)置為橫向箫津,再看一下效果:

2017-04-13 20_31_51.gif

可以看到輸入的時(shí)候都變成了全屏模式,用戶名使用EidtText的時(shí)候hint就隱藏了宰啦,而密碼使用TextInputEditText的時(shí)候hint可以正常顯示鲤嫡。

由此可見(jiàn)TextInputEditText的設(shè)計(jì)就是修復(fù)了這個(gè)缺陷,所以TextInputLayoutTextInputEditText配合使用的效果最好!

示例托管在:GitHub

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末绑莺,一起剝皮案震驚了整個(gè)濱河市暖眼,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌纺裁,老刑警劉巖诫肠,帶你破解...
    沈念sama閱讀 222,590評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件司澎,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡栋豫,警方通過(guò)查閱死者的電腦和手機(jī)挤安,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,157評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)丧鸯,“玉大人蛤铜,你說(shuō)我怎么就攤上這事〈灾” “怎么了围肥?”我有些...
    開(kāi)封第一講書(shū)人閱讀 169,301評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)蜂怎。 經(jīng)常有香客問(wèn)我穆刻,道長(zhǎng),這世上最難降的妖魔是什么杠步? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 60,078評(píng)論 1 300
  • 正文 為了忘掉前任氢伟,我火速辦了婚禮,結(jié)果婚禮上幽歼,老公的妹妹穿的比我還像新娘朵锣。我一直安慰自己,他們只是感情好甸私,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,082評(píng)論 6 398
  • 文/花漫 我一把揭開(kāi)白布诚些。 她就那樣靜靜地躺著,像睡著了一般颠蕴。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上助析,一...
    開(kāi)封第一講書(shū)人閱讀 52,682評(píng)論 1 312
  • 那天犀被,我揣著相機(jī)與錄音,去河邊找鬼外冀。 笑死寡键,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的雪隧。 我是一名探鬼主播西轩,決...
    沈念sama閱讀 41,155評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼脑沿!你這毒婦竟也來(lái)了藕畔?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 40,098評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤庄拇,失蹤者是張志新(化名)和其女友劉穎注服,沒(méi)想到半個(gè)月后韭邓,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,638評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡溶弟,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,701評(píng)論 3 342
  • 正文 我和宋清朗相戀三年女淑,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片辜御。...
    茶點(diǎn)故事閱讀 40,852評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡鸭你,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出擒权,到底是詐尸還是另有隱情袱巨,我是刑警寧澤,帶...
    沈念sama閱讀 36,520評(píng)論 5 351
  • 正文 年R本政府宣布菜拓,位于F島的核電站瓣窄,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏纳鼎。R本人自食惡果不足惜俺夕,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,181評(píng)論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望贱鄙。 院中可真熱鬧劝贸,春花似錦、人聲如沸逗宁。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,674評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)瞎颗。三九已至件甥,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間哼拔,已是汗流浹背引有。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,788評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留倦逐,地道東北人譬正。 一個(gè)月前我還...
    沈念sama閱讀 49,279評(píng)論 3 379
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像檬姥,于是被迫代替她去往敵國(guó)和親曾我。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,851評(píng)論 2 361

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