Android Support Library学习(一)

1.使用Android Support Library的时候,一般都会用Toolbar将ActionBar替换掉,具体做法如下:

<span style="font-size:18px;">Toolbar toolbar = (Toolbar) findViewById(R.id.id_register_toolbar);//绑定控件
        toolbar.setTitle("Title");//设置标题
        setSupportActionBar(toolbar);//替换ActionBar
        ActionBar actionBar = getSupportActionBar();//获取ActionBar
        actionBar.setDisplayHomeAsUpEnabled(true);//启用返回按钮</span>

注意在设置的时候,一定要写对方法名:setSupportAction getSupportAction,否则会出现空指针错误。

2.TextInputLayout控件

这是一个与EditText配套使用的控件,效果是将EditText包裹,可以显示提示信息和错误信息。使用方法如下:

<span style="font-size:18px;"><android.support.design.widget.TextInputLayout
                android:id="@+id/id_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </android.support.design.widget.TextInputLayout>

其中的EditText可以这样获得:textInputLayout.getEditText();

</span>

3.使用TextInputLayout的优势在于可以将信息显示在EditText附近,一般是对EditText进行监听,动态生成提示信息,这就用到了TextWatcher:

<span style="font-size:18px;">emailEditText.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        registerEmailContainer.setErrorEnabled(false);
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        registerEmailContainer.setErrorEnabled(false);

    }

    @Override
    public void afterTextChanged(Editable s) {
        boolean isValidate = REValidate.isEmail(s.toString());

        if (!isValidate) {


            registerEmailContainer.setError("格式不正确!(若未输入完毕,请继续输入)");
        } else {
            registerEmailContainer.setErrorEnabled(false);
        }
    }
});</span>

这里调用EditText的addTextChangedListener方法,参数为匿名内部类TextWatcher,
在afterTextWatcher方法中监听输入,判断输入的字符串是否为邮箱地址。



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值