使用TextInputLayout
来源:互联网 发布:mysql完全手册 编辑:程序博客网 时间:2024/06/03 05:09
Design Support Library是在Google I/O2015上发布的一个全新兼容函数库,主要包括:
- Snackbar
- TextInputLayout
- TabLayout
- FloatingActionButton
- Navigation View
CoordinatorLayout
首先看下使用TextInputLayout的效果
- 加入依赖
compile 'com.android.support:design:26.0.0-alpha1'
- 使用TextInputLayout包含一个EditTextView控件
<android.support.design.widget.TextInputLayout android:id="@+id/un_input_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="40dp" app:errorEnabled="true" app:errorTextAppearance="@style/TextAppearance.Design.Error"> <EditText android:id="@+id/et_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入用户名" android:textSize="14sp" /> </android.support.design.widget.TextInputLayout>
- 完整的布局文件代码
<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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:padding="10dp" tools:context="com.tlkg.welcome.textinputlayoutdemo.MainActivity"> <View android:layout_width="match_parent" android:layout_height="1dp" android:focusable="true" android:focusableInTouchMode="true" /> <android.support.design.widget.TextInputLayout android:id="@+id/un_input_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="40dp" app:errorEnabled="true" app:errorTextAppearance="@style/TextAppearance.Design.Error"> <EditText android:id="@+id/et_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入用户名" android:textSize="14sp" /> </android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout android:id="@+id/ps_input_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/un_input_layout" app:errorEnabled="true" app:errorTextAppearance="@style/TextAppearance.Design.Error"> <EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码" android:inputType="textPassword" android:textSize="14sp" /> </android.support.design.widget.TextInputLayout> <Button android:id="@+id/btn_login" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/ps_input_layout" android:enabled="false" android:text="登 录" /> <ProgressBar android:id="@+id/pb_loading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/btn_login" android:layout_centerHorizontal="true" android:visibility="gone" /></RelativeLayout>
- MainActivity代码
public class MainActivity extends AppCompatActivity implements TextWatcher, View.OnClickListener { private TextInputLayout unInputLayout, psInputLayout; private EditText etUserName, etPassWord; private Button btnLogin; private ProgressBar pbLoading; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); initEvent(); } private void initViews() { unInputLayout = (TextInputLayout) findViewById(R.id.un_input_layout); psInputLayout = (TextInputLayout) findViewById(R.id.ps_input_layout); etUserName = (EditText) findViewById(R.id.et_username); etPassWord = (EditText) findViewById(R.id.et_password); btnLogin = (Button) findViewById(R.id.btn_login); pbLoading = (ProgressBar) findViewById(R.id.pb_loading); } private void initEvent() { etUserName.postDelayed(new Runnable() { @Override public void run() { etUserName.requestFocus();//延时获取焦点 } }, 1000); etUserName.addTextChangedListener(this); etPassWord.addTextChangedListener(this); btnLogin.setOnClickListener(this); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { unInputLayout.setError(""); } @Override public void afterTextChanged(Editable s) { String userName = etUserName.getText().toString(); String passWord = etPassWord.getText().toString(); if (TextUtils.isEmpty(userName) || TextUtils.isEmpty(passWord)) { btnLogin.setEnabled(false); return; } btnLogin.setEnabled(true); } @Override public void onClick(View v) { final String userName = etUserName.getText().toString(); final String passWord = etPassWord.getText().toString(); hideKeyboard(); pbLoading.setVisibility(View.VISIBLE); new Thread(new Runnable() { @Override public void run() { SystemClock.sleep(2000); runOnUiThread(new Runnable() { @Override public void run() { pbLoading.setVisibility(View.GONE); if ("admin".equals(userName) && "12345".equals(passWord)) { Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show(); } else { unInputLayout.setError("用户名或密码错误"); } } }); } }).start(); } /** * 隐藏键盘 */ private void hideKeyboard() { if (getCurrentFocus() != null) { ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)). hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } }}
首先在initViews()方法findViewById绑定控件,然后在initEvent()方法在设置监听事件,当etUserName和etPassWord控件都输入内容的时候,将登录按钮设置为可用,点击登录按钮时,模拟显示加载圈,延时2s,显示结果,这里模拟用户名是admin,密码是12345,当输入结果正确时,显示登录成功。
阅读全文
0 0
- 使用TextInputLayout
- TextInputLayout、Snackbar使用
- TextInputLayout 的简单使用
- TextInputLayout的使用
- TextInputLayout的使用
- TextInputLayout的使用
- TextInputLayout 的使用
- TextInputLayout使用教程
- TextInputLayout的使用
- android TextInputLayout的使用
- TextInputLayout&TextInputEditText的使用
- TextInputLayout的简单使用
- TextInputLayout的使用
- Android TextInputLayout 使用
- TextInputLayout的使用
- textInputLayout使用简介
- TextInputLayout的使用
- TextInputLayout基本使用
- 设计模式之禅笔记-6大设计原则(下)
- ubuntu16.04 vps搭建vpn
- Linux系列—iptables/netfilter命令参数详解中文手册
- FFMPEG学习【libswscale】
- Spring任务调度之quartz
- 使用TextInputLayout
- linux源码解析-dup_task_struct函数
- 26资源队列工作负载管理
- POJ3692——Kindergarten(最大二分匹配)
- Linux 开发中常用命令
- hpuoj 【1037】一个简单的数学题【同余定理】
- dojo与requireJS的分析
- Linux 终端下颜色的输出
- Hadoop学习之路-----不同进程通信的代理对象