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


    实现功能

    以上代码可实现功能:

    1 自定义文本框样式

    2. 文本框触发形式转变

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

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

    1. drawable自定义形状

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

    editor_focus.xml代码

    1. "1.0" encoding="utf-8"?>
    2. "http://schemas.android.com/apk/res/android">
    3. "#ffffff" />
    4. android:width="1dp"
    5. android:color="#0000ff" />
    6. "5dp" />
    7. android:bottom="2dp"
    8. android:left="2dp"
    9. android:right="2dp"
    10. android:top="2dp" />

    editor_unfocus.xml代码

    1. "1.0" encoding="utf-8"?>
    2. "http://schemas.android.com/apk/res/android">
    3. "#ffffff" />
    4. android:width="1dp"
    5. android:color="#888888" />
    6. "5dp" />
    7. android:bottom="2dp"
    8. android:left="2dp"
    9. android:right="2dp"
    10. android:top="2dp" />

    2. drawable自定义selector

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

    1. "1.0" encoding="utf-8"?>
    2. "http://schemas.android.com/apk/res/android">
    3. "@drawable/editor_focus" android:state_focused="true" />
    4. "@drawable/editor_unfocus" />

    3. 创建 empty Activity

    4.设置layout.xml

    1. "1.0" encoding="utf-8"?>
    2. "http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. xmlns:tools="http://schemas.android.com/tools"
    5. android:layout_width="match_parent"
    6. android:layout_height="match_parent"
    7. android:orientation="vertical">
    8. android:id="@+id/et_phone"
    9. android:layout_width="match_parent"
    10. android:layout_height="50dp"
    11. android:background="@drawable/editor_selector"
    12. android:hint="请输入电话号码"
    13. android:maxLength="11"
    14. android:inputType="text"
    15. android:layout_marginTop="5dp"
    16. />
    17. android:id="@+id/et_password"
    18. android:layout_width="match_parent"
    19. android:layout_height="50dp"
    20. android:background="@drawable/editor_selector"
    21. android:hint="请输入密码"
    22. android:maxLength="6"
    23. android:inputType="textPassword"
    24. android:layout_marginTop="5dp"
    25. />

    5.Java代码

    Activity

    1. package com.example.learn;
    2. import androidx.appcompat.app.AppCompatActivity;
    3. import android.os.Bundle;
    4. import android.text.Editable;
    5. import android.text.TextWatcher;
    6. import android.view.View;
    7. import android.widget.EditText;
    8. import android.widget.Toast;
    9. import com.example.learn.utils.ViewUtil;
    10. public class EditerHideActivity extends AppCompatActivity implements View.OnFocusChangeListener{
    11. private EditText et_phone;
    12. @Override
    13. protected void onCreate(Bundle savedInstanceState) {
    14. super.onCreate(savedInstanceState);
    15. setContentView(R.layout.activity_editer_hide);
    16. et_phone = findViewById(R.id.et_phone);
    17. EditText et_password = findViewById(R.id.et_password);
    18. //设置Text changeListener
    19. et_phone.addTextChangedListener(new HideTextWatch(et_phone,11));
    20. et_password.addTextChangedListener(new HideTextWatch(et_password,6));
    21. //设置password框focus时对phone自动检测
    22. et_password.setOnFocusChangeListener(this);
    23. }
    24. @Override
    25. public void onFocusChange(View view, boolean b) {
    26. //如果获取聚焦
    27. if(b){
    28. String str = et_phone.getText().toString();
    29. if(!"".equals(str) && str.length()<11){
    30. et_phone.requestFocus();
    31. Toast.makeText(this,"电话号码不足11位",Toast.LENGTH_SHORT).show();
    32. }
    33. }
    34. }
    35. private class HideTextWatch implements TextWatcher {
    36. private EditText mView;
    37. private int maxLen;
    38. public HideTextWatch(EditText v, int len) {
    39. this.mView = v;
    40. this.maxLen=len;
    41. }
    42. @Override
    43. public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    44. }
    45. @Override
    46. public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    47. }
    48. @Override
    49. public void afterTextChanged(Editable editable) {
    50. //获取文本框输入的文本
    51. String str = editable.toString();
    52. //判断是否输入的字符串与maxLen一致
    53. //一致则关软键盘
    54. if(str.length() == this.maxLen){
    55. //EditerHideActivity.this 外部类的 activity
    56. //this:HideTextWatch
    57. ViewUtil.hideKeyboard(EditerHideActivity.this,mView);
    58. }
    59. }
    60. }
    61. }

    Util

    1. package com.example.learn.utils;
    2. import android.app.Activity;
    3. import android.content.Context;
    4. import android.view.View;
    5. import android.view.inputmethod.InputMethodManager;
    6. public class ViewUtil {
    7. //当输入达到对应长度后 隐藏键盘
    8. public static void hideKeyboard(Activity act, View v){
    9. //从系统服务中获取输入法管理器
    10. InputMethodManager im = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
    11. //将系统输入软件盘隐藏
    12. im.hideSoftInputFromWindow(v.getWindowToken(),0);
    13. }
    14. }

  • 相关阅读:
    ​目标检测算法——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