Android5.0之材料设计各大组件

简介

 

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>



 

<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配合使用的效果最好!

 

2.FloatingActionButton(悬浮按钮)

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值