以上代码可实现功能:
1 自定义文本框样式
2. 文本框触发形式转变
3. 文本框输入长度监听,达到最大长度关闭软键盘
4. password框触发检测phone框内容


我创建了editor_focus.xml 和 editor_unfocus.xml,两者仅边界颜色不同
editor_focus.xml代码
- "1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"> -
-
"#ffffff" /> -
-
- android:width="1dp"
- android:color="#0000ff" />
-
-
"5dp" /> -
-
- android:bottom="2dp"
- android:left="2dp"
- android:right="2dp"
- android:top="2dp" />
-
editor_unfocus.xml代码
- "1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"> -
-
"#ffffff" /> -
-
- android:width="1dp"
- android:color="#888888" />
-
-
"5dp" /> -
-
- android:bottom="2dp"
- android:left="2dp"
- android:right="2dp"
- android:top="2dp" />
-
2. drawable自定义selector
selector的作用为不同的触发事件,编辑框有不同的形式。创建过程和上述shape创建过程类似,只是将shape改为 selector即可。
- "1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"> -
- "@drawable/editor_focus" android:state_focused="true" />
-
- "@drawable/editor_unfocus" />
3. 创建 empty Activity
4.设置layout.xml
- "1.0" encoding="utf-8"?>
"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">
-
-
- android:id="@+id/et_phone"
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:background="@drawable/editor_selector"
- android:hint="请输入电话号码"
- android:maxLength="11"
- android:inputType="text"
- android:layout_marginTop="5dp"
- />
-
-
- android:id="@+id/et_password"
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:background="@drawable/editor_selector"
- android:hint="请输入密码"
- android:maxLength="6"
- android:inputType="textPassword"
- android:layout_marginTop="5dp"
- />
-
5.Java代码
Activity
- package com.example.learn;
-
- import androidx.appcompat.app.AppCompatActivity;
-
- import android.os.Bundle;
- import android.text.Editable;
- import android.text.TextWatcher;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
-
- import com.example.learn.utils.ViewUtil;
-
- public class EditerHideActivity extends AppCompatActivity implements View.OnFocusChangeListener{
- private EditText et_phone;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_editer_hide);
- et_phone = findViewById(R.id.et_phone);
- EditText et_password = findViewById(R.id.et_password);
- //设置Text changeListener
- et_phone.addTextChangedListener(new HideTextWatch(et_phone,11));
- et_password.addTextChangedListener(new HideTextWatch(et_password,6));
- //设置password框focus时对phone自动检测
- et_password.setOnFocusChangeListener(this);
- }
-
- @Override
- public void onFocusChange(View view, boolean b) {
- //如果获取聚焦
- if(b){
- String str = et_phone.getText().toString();
- if(!"".equals(str) && str.length()<11){
- et_phone.requestFocus();
- Toast.makeText(this,"电话号码不足11位",Toast.LENGTH_SHORT).show();
- }
- }
- }
-
- private class HideTextWatch implements TextWatcher {
- private EditText mView;
- private int maxLen;
- public HideTextWatch(EditText v, int len) {
- this.mView = v;
- this.maxLen=len;
- }
-
- @Override
- public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
-
- }
-
- @Override
- public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
-
- }
-
- @Override
- public void afterTextChanged(Editable editable) {
- //获取文本框输入的文本
- String str = editable.toString();
- //判断是否输入的字符串与maxLen一致
- //一致则关软键盘
- if(str.length() == this.maxLen){
- //EditerHideActivity.this 外部类的 activity
- //this:HideTextWatch
- ViewUtil.hideKeyboard(EditerHideActivity.this,mView);
- }
- }
- }
- }
Util
- package com.example.learn.utils;
-
- import android.app.Activity;
- import android.content.Context;
- import android.view.View;
- import android.view.inputmethod.InputMethodManager;
-
- public class ViewUtil {
- //当输入达到对应长度后 隐藏键盘
- public static void hideKeyboard(Activity act, View v){
- //从系统服务中获取输入法管理器
- InputMethodManager im = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
- //将系统输入软件盘隐藏
- im.hideSoftInputFromWindow(v.getWindowToken(),0);
- }
- }
-
相关阅读:
目标检测算法——YOLOv5/YOLOv7改进之结合SOCA(单幅图像超分辨率)
stm32之PWM呼吸灯
怎样查询服务器位置和IP地址?
【C++ 学习 ⑳】- 详解二叉搜索树
Oracle/PLSQL: From_Tz function
通过Redis实现一个异步请求-响应程序
Leetcode148. 排序链表
《Python 3网络爬虫开发实战 》崔庆才著 第五章笔记
直销系统开发是找开发公司还是外包团队?
uniapp heckbox-group实现多选
-
原文地址:https://blog.csdn.net/qq_29750461/article/details/138449779