【问题标题】:How to allow only some characters in android `edittext` control如何在android`edittext`控件中只允许某些字符
【发布时间】:2019-06-15 21:44:30
【问题描述】:

在下面的EditText 我想允许一些字符,如@, -, _, . (dot) 剩余的我不想允许输入的字符。甚至想屏蔽笑脸。

谁能帮帮我?

<EditText
     android:id="@+id/emailId"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_gravity="center"
     android:background="@color/transperent"
     android:hint="@string/email_id" 
     android:digits="0123456789*qwertzuiopasdfghjklyxcvbnm_-." 
     android:inputType="text"
     android:maxLines="1"
     android:padding="8dp"
     android:textColor="@color/white"
     android:textColorHint="@color/white" />

【问题讨论】:

  • 你为什么不在android:digits中添加那些字符

标签: android android-edittext


【解决方案1】:

您可以尝试使用数字,然后将您允许的每个字符都放在那里,例如所有英文字符:

android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ " 

【讨论】:

  • 这不是问题,例如,当您输入电子邮件 ID 时,我不想允许某些字符,如 *、&、$、# 等我想阻止某些字符
  • 比如说当用户输入 # char 你想禁用它吗?
  • 是的。但不是禁用某些字符。我想允许一些字符这很好
  • 看看this thread,或许对你有帮助
【解决方案2】:

Build.gradle (Module:app)

dependencies{
        compile 'com.xw.repo:xedittext:2.0.0@aar'
    }

XML:

<com.xw.repo.XEditText
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:inputType="text"
    android:layout_height="wrap_content"
    android:digits="0123456789qwertzuiopasdfghjklyxcvbnm_-." //Digits specific
    app:x_disableEmoji="true"/> // Disable Emoticons

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-09
    • 1970-01-01
    • 2020-02-27
    • 1970-01-01
    • 2013-05-02
    • 2017-12-07
    • 1970-01-01
    相关资源
    最近更新 更多