• 第五篇Android--EditText详解


    EditText 字面意思可以编辑的文本。在Android中就是用来接收用户输入的输入框。

    1.基本用法

            

    1. <EditText
    2. android:id="@+id/id_phone_edit"
    3. android:layout_width="match_parent"
    4. android:layout_height="48dp"
    5. android:background="@android:color/transparent"
    6. android:hint="请输入手机号"
    7. android:inputType="phone"
    8. android:lines="1"
    9. android:maxLength="11"
    10. android:textColor="#FF151F24"
    11. android:textColorHint="#FFCECECE"
    12. android:textCursorDrawable="@drawable/edittext_cursor_bg"
    13. android:textSize="16sp" />

      清除系统默认的EditText背景,添加一个透明背景:

               android:background="@android:color/transparent"

      未输入时的提示文字和颜色:

             android:hint="请输入手机号"

             android:textColorHint="#FFCECECE"

        输入文字的颜色:

             android:textColor="#FF151F24"

        修改输入光标的颜色和大小:

            android:textCursorDrawable="@drawable/edittext_cursor_bg"

            通过drawable目录下定义shape的形式,创建一个drawable

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <shape xmlns:android="http://schemas.android.com/apk/res/android"
    3. android:shape="rectangle">
    4. <solid android:color="#FFAC36"></solid>
    5. <size
    6. android:width="1.8dp"
    7. android:height="17dp" />
    8. <corners android:radius="0.9dp" />
    9. </shape>

        类型:android:inputType="phone"

       phone:呼出的软键盘会自动切换成数字键盘,并且限制输入最大长度11个。 

       textPassword:文本密码,输入的内容会呈现密码输入的形式。

        numberPassword:数字密码,只接受数字类型。

     2.代码获取输入的文字:

    1. editText = findViewById(R.id.id_phone_edit);
    2. String phoneNum = editText.getText().toString().trim();

    3.监听EditText文本输入:10086

    1. editText.addTextChangedListener(new TextWatcher() {
    2. @Override
    3. public void beforeTextChanged(CharSequence s, int start, int count, int after){
    4. Log.e("nyz","before "+s);
    5. }
    6. @Override
    7. public void onTextChanged(CharSequence s, int start, int before, int count) {
    8. Log.e("nyz","change "+s);
    9. }
    10. @Override
    11. public void afterTextChanged(Editable s) {
    12. Log.e("nyz","after "+s);
    13. }
    14. });

       打印日志:

          通过日志可以看出:每一次输入都会调用三个回调函数。

    1. 2023-10-12 15:04:24.166 23878-23878/com.example.testview E/nyz: before
    2. 2023-10-12 15:04:24.169 23878-23878/com.example.testview E/nyz: change 1
    3. 2023-10-12 15:04:24.169 23878-23878/com.example.testview E/nyz: after 1
    4. 2023-10-12 15:04:24.555 23878-23878/com.example.testview E/nyz: before 1
    5. 2023-10-12 15:04:24.556 23878-23878/com.example.testview E/nyz: change 10
    6. 2023-10-12 15:04:24.556 23878-23878/com.example.testview E/nyz: after 10
    7. 2023-10-12 15:04:24.745 23878-23878/com.example.testview E/nyz: before 10
    8. 2023-10-12 15:04:24.747 23878-23878/com.example.testview E/nyz: change 100
    9. 2023-10-12 15:04:24.748 23878-23878/com.example.testview E/nyz: after 100
    10. 2023-10-12 15:04:24.956 23878-23878/com.example.testview E/nyz: before 100
    11. 2023-10-12 15:04:24.957 23878-23878/com.example.testview E/nyz: change 1008
    12. 2023-10-12 15:04:24.957 23878-23878/com.example.testview E/nyz: after 1008
    13. 2023-10-12 15:04:25.374 23878-23878/com.example.testview E/nyz: before 1008
    14. 2023-10-12 15:04:25.375 23878-23878/com.example.testview E/nyz: change 10086
    15. 2023-10-12 15:04:25.376 23878-23878/com.example.testview E/nyz: after 10086

    4.监听软件盘中的回车键:

        1)设置回车键的样式 android:imeOptions=“”,

               actionGo:回车键变为“开始”

               actionSearch:回车键变为“搜索”

               actionDone:默认样式

      2)代码中监听回车键:

    1. InputMethodManager manager = (InputMethodManager)
    2. getSystemService(Context.INPUT_METHOD_SERVICE);
    3. editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    4. @Override
    5. public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    6. Log.e("nyz", "actionId "+actionId);
    7. if (actionId == EditorInfo.IME_ACTION_UNSPECIFIED) {
    8. //隐藏软键盘
    9. if (manager.isActive()) {
    10. manager.hideSoftInputFromWindow(v.getWindowToken(), 0);
    11. }
    12. return true;
    13. }
    14. return false;
    15. }
    16. });

    通过actionId 可以监听按键的类型:

       最常用的还是ActionDone

    1. /**
    2. * Bits of {@link #IME_MASK_ACTION}: no specific action has been
    3. * associated with this editor, let the editor come up with its own if
    4. * it can.
    5. */
    6. public static final int IME_ACTION_UNSPECIFIED = 0x00000000;
    7. /**
    8. * Bits of {@link #IME_MASK_ACTION}: there is no available action.
    9. */
    10. public static final int IME_ACTION_NONE = 0x00000001;
    11. /**
    12. * Bits of {@link #IME_MASK_ACTION}: the action key performs a "go"
    13. * operation to take the user to the target of the text they typed.
    14. * Typically used, for example, when entering a URL.
    15. */
    16. public static final int IME_ACTION_GO = 0x00000002;
    17. /**
    18. * Bits of {@link #IME_MASK_ACTION}: the action key performs a "search"
    19. * operation, taking the user to the results of searching for the text
    20. * they have typed (in whatever context is appropriate).
    21. */
    22. public static final int IME_ACTION_SEARCH = 0x00000003;
    23. /**
    24. * Bits of {@link #IME_MASK_ACTION}: the action key performs a "send"
    25. * operation, delivering the text to its target. This is typically used
    26. * when composing a message in IM or SMS where sending is immediate.
    27. */
    28. public static final int IME_ACTION_SEND = 0x00000004;
    29. /**
    30. * Bits of {@link #IME_MASK_ACTION}: the action key performs a "next"
    31. * operation, taking the user to the next field that will accept text.
    32. */
    33. public static final int IME_ACTION_NEXT = 0x00000005;
    34. /**
    35. * Bits of {@link #IME_MASK_ACTION}: the action key performs a "done"
    36. * operation, typically meaning there is nothing more to input and the
    37. * IME will be closed.
    38. */
    39. public static final int IME_ACTION_DONE = 0x00000006;

    5.EditText 设置其他类型的事件监听:

    1. editText.setOnTouchListener((v, event) -> {
    2. //todo
    3. return false;
    4. });
    5. editText.setOnLongClickListener(v -> {
    6. //todo
    7. return false;
    8. });
    9. editText.setOnClickListener(v -> {
    10. });

  • 相关阅读:
    【启明智显分享】乐鑫HMI方案4.3寸触摸串口屏应用于称重测力控制仪表
    SLAM学了2年还是不会?每一步其实都是脚印
    扩散模型(Diffusion Model)简介
    VS 调试Hololens 2工程报错 有未经处理的异常: Microsoft C++ 异常:
    岛屿数量 -- 二维矩阵的dfs算法
    ChatGPT 4.0 升级指南
    Google Earth Engine(GEE)——利用行列号进行影像去云,使用研究区大于单景影像的研究区范围
    第20章 使用Spring进行事务管理(二)
    Docker 网络
    回顾以前的java
  • 原文地址:https://blog.csdn.net/niuyongzhi/article/details/133790773