Material Design之TextInputLayout
2016-05-04 11:34
453 查看
TextInputLayout包裹EditText,且必须只有子元素,类似ScrollView。例子:
" data-snippet-id="ext.755ea1ebdbe82042bf1c8a727a35bbd1" data-snippet-saved="false" data-codota-status="done">[code]<android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.design.widget.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/form_username"/> </android.support.design.widget.TextInputLayout>
下面来实现一个简单的登录:
activity_login.xml:
" data-snippet-id="ext.5028389a45fc7195637d57422fb7c073" data-snippet-saved="false" data-codota-status="done">[code]<?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:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="@dimen/activity_horizontal_margin"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.5" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:gravity="center" android:text="Welcome" android:textColor="#333333" android:textSize="30sp"/> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.5" android:orientation="vertical"> <android.support.design.widget.TextInputLayout android:id="@+id/usernameWrapper" android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.design.widget.TextInputEditText android:id="@+id/username" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Username" android:inputType="textEmailAddress"/> </android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout android:id="@+id/passwordWrapper" android:layout_width="match_parent" android:layout_height="wrap_content" app:counterEnabled="true" app:counterMaxLength="6"> <android.support.design.widget.TextInputEditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Password" android:inputType="textPassword"/> </android.support.design.widget.TextInputLayout> <Button android:id="@+id/btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="4dp" android:text="Login"/> </LinearLayout> </LinearLayout>
package com.leigo.myapplication; import android.content.Context; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.design.widget.Snackbar; import android.support.design.widget.TextInputLayout; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.util.Patterns; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.Button; /** * Created by GL on 2016/4/26. */ public class TextInputLayoutDemo extends AppCompatActivity implements View.OnClickListener { private TextInputLayout usernameWrapper; private TextInputLayout passwordWrapper; private Button mButton; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); usernameWrapper = (TextInputLayout) findViewById(R.id.usernameWrapper); passwordWrapper = (TextInputLayout) findViewById(R.id.passwordWrapper); mButton = (Button) findViewById(R.id.btn); mButton.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn: hideKeyboard(); String username = usernameWrapper.getEditText().getText().toString().trim(); String password = passwordWrapper.getEditText().getText().toString().trim(); if (TextUtils.isEmpty(username)) { passwordWrapper.setError(null); usernameWrapper.setError("请输入用户名"); } else if (!Patterns.PHONE.matcher(username).matches()) { passwordWrapper.setError(null); usernameWrapper.setError("请输入手机号"); } else if (TextUtils.isEmpty(password)) { usernameWrapper.setError(null); passwordWrapper.setError("请输入密码"); } else { usernameWrapper.setErrorEnabled(false); passwordWrapper.setErrorEnabled(false); Snackbar.make(findViewById(R.id.container), "我登录了", Snackbar.LENGTH_LONG).show(); } break; } } private void hideKeyboard() { View view = getCurrentFocus(); if (view != null) { ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)). hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } } }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories