总是收到此警告 TextInputLayout:添加的EditText不是TextInputEditText。请改用该类。 ,并且第一次没有完成 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();
}
}
});
}
}
最佳答案
更改此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>