如何禁止在 Android EditText 中输入表情符号?
- 作者: 如果如果可以的话我想说我爱你
- 来源: 51数据库
- 2023-02-09
问题描述
EditText 和 TextView 的 text inputType(URI、密码等除外)的大多数实现都允许 Emoji - 尽管在大多数 Google 键盘配置中此按钮是隐藏的.有没有办法禁止在 EditText 中输入表情符号?是否有一个 inputType 参数可以与禁用 Emoji 的 textMultiLine 配对?
Most implementations of the text inputType (other than URI, password, etc.) for EditText and TextView allow Emoji - although in most Google keyboard configurations this button is hidden. Is there a way to disable Emoji from being entered in an EditText? Is there an inputType parameter that could be paired with textMultiLine that would disable Emoji?
推荐答案
修改build.gradle文件,添加XEditText 到你的项目:
Modify build.gradle file, add XEditText to your project:
dependencies{ compile 'com.xw.repo:xedittext:2.0.0@aar' }
之后,在您的 layout.xml 中:
<com.xw.repo.XEditText xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" app:x_disableEmoji="true"/>
或者:
像这样自定义 EditText:
Or:
Customize EditText like this:
public class CustomEditText extends EditText { public CustomEditText(Context context) { super(context); init(); } public CustomEditText(Context context, AttributeSet attrs) { super(context, attrs); init(); } public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { setFilters(new InputFilter[]{new EmojiExcludeFilter()}); } private class EmojiExcludeFilter implements InputFilter { @Override public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { for (int i = start; i < end; i++) { int type = Character.getType(source.charAt(i)); if (type == Character.SURROGATE || type == Character.OTHER_SYMBOL) { return ""; } } return null; } } }
两者都可以正常工作!
推荐阅读
热点文章
检查拆分键盘
0
带有“上一个"的工具栏和“下一个"用于键盘输入AccessoryView
0
Activity 启动时显示软键盘
0
UIWebView 键盘 - 摆脱“上一个/下一个/完成"酒吧
0
在 iOS7 中边缘滑动时,使键盘与 UIView 同步动画
0
我的 iOS 应用程序中的键盘在 iPhone 6 上太高了.如何在 XCode 中调整键盘的分辨率?
0
android:inputType="textEmailAddress";- '@' 键和 '.com' 键?
0
禁用 iPhone 中键盘的方向
0
Android 2.3 模拟器上的印地语键盘问题
0
keyDown 没有被调用
0