TextInputEditText 替换EditText解决警告Missing autofillHints attribute以及实现错误提示
16lz
2021-01-23
在使用EditText时,会出现Missing autofillHints attribute
解决方式1:使用ignore或者加入android:autofillHints 但是如果你api低于26,又会提示only used in API level 26 and higher 就又要使用ignore
解决方式2:使用TextInputEditText替代,本文主要讲解TextInputEditText,以邮箱格式作为讲解内容
判断是否是邮箱使用了正则表达式
<?xml version="1.0" encoding="utf-8"?>
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("\\[email protected]\\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("\\[email protected]\\w+\\.\\w+")) { image_status.setImageResource(R.drawable.ic_right); } else { image_status.setImageResource(R.drawable.ic_wrong); } image_status.setVisibility(View.VISIBLE); } }}
更多相关文章
- Android联网方式判断详解
- Android的Activity加载方式实例分析
- Android 开发艺术探索笔记(五) 之 Android 中的 IPC 方式
- android学习小结3-各种控件使用方式DEMO
- Android桌面快捷方式那些事与那些坑
- android httpClient 支持HTTPS的访问方式
- Android--AIDL,一种实现进程间通信的方式
- Android IPC的6种方式