1.自定义文本框
选中状态: <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"><!--指定形状内部颜色--><solid android:color="#ffffff"></solid><!--指定形状轮廓的粗细与颜色--><stroke android:width="1dp"android:color="#0000ff"></stroke><!--指定圆角半径--><corners android:radius="5dp"/><!--指定上下左右间距--><padding android:bottom="2dp"android:left="2dp"android:right="2dp"android:top="2dp"></padding> </shape>
未选中状态:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"><!--指定形状内部颜色--><solid android:color="#ffffff"></solid><!--指定形状轮廓的粗细与颜色--><stroke android:width="1dp"android:color="#aaaaaa"></stroke><!--指定圆角半径--><corners android:radius="5dp"/><!--指定上下左右间距--><padding android:bottom="2dp"android:left="2dp"android:right="2dp"android:top="2dp"></padding></shape>
2.定义文本框
<EditTextandroid:id="@+id/ed1"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@drawable/et1"android:text="请输入名字"> </EditText> <EditTextandroid:id="@+id/ed2"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@drawable/edittext_selector"android:inputType="textPassword"android:maxLength="11"android:hint="请输密码"> </EditText> <LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:gravity="end"android:text="用户:"></TextView><EditTextandroid:id="@+id/ed3"android:layout_width="0dp"android:layout_weight="3"android:layout_height="wrap_content"android:hint="请输入手机号码"android:inputType="number"android:maxLength="11"></EditText> </LinearLayout> <LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:gravity="end"android:text="密码:"></TextView><EditTextandroid:id="@+id/ed4"android:layout_width="0dp"android:layout_weight="3"android:layout_height="wrap_content"android:hint="请输入验证码"android:maxLength="6"android:inputType="numberPassword"></EditText> </LinearLayout>
3.焦点监听
editText3 = findViewById(R.id.ed3); //焦点监听 editText3.setOnFocusChangeListener(this::onFocusChange);private void onFocusChange(View view, boolean b) {if(b){String phone = editText3.getText().toString();Log.d("用户账号数据:", "onFocusChange: "+phone);//手机号码不足11位if(TextUtils.isEmpty(phone)||phone.length()<11){//手机号码编辑框请求焦点,把光标移回手机号码编辑框editText3.requestFocus();Log.d("用户账号数据:", "手机号码不足11位!!! ");Toast.makeText(this, "手机号码不足11位", Toast.LENGTH_SHORT).show();}}}
4.当输入达到最大值,关闭键盘
editText3 = findViewById(R.id.ed3); editText4 = findViewById(R.id.ed4);
//文本变化监听 editText3.addTextChangedListener(new HideTextWatcher (editText3,11)); editText4.addTextChangedListener(new HideTextWatcher (editText4,6));
/*** 自定义文本变化监听类*/ private class HideTextWatcher implements TextWatcher{private EditText mView;private int mMaxLenth;//定义一个构造方法public HideTextWatcher(EditText v, int maxLenth) {this.mView = v;this.mMaxLenth = maxLenth;}//改变之前@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {}//改变中@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {}//改变之后@Overridepublic void afterTextChanged(Editable s) {//获取输入文本String str = s.toString();//输入文本达到最大数,关闭输入法if(str.length()==mMaxLenth){ViewUitl.hideOneInputMethod( MainActivity3.this,mView);}} }
5.自定义关闭键盘工具类
public class ViewUitl {/*** 关闭键盘* @param act* @param v*/public static void hideOneInputMethod(Activity act, View v) {//从系统获取输入法InputMethodManager imm =(InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);//关闭键盘imm.hideSoftInputFromWindow(v.getWindowToken(),0);}}