Android 中 TextinputLayout 的用法

textinputlayout_pic.png

Layout which wraps an EditText (or descendant) to show a floating label when the hint is hidden due to the user inputting text.

TextinputLayout 在內(nèi)部嵌套一個(gè) EditText ,并展示一個(gè)浮動(dòng)的標(biāo)簽,標(biāo)簽會(huì)在用戶輸入的時(shí)候移動(dòng)到 EditText 上方。

我在這里簡單的記錄一下 TextInputLayout 的用法


添加依賴

dependencies{
    compile ‘com.android.support:appcompat-v7:25.0.1’
    compile ‘com.android.support:design:25.0.1'
}

設(shè)置相關(guān)屬性

  • 開啟浮動(dòng)標(biāo)簽
    在 TextinputLayout 中加入 hint 屬性
  • 關(guān)閉浮動(dòng)標(biāo)簽
    app:hintEnable=“true | false”
  • 浮動(dòng)標(biāo)簽動(dòng)畫
    app:hintAnimationEnable=“true | false”
  • 自定義浮動(dòng)標(biāo)簽
 <style name=“hintAppearence” parent=“TextAppearance.AppCompat”>
     <item name=“android:textSize”>14sp</item>
     <item name=“android:textColor”>#00ff00</item>
 </style>
在 TextinputLayout 中加入 app:hintAppearance=“@style/hintAppearence"
  • 在 EditText 下顯示字?jǐn)?shù)統(tǒng)計(jì)
    app:counterEnable=“true”
    app:counterMaxLength=“11”. //超過 11 位后換顏色
  • 顯示密碼:
    app:passwordToggleEnable=“true”
    換圖標(biāo):app:passwordToggleDrawable=“@mipmap/ic_launcher”
    加顏色:app:passwordToggleTint=“@color/colorBlue”
    設(shè)置模式:app:passwordToggleMode=“screen | src_in | src_atop | src_over | multiply”

設(shè)置錯(cuò)誤提示

setError: 設(shè)置錯(cuò)誤提示信息
setErrorEnable: 開啟錯(cuò)誤提示功能
直接調(diào)用 setError,會(huì)自動(dòng)調(diào)用 setErrorEnable
自定義錯(cuò)誤提示樣式

<style name=“errorAppearance” parent=“TextAppearance.AppCompat” >
    <item name=“android:textSize”>14sp</item>
    <item name=“android:textColor”>@color/colorBlue</item>
</style>

在 TextinputLayout 中加入自定義的錯(cuò)誤提示樣式

app:errorTextAppearance=“@style/errorAppearance"

Java 代碼實(shí)現(xiàn):

public void onClick(View v){
    String username = usernameTextinputlayout.getEditText().getText().toString();
    String password = passwordTextinputLayout.getEditText().getText().toString();
    If(username == “”){
        usernameTextinputLayout.setError(“用戶名不能為空”);
    }else if(password == “”){
        passwordTextinputLayout.setError(“密碼不能為空”);
    }else{
        usernameTextinputLayout.setErrorEnable(false);
        passwordTextinputLayout.setErrorEnable(false);
    }
}

修改樣式

  • 默認(rèn)情況下 TextinputLaylout 控件的顏色是 當(dāng)前 Style 下 colorAccent 設(shè)置的,所以修改 style 下 colorAccent 的值即可.
  • 修改光標(biāo)樣式 :
    隱藏光標(biāo) : android:cursorVisible.
    光標(biāo)樣式 : android:textCursorDrawable. 為空時(shí),光標(biāo)顏色和文字顏色一致
    在 drawable 下創(chuàng)建 cursor_color.xml
<shape android:shape=“rectangle">
    <size android:width=“1dp”/>
    <solid android:color=“@color/colorBlue"/>
</shape>

隱藏下劃線 : android:background=“@null"

參考:
https://segmentfault.com/a/1190000009507919r

歡迎關(guān)注我的博客簡書、CSDN、GitHub

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,039評(píng)論 25 709
  • 內(nèi)容抽屜菜單ListViewWebViewSwitchButton按鈕點(diǎn)贊按鈕進(jìn)度條TabLayout圖標(biāo)下拉刷新...
    皇小弟閱讀 47,161評(píng)論 22 665
  • 徒步 近幾年由于人民生活質(zhì)量的提高,越來越多的人更注重健康,于是官辦、民辦的紛紛成立健身團(tuán)體,舉辦各種形式的健身...
    淡然2017閱讀 230評(píng)論 0 0
  • By 鏡天。 我仿佛是從水中而來 皮膚由于長時(shí)...
    鏡天閱讀 322評(píng)論 0 3
  • 你終究是要奔向遠(yuǎn)方的,偶然路過我這小小的酒肆,小憩一下,便要再次風(fēng)雨兼程。 山高水長,愿有人陪你顛沛流離。 我這間...
    紫翼i閱讀 1,244評(píng)論 0 1

友情鏈接更多精彩內(nèi)容