在使用EditText时,会出现Missing autofillHints attribute
解决方式1:使用ignore或者加入android:autofillHints 但是如果你api低于26,又会提示only used in API level 26 and higher 就又要使用ignore
解决方式2:使用TextInputEditText替代,本文主要讲解TextInputEditText,以邮箱格式作为讲解内容
判断是否是邮箱使用了正则表达式
解决方式3:使用androidx.appcompat.widget.AppCompatEditText替代
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".edit.EditTextActivity">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/edit_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="@string/edit_hint_email"
android:ems="10"/>
<ImageView
android:id="@+id/image_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_right"
android:contentDescription="@null"
android:visibility="gone"/>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/email_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/edit_hint_email"
app:errorTextAppearance="@style/ErrorTextAppearanceStyle"
>
<!--if you don't want to show hint in layout when focus , set app:hintEnabled="false"-->
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/edit_email_second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/edit_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
/>
</LinearLayout>
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;
import com.thomas.android.base.R;
/**
* I use TextInputEditText instead of EditText
*
*/
public class EditTextActivity extends AppCompatActivity {
private TextInputEditText edit_email ,edit_email_second,edit_pwd;
private TextInputLayout email_input_layout;
private ImageView image_status;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_text);
edit_email = findViewById(R.id.edit_email);
edit_email_second = findViewById(R.id.edit_email_second);
email_input_layout = findViewById(R.id.email_input_layout);
edit_pwd = findViewById(R.id.edit_pwd);
image_status = findViewById(R.id.image_status);
initEmailEvent();
initEmailEventSecond();
//android:inputType=textPassword //不可见密码
//android:inputType=textVisiblePassword//可见密码
//edit_pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
//edit_pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
/**
* I think use addTextChangedListener it will often do some useless work(is recommended)
*/
private void initEmailEventSecond() {
edit_email_second.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
}
/**
* @param charSequence text after you write
* @param start how long before you write
* @param before i wonder
* @param count how long you write
*/
@Override
public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
if (charSequence.length() >= 4 && charSequence.toString().matches("\\w+@\\w+\\.\\w+")) {
email_input_layout.setErrorEnabled(false);
} else {
email_input_layout.setError(getString(R.string.email_format_wrong));
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
/**
* KeyEvent @link https://www.cnblogs.com/paulwinflo/p/4754701.html
*/
private void initEmailEvent() {
edit_email.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
disposeKeyEvent(keyCode,keyEvent);
// false will continue event , I don't think so
return false;
}
});
}
/**
* first way to control input text (is not recommended)
* @param keyCode what you pressed
* @param keyEvent event happened when you pressed key
*/
private void disposeKeyEvent(int keyCode, KeyEvent keyEvent) {
// from this we can see that when you press key enter ,the event order is: ACTION_DOWN ->ACTION_UP
switch (keyEvent.getAction()){
case KeyEvent.ACTION_DOWN:
Toast.makeText(EditTextActivity.this,"ACTION_DOWN",Toast.LENGTH_SHORT).show();
break;
case KeyEvent.ACTION_UP:
Toast.makeText(EditTextActivity.this,"ACTION_UP",Toast.LENGTH_SHORT).show();
break;
}
// by the way , you can also control input text when ACTION_DOWN or ACTION_UP
if (keyCode == KeyEvent.KEYCODE_ENTER) {
String email = edit_email.getEditableText().toString().trim();
// email regular
if (email.matches("\\w+@\\w+\\.\\w+")) {
image_status.setImageResource(R.drawable.ic_right);
} else {
image_status.setImageResource(R.drawable.ic_wrong);
}
image_status.setVisibility(View.VISIBLE);
}
}
}