您的位置:首页 > 移动开发 > Android开发

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);
}
}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Material Design Android