TextInputLayout:添加的 EditText 不是 TextInputEditText。请改为使用 class

TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead

总是收到此警告TextInputLayout:添加的 EditText 不是 TextInputEditText。请改为使用 class。 而不是第一次完成 Activity。第二次没有收到警告并且 activity 完美完成。

activity_login.xml

<LinearLayout
        android:id="@+id/ll_login_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:visibility="visible">


        <android.support.design.widget.TextInputLayout
            android:id="@+id/email_field"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="16dp"
            android:hint="@string/email_phone"
            android:paddingTop="48dp">

            <android.support.v7.widget.AppCompatEditText
                android:id="@+id/et_email"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:inputType="textEmailAddress"
                android:paddingEnd="0dp"
                android:paddingStart="8dp" />

        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/password_field"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/password"
            android:paddingBottom="16dp">

            <android.support.v7.widget.AppCompatEditText
                android:id="@+id/et_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:inputType="textPassword"
                android:paddingEnd="0dp"
                android:paddingStart="8dp" />

        </android.support.design.widget.TextInputLayout>


        <Button
            android:id="@+id/btn_login"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="@dimen/button_login_social_margin"
            android:layout_marginStart="@dimen/button_login_social_margin"
            android:layout_marginBottom="@dimen/activity_vertical_margin"
            android:text="@string/login"
            android:textColor="@android:color/black" />

    </LinearLayout>

LoginActivity.java

    public class LoginActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        final AppCompatEditText etEmailOrPh = (AppCompatEditText) findViewById(R.id.et_email);
        final AppCompatEditText etPassword = (AppCompatEditText) findViewById(R.id.et_password);
        final Button btnLogin = (Button) findViewById(R.id.btn_login);

        assert etEmailOrPh != null;
        assert etPassword != null;
        assert  btnLogin != null;

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String emailOrPhone = etEmailOrPh.getText().toString().trim();
                String password = etPassword.getText().toString().trim();

                if(emailOrPhone.isEmpty()){
                    etEmailOrPh.setError(getResources().getString(R.string.email_phone_mandatory));
                    etEmailOrPh.requestFocus();
                } else if(emailOrPhone.contains("@") && CommonUtil.isValidEmail(emailOrPhone)) {
                    etEmailOrPh.setError(getResources().getString(R.string.email_error));
                    etEmailOrPh.requestFocus();
                } else if(password.isEmpty()) {
                    etPassword.setError(getResources().getString(R.string.password_mandatory));
                    etPassword.requestFocus();
                } else {
                    SharedPreferences cache = LoginActivity.this.getSharedPreferences(Constants.SHARED_PREF_NAME, Context.MODE_PRIVATE);
                    final SharedPreferences.Editor preferenceEditor = cache.edit();
                    preferenceEditor.putInt(Constants.SHARED_PREF_ITEM_USER_ID, 1);
                    preferenceEditor.apply();
                    setResult(RESULT_OK);
                    finish();
                }
            }
        });

    }
}

尽量不要使用

<android.support.v7.widget.AppCompatEditText

改为使用

<EditText

所以你会得到:

<EditText
            android:id="@+id/et_email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:inputType="textEmailAddress"
            android:paddingEnd="0dp"
            android:paddingStart="8dp" />

在对这个问题做了一些研究之后,我发现了这个 post:

如果对你有帮助,请告诉我。

更改此 EditText

android.support.v7.widget.AppCompatEditText

至此

android.support.design.widget.TextInputEditText

完整代码:

<android.support.design.widget.TextInputLayout
       android:id="@+id/email_field"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:paddingBottom="16dp"
       android:hint="@string/email_phone"
       android:paddingTop="48dp">

    <android.support.design.widget.TextInputEditText
            android:id="@+id/et_email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:inputType="textEmailAddress"
            android:paddingEnd="0dp"
            android:paddingStart="8dp"
        />
</android.support.design.widget.TextInputLayout>