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方法中监听输入,判断输入的字符串是否为邮箱地址。
289

被折叠的 条评论
为什么被折叠?



