Android Design Support Library常用控件介绍(上)

Android Design Support Library给我们提供了一些既方便操作又非常实用还非常酷炫的控件。让我们来简单地体验下吧!先介绍下FloatingActionButton,Snackbar及TextInputLayout。

  1. 首先介绍下FloatingActionButton(浮动的圆形按钮)和Snackbar(:可交互的提示框)。
<?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" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" >

    <android.support.design.widget.FloatingActionButton  android:id="@+id/float_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top|left" android:layout_margin="@dimen/fab_margin" app:fabSize="normal" app:elevation="8dp" android:src="@android:drawable/ic_dialog_email" />

</LinearLayout>
public class MainActivity extends Activity {
    //FloatingActionButton:浮动的圆形按钮
    private FloatingActionButton float_btn;
    //Snackbar:可交互的提示框,功能相当于Toast,但可以操作
    private Snackbar snackbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        float_btn = (FloatingActionButton) findViewById(R.id.float_btn);
        float_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                snackbar = Snackbar.make(view, "这是个可交互的提示框!", Snackbar.LENGTH_LONG);
                snackbar.show();
                snackbar.setAction("确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        snackbar.dismiss();
                    }
                });
            }
        });
    }

}
```效果如图
![这里写图片描述](http://img.blog.csdn.net/20160127135455731)
 2. 接下来介绍下TextInputLayout(体验效果更好的EditText)
布局文件




<div class="se-preview-section-delimiter"></div> 

public class MainActivity extends Activity {
    //更加人性化的EditText
    private TextInputLayout text_input;
    private EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text_input=(TextInputLayout)findViewById(R.id.text_input);
        text_input.setHint("请输入你的手机号码!");//提示文字
        editText=text_input.getEditText();//获取到里面的EditText
        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) {
          if(s.length()!=11){
                    text_input.setError("您输入的手机号码不正确");//错误提示
                    text_input.setErrorEnabled(true);
                }else{
                    text_input.setErrorEnabled(false);
                }
            }
        });
    }

}

显示的效果图:
Android Design Support Library常用控件介绍(上)_第1张图片

你可能感兴趣的:(Android Design Support Library常用控件介绍(上))