android新特性:TextInputLayout使用方法

  • 效果如下:
20170207094559319.png
20170207094616679.png

TextInputLayout 控件表現(xiàn)得就像LinearLayout 一樣氨肌,它是一個容器汞扎。TextInputLayout 中只能放一個子元素国瓮,和ScrollView有點(diǎn)類似茂蚓,并且子元素必須是EditText押赊;

TextInputLayout 實(shí)現(xiàn)的功能就是當(dāng)EditText中輸入第一個字母要隱藏hint的時候捞挥,TextInputLayout中會出現(xiàn)一個懸浮的標(biāo)簽來顯示這個hint兼蕊,還負(fù)責(zé)一個炫酷的的material 動畫

  • 首先是怎樣引入TextInputLayout
   compile 'com.android.support:design:25.1.0'
  • 布局如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@mipmap/login_bg"
    tools:context="cn.hnshangyu.textinputlayout.MainActivity">

    <android.support.design.widget.TextInputLayout
        app:hintTextAppearance="@style/FloatingStyle"
        android:id="@+id/username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true">

        <EditText
            android:layout_width="260dp"
            android:layout_height="wrap_content"
            android:inputType="phone"
            android:maxLines="1"
            android:padding="@dimen/padding_10"
            android:textColor="@color/white70"
            android:textColorHint="@color/white70" />
    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_below="@id/username"
        android:layout_marginTop="@dimen/padding_20">
        <EditText
            android:layout_width="260dp"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:maxLines="1"
            android:padding="@dimen/padding_10"
            android:textColor="@color/white70"
            android:textColorHint="@color/white70" />
    </android.support.design.widget.TextInputLayout>

</RelativeLayout>

在這里可以看到 app:hintTextAppearance="@style/FloatingStyle"這個屬性果正,這個屬性是干嘛的呢秦躯?

其實(shí)當(dāng)在TextInputLayout中設(shè)置這個屬性后忆谓,就會讓hint字體的內(nèi)容固定在EditText上方不會出現(xiàn)動畫效果,同時hint字體顏色變化在FloatingStyle中設(shè)置踱承!如效果圖中的手機(jī)號輸入框...

  • FloatingStyle的內(nèi)容:
 <style name="FloatingStyle" parent="@android:style/TextAppearance">
        <item name="android:textColor">#e0c9c4c4</item>
        <item name="android:textSize">12sp</item>
    </style>
  • 在MainActivity中對TextInputLayout進(jìn)行設(shè)置
package cn.hnshangyu.textinputlayout;

import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {


    @Bind(R.id.username)
    TextInputLayout username;

    @Bind(R.id.password)
    TextInputLayout password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        username.setHint(getResources().getString(R.string.phone_num));
        password.setHint(getResources().getString(R.string.input_password));
        username.setError(getResources().getString(R.string.phone_num_error));
    }
}

TextInputLayout既可以在輸入時把提示語顯示與EditText的上方倡缠,又能把錯誤信息提示顯示在EditText的下方。

TextInputLayout需要注意的幾點(diǎn):

  • TextInputLayout不能單獨(dú)使用茎活,必須和EditText嵌套使用昙沦。

  • 1個TextInputLayout只能包1個EditTExt。

再來看看比較常用的方法

1载荔、setHint();設(shè)置提示語

2盾饮、setError();設(shè)置錯誤顯示信息

3、setErrorEnabled();設(shè)置錯誤信息是否顯示懒熙。true顯示丘损,false不顯示。

4工扎、getEditText();得到EditText的控件實(shí)例徘钥。

注意:

不用設(shè)置setErrorEnabled()為true也是可以的。因?yàn)檫@里會調(diào)用setErrorEnabled(true)定庵。 
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末吏饿,一起剝皮案震驚了整個濱河市踪危,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌猪落,老刑警劉巖贞远,帶你破解...
    沈念sama閱讀 219,366評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異笨忌,居然都是意外死亡蓝仲,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,521評論 3 395
  • 文/潘曉璐 我一進(jìn)店門官疲,熙熙樓的掌柜王于貴愁眉苦臉地迎上來袱结,“玉大人,你說我怎么就攤上這事途凫」讣校” “怎么了?”我有些...
    開封第一講書人閱讀 165,689評論 0 356
  • 文/不壞的土叔 我叫張陵维费,是天一觀的道長果元。 經(jīng)常有香客問我,道長犀盟,這世上最難降的妖魔是什么而晒? 我笑而不...
    開封第一講書人閱讀 58,925評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮阅畴,結(jié)果婚禮上倡怎,老公的妹妹穿的比我還像新娘。我一直安慰自己贱枣,他們只是感情好监署,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,942評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著冯事,像睡著了一般焦匈。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上昵仅,一...
    開封第一講書人閱讀 51,727評論 1 305
  • 那天缓熟,我揣著相機(jī)與錄音,去河邊找鬼摔笤。 笑死够滑,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的吕世。 我是一名探鬼主播彰触,決...
    沈念sama閱讀 40,447評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼命辖!你這毒婦竟也來了况毅?” 一聲冷哼從身側(cè)響起分蓖,我...
    開封第一講書人閱讀 39,349評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎尔许,沒想到半個月后么鹤,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,820評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡味廊,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,990評論 3 337
  • 正文 我和宋清朗相戀三年蒸甜,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片余佛。...
    茶點(diǎn)故事閱讀 40,127評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡柠新,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出辉巡,到底是詐尸還是另有隱情恨憎,我是刑警寧澤,帶...
    沈念sama閱讀 35,812評論 5 346
  • 正文 年R本政府宣布红氯,位于F島的核電站框咙,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏痢甘。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,471評論 3 331
  • 文/蒙蒙 一茉贡、第九天 我趴在偏房一處隱蔽的房頂上張望塞栅。 院中可真熱鬧,春花似錦腔丧、人聲如沸放椰。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,017評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽砾医。三九已至,卻和暖如春衣厘,著一層夾襖步出監(jiān)牢的瞬間如蚜,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,142評論 1 272
  • 我被黑心中介騙來泰國打工影暴, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留错邦,地道東北人。 一個月前我還...
    沈念sama閱讀 48,388評論 3 373
  • 正文 我出身青樓型宙,卻偏偏與公主長得像撬呢,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子妆兑,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,066評論 2 355

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