本文轉(zhuǎn)載自:http://blog.csdn.net/android_freshman/article/details/51136657
版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請(qǐng)注明出處斧账。
繼CoordinatorLayout之后谴返,繼續(xù)研究 material design 的相關(guān)控件TextInputLayout,下面是效果圖:
1.gradle 配置
compile ‘com.Android.support:design:22.2.0’
compile ‘com.android.support:appcompat-v7:22.2.0’
2.xml
privateandroid.widget.LinearLayout.LayoutParamssetEditText(EditText editText,? LayoutParams lp) {if(this.mEditText !=null) {thrownewIllegalArgumentException("We already have an EditText, can only? ? have one");? ? ? ? }else{? ? ? ? ? ? .......? ? ? ? ? ? .....
1
2
3
4
5
6
7
1
2
3
4
5
6
7
注意點(diǎn):部分源代碼中的內(nèi)容 咧织,TextInputLayout 繼承LinearLayout 且里面只能有一個(gè)editEditText嗓袱,和scrollView 很像。下面是布局文件:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
3.java
注意:不能 重寫 TextInputLayout的OnFocusChangeListener的監(jiān)聽事件习绢,因?yàn)樵谠创a中定義了動(dòng)畫效果和editText注入渠抹,重寫了會(huì)導(dǎo)致動(dòng)畫失效。
設(shè)置 浮動(dòng)標(biāo)簽動(dòng)畫效果
titleTextInput.setHint(“Title”);
if(titleEditText.getText().toString().length()<6){? titleTextInput.setErrorEnabled(true);? titleTextInput.setError("title length must >= 6");}else{? titleTextInput.setErrorEnabled(false);}
1
2
3
4
5
6
1
2
3
4
5
6
這一部分是動(dòng)態(tài)錯(cuò)誤提示的相關(guān)代碼
完成上面的闪萄,基本就可以出現(xiàn)TextInputLayout 的動(dòng)畫效果了梧却,但是默認(rèn)的顏色不是很好看,所以我們需要自定義相關(guān)的顏色败去,比如 hint 字的顏色放航,下劃線的顏色,錯(cuò)誤字體的顏色大小等圆裕,下面就是自定義顏色的部分:
谷歌把Design Support Library寫的很好广鳍。每一個(gè)控件的顏色都是直接通過主題顏色繪制的缺菌,在 style.xml 中指定。打開它添加colorAccent 到主題以改變表單的顏色搜锰。在 style.xml 中修改相關(guān)的屬性
? ? ? ? @color/primary? ? ? ? @color/primary_dark? ? ? ? #3498db-->? ? ? ? @color/alpha_white? ? ? ? @color/alpha_white? ? ? ? @color/white? ? ? ? @color/white? ? ? ? true? ?
1
2
3
4
5
6
7
8
9
10
11
1
2
3
4
5
6
7
8
9
10
11
2.1 colorAccent 是什么意思伴郁,哪里的顏色
這張圖片基本說明了colorAccent 代表的顏色,而在google 的官網(wǎng)上:
https://www.google.com/design/spec/style/color.html#color-color-schemes也有相關(guān)的說明
2.2 其他相關(guān)顏色的說明
android:textColorHint 代表 hint 的顏色
colorControlNormal 代表 下劃線沒有獲取焦點(diǎn)的顏色
colorControlActivated蛋叼,colorControlHighlight 代表了獲取焦點(diǎn)或者點(diǎn)擊的時(shí)候 下劃線 的顏色
2.3 錯(cuò)誤提示的顏色說明:
默認(rèn)的錯(cuò)誤提示的顏色是紅色:在這種背景色下面焊傅,紅色不是很好看,所以需要自定義顏色
在設(shè)置布局的時(shí)候errorTextAppearance這個(gè)屬性狈涮,自定義style 寫顏色和大小就可以了狐胎,至于另一個(gè)屬性hintTextAppearance 這個(gè)屬性修改顏色,好像沒有什么效果歌馍,不起作用握巢。
修改之后的效果,如下圖:有的機(jī)器上面可能沒有效果松却,下面提供一種解決方案:
publicstaticvoidsetErrorTextColor(TextInputLayout textInputLayout,intcolor) {try{? ? ? ? ? ? Field fErrorView = TextInputLayout.class.getDeclaredField("mErrorView");? ? ? ? ? ? fErrorView.setAccessible(true);? ? ? ? ? ? TextView mErrorView = (TextView) fErrorView.get(textInputLayout);? ? ? ? ? ? Field fCurTextColor = TextView.class.getDeclaredField("mCurTextColor");? ? ? ? ? ? fCurTextColor.setAccessible(true);? ? ? ? ? ? fCurTextColor.set(mErrorView, color);? ? ? ? }catch(Exception e) {? ? ? ? ? ? e.printStackTrace();? ? ? ? }? ? }
1
2
3
4
5
6
7
8
9
10
11
12
1
2
3
4
5
6
7
8
9
10
11
12
需要注意的是:
setErrorTextColor(titleTextInput,getResources().getColor(R.color.grey));
需要在titleTextInput.setErrorEnabled(true);之前調(diào)用
這個(gè)是 material design 的說明文檔暴浦,收藏記錄下:
https://www.google.com/design/spec/components/text-fields.html#text-fields-multi-line-text-field