版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/q714093365/article/details/79277489
简介
在Google I/O 2015大会中,Google为Android开发者介绍了Design Support Library。这个library可以让开发者很容易地实现更多Material Design概念到他们的应用中,因为很多关键元素是不可用的在原来的框架外。首先就是很易于使用,Design Support Library向下兼容到API 7。Design Support Library可以引入到你的Android工程中通过导入Gradle依赖。
compile 'com.android.support:design:26.1.0'
1.TextInputLayout(带提示的输入框)
布局文件如下
<android.support.design.widget.TextInputLayout android:id="@+id/til_account" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="@dimen/pd_10">
<EditText android:id="@+id/et_account" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/form_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" android:layout_margin="@dimen/pd_10">
<android.support.design.widget.TextInputEditText android:id="@+id/tiet_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/form_password" android:inputType="textPassword"/>
</android.support.design.widget.TextInputLayout>
作者:GCZeng
链接:https://www.jianshu.com/p/de9c19d73450
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
<android.support.design.widget.TextInputLayout android:id="@+id/til_account" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="@dimen/pd_10">
<EditText android:id="@+id/et_account" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/form_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" android:layout_margin="@dimen/pd_10">
<android.support.design.widget.TextInputEditText android:id="@+id/tiet_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/form_password" android:inputType="textPassword"/>
</android.support.design.widget.TextInputLayout>
作者:GCZeng
链接:https://www.jianshu.com/p/de9c19d73450
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
<android.support.design.widget.TextInputLayout android:id="@+id/til_account" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="@dimen/pd_10">
<EditText android:id="@+id/et_account" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/form_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" android:layout_margin="@dimen/pd_10">
<android.support.design.widget.TextInputEditText android:id="@+id/tiet_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/form_password" android:inputType="textPassword"/>
</android.support.design.widget.TextInputLayout>
作者:GCZeng
链接:https://www.jianshu.com/p/de9c19d73450
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
<android.support.design.widget.TextInputLayout android:id="@+id/til_account" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="@dimen/pd_10">
<EditText android:id="@+id/et_account" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/form_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" android:layout_margin="@dimen/pd_10">
<android.support.design.widget.TextInputEditText android:id="@+id/tiet_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/form_password" android:inputType="textPassword"/>
</android.support.design.widget.TextInputLayout>
作者:GCZeng
链接:https://www.jianshu.com/p/de9c19d73450
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
<android.support.design.widget.TextInputLayout android:id="@+id/til_account" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="@dimen/pd_10">
<EditText android:id="@+id/et_account" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/form_username"/>
</android.support.design.widget.TextInputLayout>
作者:GCZeng
链接:https://www.jianshu.com/p/de9c19d73450
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
<
android.support.design.widget.TextInputLayout
android:id=
"@+id/til_account"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:layout_margin=
"@dimen/pd_10">
作者:GCZeng
链接:https://www.jianshu.com/p/de9c19d73450
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处
<android.support.design.widget.TextInputLayout
android:id="@+id/til_account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<EditText
android:id="@+id/et_account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名" />
</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"
android:layout_margin="10dp">
<android.support.design.widget.TextInputEditText
android:id="@+id/tiet_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码"
android:inputType="textPassword" />
</android.support.design.widget.TextInputLayout>
TextInputLayout错误提示如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_input_layout);
Button button = findViewById(R.id.btn_login);
final TextInputLayout textInputLayout = findViewById(R.id.til_account);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showError(textInputLayout, "用户名错误");
}
});
}
/**
* 显示错误提示,并获取焦点 * @param textInputLayout * @param error
*/
private void showError(TextInputLayout textInputLayout, String error) {
textInputLayout.setError(error);
textInputLayout.getEditText().setFocusable(true);
textInputLayout.getEditText().setFocusableInTouchMode(true);
textInputLayout.getEditText().requestFocus();
}
TextInputEditText
上面的例子中,你会看到用户输入控件使用的是的EditText,而密码输入控件则使用了TextInputEditText,这里是为了对比一下两者的区别。还是上面的例子,我们把手机设置为横向,再看一下效果:
可以看到输入的时候都变成了全屏模式,用户名使用EidtText的时候hint就隐藏了,而密码使用TextInputEditText的时候hint可以正常显示。
由此可见TextInputEditText的设计就是修复了这个缺陷,所以TextInputLayout和TextInputEditText配合使用的效果最好!