編輯:關於 Android 程式設計
TextInputLayout 是 Design Support Library 提供的一個輔助控制項,作用是讓 EditText 的提示文字在獲得焦點、開始輸入時「浮」到框線上方,形成 Material Design 的浮動標籤效果。它同時還能顯示錯誤訊息。
效果如下:

用法很直觀:用 TextInputLayout 包住 EditText,設定提示文字時改用 setHint(),並透過 getText() 之外的 getEditText() 取得內部的 EditText。
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import butterknife.Bind;
import butterknife.ButterKnife;
/**
* Created by yangle on 2016/7/28.
*/
public class TextInputLayoutActivity extends AppCompatActivity {
@Bind(R.id.layout_account) TextInputLayout layoutAccount;
@Bind(R.id.layout_password) TextInputLayout layoutPassword;
private EditText mAccount;
private EditText mPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_textinput_layout);
ButterKnife.bind(this);
initView();
}
private void initView() {
// 設定提示文字
layoutAccount.setHint("Username");
layoutPassword.setHint("Password");
// 透過 getEditText() 取得內部的 EditText
mAccount = layoutAccount.getEditText();
mPassword = layoutPassword.getEditText();
mAccount.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if (s.length() > 5) {
// 顯示錯誤提示
layoutAccount.setError("Username Error");
layoutAccount.setErrorEnabled(true);
} else {
layoutAccount.setErrorEnabled(false);
}
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// 點擊空白處時收起軟鍵盤
InputMethodManager inputMethodManager =
(InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(mAccount.getWindowToken(), 0);
inputMethodManager.hideSoftInputFromWindow(mPassword.getWindowToken(), 0);
return super.onTouchEvent(event);
}
}
對應的布局檔:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_E3E3E3"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="100dp"
android:text="Welcome"
android:textColor="@color/color_333333"
android:textSize="35sp" />
<TextInputLayout
android:id="@+id/layout_account"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</TextInputLayout>
<TextInputLayout
android:id="@+id/layout_password"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword" />
</TextInputLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:layout_marginRight="18dp"
android:layout_marginTop="20dp"
android:text="Login"
android:textSize="18sp" />
</LinearLayout>
提示文字顏色不受 textColorHint 控制。在 EditText 上直接設定 android:textColorHint 是沒有效果的,浮動標籤的顏色會跟著 Theme 走。要改只能改 Activity 的 Theme:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:textColorHint">@color/color_333333</item>
</style>
另外兩個實務提醒:一是 setErrorEnabled(true) 必須先呼叫,後續 setError 才看得到訊息,而且錯誤文字會佔用額外的高度,布局時要預留空間避免跳動;二是 TextInputLayout 必須正確包住 EditText 才會生效,兩者平級並不會有效果。
GitHub 範例專案:https://github.com/alidili/DesignSupportDemo
Android 常見內存洩漏場景大盤點與排查思路
內存洩漏是 Android 開發中反覆出現的難題。本文系統梳理幾類最常見的洩漏場景,並給出相應的规避與修復思路。集合類洩漏如果一個集合只有添加、沒有刪除機制,而它又是全局
用 Vitamio 打造萬能播放器(2):手勢控制亮度、音量與畫面縮放
前言 本章繼續補上播放器的核心能力,把亮度、音量、畫面縮放這三項手勢操作做完整,替後續的擴充打好地基。 系列 1、Android 使用 Vitamio 打造自己的萬能播放
VideoView 播放影片實戰:全螢幕、進度保持與黑屏問題一次解
忙完一段專案,趁空整理收藏夾,發現還有些東西值得分享。近期需求是加上影片播放能力。市面上第三方播放器無論是播放體驗還是功能完整度都遠勝原生方案,公司自家的 SDK 和 G
Android Glide 進階用法:高斯模糊、載入監聽與圓角圖片
高斯模糊、載入監聽、圓角圖片這些效果相信大家都耳熟能詳,那該怎麼用 Glide 實作呢?本文就帶大家一步步學會。1、引用依賴compile com.github.bump