美文网首页Android开发经验总结篇
MD5新属性之TextInputLayout

MD5新属性之TextInputLayout

作者: zhengLH | 来源:发表于2018-03-20 15:56 被阅读2次

    【核心代码】

    /**
    * @Author Lee
    * @Time 2018/3/20
    * @Theme  TextInputLayout 相当Linearlayout 容器
    */
    
    public class TextInputLayoutActivity extends AppCompatActivity implements View.OnClickListener {
    
    private EditText mEtAccount;
    private EditText mEtAPassword;
    private TextInputLayout mTilAccount;
    private TextInputLayout mTilPwd;
    private Button mBtnLogin;
    
    private static final String EMAIL_PATTERN = "^[a-zA-Z0-9#_~!$&'()*+,;=:.\"(),:;<>@\\[\\]\\\\]+@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*$";
    private Pattern pattern = Pattern.compile(EMAIL_PATTERN);
    private Matcher matcher;
    
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activit_text_input_layout);
        initView();
    }
    
    private void initView() {
    
    
        mEtAccount = findViewById(R.id.et_account);
        mEtAPassword = findViewById(R.id.et_password);
        mBtnLogin = findViewById(R.id.btn_login);
    
        mTilAccount = findViewById(R.id.til_account);
        mTilPwd = findViewById(R.id.til_pwd);
    
    /*
        mEtAccount.setHint("请输入账号");
        mEtAPassword.setHint("请输入密码");*/
    
        mTilAccount.setHint("请输入账号");
        mTilPwd.setHint("请输入密码");
    
        mEtAccount.setOnClickListener(this);
        mBtnLogin.setOnClickListener(this);
    
    
    }
    
    /**
     *  验证邮箱
     * @param email
     * @return
     */
    public boolean validateEmail(String email) {
        matcher = pattern.matcher(email);
        return matcher.matches();
    }
    
    /**
     * 验证密码
     * @param password
     * @return
     */
    public boolean validatePassword(String password) {
        return password.length() > 5;
    }
    
    /**
     * 隐藏键盘
     */
    public void hindSoftInput(){
    
        View view1 = getCurrentFocus();
        if(view1 != null){
    
            InputMethodManager mInputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            mInputManager.hideSoftInputFromWindow(view1.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    
        }
    }
    
    
    
    @Override
    public void onClick(View view) {
    
        switch (view.getId()) {
            case R.id.btn_login:
    
                hindSoftInput();
    
                // getEditText() 相当于 findViewById()
                String username = mTilAccount.getEditText().getText().toString();
                String password = mTilPwd.getEditText().getText().toString();
    
                if(!validateEmail(username)){
    
                    mTilAccount.setError("不是一个有效的邮箱地址");
    
                }else if( ! validatePassword(password)){
    
                    mTilPwd.setError("不是一个有效的密码");
    
                 }else{
    
                    mTilAccount.setErrorEnabled(false);
                    mTilPwd.setErrorEnabled(false);
    
                }
    
                break;
    
        }
        }
    }
    

    【xml 布局】

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    
    <android.support.design.widget.TextInputLayout
        android:id="@+id/til_account"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <EditText
            android:id="@+id/et_account"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
         />
    
    </android.support.design.widget.TextInputLayout>
    
    <android.support.design.widget.TextInputLayout
        android:id="@+id/til_pwd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
        <EditText
            android:id="@+id/et_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
          />
    
    </android.support.design.widget.TextInputLayout>
    
    <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录"
        />
     </LinearLayout>
    

    【注】TextInputLayout控件和LinearLayout完全一样,它只是一个容器。跟ScrollView一样,TextInputLayout只接受一个子元素。子元素需要是一个EditText元素。

    【来源】http://www.jcodecraeer.com/a/basictutorial/2015/0821/3338.html

    相关文章

      网友评论

        本文标题:MD5新属性之TextInputLayout

        本文链接:https://www.haomeiwen.com/subject/pujbqftx.html