TextInputLayout,讓輸入框更有靈性
介紹
Layout which wraps an {@link android.widget.EditText} (or descendant) to show a floating label
when the hint is hidden due to the user inputting text.Also supports showing an error via {@link #setErrorEnabled(boolean)} and
{@link #setError(CharSequence)}, and a character counter via
{@link #setCounterEnabled(boolean)}.翻譯:
TextInputLayout需要包裹一個EditText來實現當用戶輸入文字的時候,將hint作為一個浮動的標籤顯示的效果。使用比較多大的兩個方法:
setError(CharSequence) - > 使能錯誤訊息提示
對應屬性值:app:errorEnabled=”true”
setCounterEnabled(boolean) -> 使能字元長度顯示
對應屬性值:app:counterEnabled=”true”
類介紹
TextInputLayout的父類是LinearLayout,原始碼位置
frameworks/support/design/src/android/support/design/widget/TextInputLayout.java
類結構檢視
方法 | 意義 |
---|---|
setTypeface | 設定tf字型 |
getEditText | 獲取EditText檢視 |
setHint | 設定Hint內容 |
setHintEnabled | 使能hint |
setHintTextAppearance | 設定hint的Text Style |
setErrorEnabled | 使能錯誤提示 |
setError | 設定錯誤提示訊息 |
setCounterEnabled | 使能計數 |
setCounterMaxLength | 設定輸入框最大長度 |
setHintAnimationEnabled | 使能Hint浮動動畫,預設為true |
總體來看,用的比較多的就是錯誤訊息提示和計數功能,對字型的一些設定和TextView和EditText使用方法類似,這個佈局的特點就是視覺感受和使用者體驗比簡單的輸入框文字框提升很多。符合google的導向,但是很多apk中很少看到人使用,應該大家有更炫酷的方法。
具體使用
佈局檔案
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="mraz.com.tabdemo.MainActivity">
<android.support.design.widget.TextInputLayout
android:id="@+id/til_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:errorEnabled="true">
<android.support.design.widget.TextInputEditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="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"
app:counterEnabled="true"
app:counterMaxLength="40">
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password..." />
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/bt_showerror"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="ShowError"
android:textAllCaps="false" />
<Button
android:id="@+id/bt_clearerror"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="ClearError"
android:textAllCaps="false" />
</LinearLayout>
程式碼內容
MainActivity.java
package mraz.com.tabdemo;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextInputLayout userTextInputLayout = (TextInputLayout) findViewById(R.id.til_username);
TextInputLayout passTextInputLayout = (TextInputLayout) findViewById(R.id.til_password);
Button showErrorBtn = (Button) findViewById(R.id.bt_showerror);
showErrorBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
userTextInputLayout.setError("UserName is not correct!");
}
});
Button clearErrorBtn = (Button) findViewById(R.id.bt_clearerror);
clearErrorBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
userTextInputLayout.setError("");
}
});
}
}
程式碼上就不上註釋了,如果有疑問請提出來,不過程式碼很簡潔,應該問題不大。
實際效果