TextInputLayout 登錄的實現

 TextInputLayout 
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="65dp"
        android:orientation="vertical">
        <android.support.design.widget.TextInputLayout
            android:id="@+id/layout_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:counterEnabled="false"
            app:counterMaxLength="5"
           >
            <EditText
                android:id="@+id/input_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="請輸入賬號"
                android:singleLine="true" />
        </android.support.design.widget.TextInputLayout>
        <android.support.design.widget.TextInputLayout
            android:id="@+id/layout_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:counterEnabled="false"
            app:counterMaxLength="11"
           >
            <EditText
                android:id="@+id/input_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="請輸入入密碼"
                android:inputType="textPassword"
                android:singleLine="true" />
        </android.support.design.widget.TextInputLayout>
        <Button
            android:id="@+id/login"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp"
            android:background="@color/colorPrimary"
            android:text="commit"
            android:textColor="#ffffff"
            android:textSize="20sp"
            android:textStyle="bold" />
    </LinearLayout>
代碼
 private TextInputEditText ut;
    private EditText et;
    private EditText input_name;
    private EditText input_password;
    private EditText input_email;
    private TextInputLayout layout_name;
    private TextInputLayout layout_password;
    private TextInputLayout layout_email;
    private Button btn_login;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          initWidget();
    }
    private void initWidget() {
        input_name = (EditText) findViewById(R.id.input_name);
        input_password = (EditText) findViewById(R.id.input_password);
        layout_name = (TextInputLayout) findViewById(R.id.layout_name);
        layout_password = (TextInputLayout) findViewById(R.id.layout_password);
        btn_login = (Button) findViewById(R.id.login);
        btn_login.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
       if (input_name.getText().toString().trim().equals("aaa")) {
            layout_name.setErrorEnabled(false);
        }else{
            layout_name.setError("您輸入的賬號不對");
            layout_password.setErrorEnabled(true);
                                        //requestFocus誰不對讓誰獲取焦點
           input_name.requestFocus();
        }
        if (input_password.getText().toString().trim().equals("aaa")) {
            layout_password.setErrorEnabled(false);
        }else{
            layout_password.setErrorEnabled(true);
            layout_password.setError("您輸入的密碼不對");
           input_password.requestFocus();
        }
    }
   
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章