SeniorUI26_Snackbar使用及其源码分析

SeniorUI_高级UI汇总目录

一 Snackbar的简单使用

  • Snackbar是介于Toast和Dialog之前的提示框,既能像Toast一样短暂提示也能像Dialog长久显示。
  • Snackbar本质上是通过addView显示的,当布局有CoordinatorLayout时,添加到CoordinatorLayout;没有则添加到对应id为android.R.id.content的FrameLayout

1 短提示

Snackbar.make(button, "点击了button", Snackbar.LENGTH_INDEFINITE)

2 一直显示

当设置为Snackbar.LENGTH_INDEFINITE时,只有实现Action点击,Snackbar才消失

Snackbar snackbar = Snackbar.make(button, "点击了button", Snackbar.LENGTH_INDEFINITE)
                .setAction("确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(SnackeBarActivity.this,"确定了 ",Toast.LENGTH_LONG).show();
                    }
                }).setCallback(new Snackbar.Callback(){
                    @Override
                    public void onShown(Snackbar sb) {
                        super.onShown(sb);
                        time=System.currentTimeMillis();
                    }

                    @Override
                    public void onDismissed(Snackbar transientBottomBar, int event) {
                        time=System.currentTimeMillis()-time;
                        Toast.makeText(SnackeBarActivity.this,"  时间" +time,Toast.LENGTH_SHORT).show();
                        super.onDismissed(transientBottomBar, event);
                    }
                }).setActionTextColor(Color.YELLOW);
        View view1=snackbar.getView();
        TextView textView= (TextView) view1.findViewById(R.id.snackbar_text);
        textView.setText("内容");
        snackbar.show();

在这里插入图片描述

二 Snackbar源码分析

Snackbar分析分部分:Snackbar.make() 和Snackbar.show()
在这里插入图片描述

三 TextInputLayout简单使用

TextInputLayout一般和EditText配合使用
在这里插入图片描述

private void initView(){
		editText = (EditText) findViewById(R.id.et_name);
        textInputLayout = (TextInputLayout) findViewById(R.id.tliayout);
        textInputLayout.setErrorEnabled(true);
        //设置错误信息是否显示。true显示,false不显示。
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                checkName(s.toString());
            }
        });
}

 private void checkName(String s) {
        if (s.length() < 4) {
            textInputLayout.setError("名字长度太短");
        }else {
            textInputLayout.setError(null);
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Android Studio中使用Snackbar点击按钮带图标方式显示消息提示,需要遵循以下步骤: 1.在app/build.gradle文件中添加以下依赖项: ``` implementation 'com.android.support:design:28.0.0' ``` 这将添加支持Snackbar的Material Design库。 2.在布局文件中添加一个Button和一个Snackbar容器View: ``` <RelativeLayout android:id="@+id/main_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/my_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show Snackbar"/> <LinearLayout android:id="@+id/snackbar_container" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true"/> </RelativeLayout> ``` 3.在Activity中获取Button和Snackbar容器View的引用: ``` Button myButton = findViewById(R.id.my_button); View snackbarContainer = findViewById(R.id.snackbar_container); ``` 4.在Button的onClick事件中,创建Snackbar实例并设置消息内容和图标: ``` myButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar snackbar = Snackbar.make(snackbarContainer, "This is a Snackbar message", Snackbar.LENGTH_LONG); //设置Snackbar中的图标 Drawable icon = getResources().getDrawable(R.drawable.ic_info_outline); icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight()); TextView textView = snackbar.getView().findViewById(android.support.design.R.id.snackbar_text); textView.setCompoundDrawables(icon, null, null, null); textView.setCompoundDrawablePadding(getResources().getDimensionPixelOffset(R.dimen.snackbar_icon_padding)); snackbar.show(); } }); ``` 这将创建一个Snackbar实例,将其附加到Snackbar容器View中,并在Snackbar消息中添加一个带有指定图标的TextView。 现在,当用户点击按钮时,Snackbar将显示在屏幕底部,并显示指定的消息和图标。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值