一 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);
}
}