Android之登陆样式(仿知乎)
2016-04-15 16:37
691 查看
好长时间没有上知乎了,今天打开的时候,发现提示令牌失效了,让重新登陆,一看这个效果,其实很好实现的,在Material Design里面其实有相关的view的,为TextInputLayout,这里简单写个demo:
接下来,我们就在Activity里面去实现相关的处理
最终的效果如下:
xml布局文件 <?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" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".MainActivity" android:orientation="vertical" tools:showIn="@layout/activity_main"> <android.support.design.widget.TextInputLayout android:id="@+id/til_id" 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> <android.support.design.widget.TextInputLayout android:id="@+id/til_id2" 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> </LinearLayout>
接下来,我们就在Activity里面去实现相关的处理
public class MainActivity extends AppCompatActivity { private EditText editText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final TextInputLayout textInputLayout1 = (TextInputLayout) findViewById(R.id.til_id); final TextInputLayout textInputLayout2 = (TextInputLayout) findViewById(R.id.til_id2); generateEdit(textInputLayout1, "用户名", "用户名输入不要超过5位",5); generateEdit(textInputLayout2,"密码","密码长度应小于10位",10); } private void generateEdit(final TextInputLayout text,String textinput, final String texterror, final int length) { editText = text.getEditText(); text.setHint(textinput); editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { if (s.length() > length) { text.setError(texterror); text.setErrorEnabled(true); } else { text.setErrorEnabled(false); } } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { } }); } }
最终的效果如下:
相关文章推荐
- Android 基本控件使用六(ListView+SimpleAdapter)
- android selector 和 layer-list复合使用
- Android N For Developers 笔记(二)
- Android之本地相册图片选取和拍照以及图片剪辑
- android TextView设置自定义字体
- Android 事件传递
- Android异步消息处理机制详解及源码分析
- Android应用之间的跳转
- [译]Android冰淇淋三明治ICS(4.0+)JNI局部引用的变化
- Android studio编译OOM
- android 基础知识-API
- android那些事系列之android打开各类文件
- Android平台根目录文件
- Mediaplayer State Diagram
- 关于设计模式的一点总结
- android 基础知识-开发代号解释
- Android删除,查找R,输出级别,补充知识,主要组件
- Apktool—A tool for reverse engineering Android apk files
- AndroidStudio
- Android:在工程任意位置获取应用程序的Context