【Android学习】自定义文本框和输入监听

实现功能

以上代码可实现功能:

1 自定义文本框样式

  1. 文本框触发形式转变

  2. 文本框输入长度监听,达到最大长度关闭软键盘

  3. password框触发检测phone框内容

1. drawable自定义形状

我创建了**editor_focus.xml 和 editor_unfocus.xml,**两者仅边界颜色不同

editor_focus.xml代码

java 复制代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!--    指定文本框内部颜色-->
    <solid android:color="#ffffff" />
    <!--    声明边界线颜色-->
    <stroke
        android:width="1dp"
        android:color="#0000ff" />
    <!--    使用圆角-->
    <corners android:radius="5dp" />
    <!--    指定形状四个方向的间距-->
    <padding
        android:bottom="2dp"
        android:left="2dp"
        android:right="2dp"
        android:top="2dp" />

</shape>

editor_unfocus.xml代码

java 复制代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!--    指定文本框内部颜色-->
    <solid android:color="#ffffff" />
    <!--    声明边界线颜色-->
    <stroke
        android:width="1dp"
        android:color="#888888" />
    <!--    使用圆角-->
    <corners android:radius="5dp" />
    <!--    指定形状四个方向的间距-->
    <padding
        android:bottom="2dp"
        android:left="2dp"
        android:right="2dp"
        android:top="2dp" />

</shape>

2. drawable自定义selector

selector的作用为不同的触发事件,编辑框有不同的形式。创建过程和上述shape创建过程类似,只是将shape改为 selector即可。

java 复制代码
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/editor_focus" android:state_focused="true" />
    <item android:drawable="@drawable/editor_unfocus" />
</selector>

3. 创建 empty Activity

4.设置layout.xml

java 复制代码
<?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">

    <EditText
        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"
        />

    <EditText
        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"
        />

</LinearLayout>

5.Java代码

Activity

java 复制代码
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

java 复制代码
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);
    }
}
相关推荐
三少爷的鞋2 分钟前
为什么我不在 Android ViewModel 中直接处理异常?
android
魔力军4 分钟前
Rust学习Day2: 变量与可变性、数据类型和函数和控制流
开发语言·学习·rust
黄大帅@lz6 分钟前
openai提示词学习
windows·学习
pop_xiaoli7 分钟前
effective-Objective-C 第二章阅读笔记
笔记·学习·ios·objective-c·cocoa
恣逍信点8 分钟前
《凌微经 · 理悖相涵》第七章 形性一体——本然如是之元观
人工智能·科技·学习·程序人生·生活·交友·哲学
stars-he8 分钟前
AI工具配置学习笔记
人工智能·笔记·学习
Master_oid9 分钟前
机器学习32:机器终生学习(Life Long Learning)
人工智能·学习·机器学习
我的xiaodoujiao16 分钟前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 47--设置Selenium以无头模式运行代码
python·学习·selenium·测试工具·pytest
草莓熊Lotso1 小时前
Linux 文件描述符与重定向实战:从原理到 minishell 实现
android·linux·运维·服务器·数据库·c++·人工智能
恋猫de小郭1 小时前
Flutter Zero 是什么?它的出现有什么意义?为什么你需要了解下?
android·前端·flutter