如何禁止在 Android EditText 中输入表情符号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22990870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to disable emoji from being entered in Android EditText?
提问by Alex Wang
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?
text
EditText 和 TextView的inputType(除了 URI、密码等)的大多数实现都允许表情符号 - 尽管在大多数 Google 键盘配置中,此按钮是隐藏的。有没有办法禁止在 EditText 中输入表情符号?是否有可以与textMultiLine
禁用表情符号配对的 inputType 参数?
回答by woxingxiao
Modify build.gradlefile, add XEditTextto your project:
修改build.gradle文件,将XEditText添加到您的项目中:
dependencies{
compile 'com.xw.repo:xedittext:2.0.0@aar'
}
after that, in your layout.xml:
之后,在你的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"/>
Or:
或者:
Customize EditText like this:
像这样自定义 EditText:
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;
}
}
}
Both will work well !
两者都会很好用!
回答by Pragnesh Ghoda シ
There is tricky way for disabling emoji from keyboard..
从键盘禁用表情符号有一种棘手的方法..
you just have to set
你只需要设置
android:inputType="textEmailAddress"
android:inputType="textEmailAddress"
for EditText
..
对于EditText
..
<EditText
android:id="@+id/edt_note"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/note"
android:inputType="textEmailAddress"
android:padding="10dp"
android:textColor="@color/white" />
I am not sure that it will work in all cases but in my case it worked for me...
我不确定它是否适用于所有情况,但就我而言,它对我有用......
回答by Adrian Grygutis
There is value digits
available in xml file for EditText. You can set there all acceptable chars.
digits
EditText 的 xml 文件中有可用的值。您可以在那里设置所有可接受的字符。
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:digits="qwertyuiopasdfghjklzxcvbnm 1234567890 QWERTYUIOPASDFGHJKLZXCVBNM" />
I know, it's not the best solution but works :)
我知道,这不是最好的解决方案,但有效:)
回答by pauminku
Code from @woxingxiao works great, until you specify any inputType in your xml, for example android:inputType="textMultiLine"
.
来自@woxingxiao 的代码效果很好,直到您在 xml 中指定任何 inputType,例如android:inputType="textMultiLine"
.
I changed a little bit his proposal and I think it works great.
我稍微改变了他的提议,我认为它很有效。
public class EmojiExcludeEditText extends EditText {
private EmojiExcludeFilter emojiExcludeFilter;
public EmojiExcludeEditText(Context context) {
super(context);
init();
}
public EmojiExcludeEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public EmojiExcludeEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
if (emojiExcludeFilter == null) {
emojiExcludeFilter = new EmojiExcludeFilter();
}
setFilters(new InputFilter[]{emojiExcludeFilter});
}
@Override
public void setFilters(InputFilter[] filters) {
if (filters.length != 0) { //if length == 0 it will here return when init() is called
boolean add = true;
for (InputFilter inputFilter : filters) {
if (inputFilter == emojiExcludeFilter) {
add = false;
break;
}
}
if (add) {
filters = Arrays.copyOf(filters, filters.length + 1);
filters[filters.length - 1] = emojiExcludeFilter;
}
}
super.setFilters(filters);
}
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;
}
}
}
回答by erandac
You can write input filter for blacklist/whitelist characters
您可以为黑名单/白名单字符编写输入过滤器
public static InputFilter getEditTextFilterEmoji()
{
return new InputFilter()
{
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend)
{
CharSequence sourceOriginal = source;
source = replaceEmoji(source);
end = source.toString().length();
if (end == 0) return ""; //Return empty string if the input character is already removed
if (! sourceOriginal.toString().equals(source.toString()))
{
char[] v = new char[end - start];
TextUtils.getChars(source, start, end, v, 0);
String s = new String(v);
if (source instanceof Spanned)
{
SpannableString sp = new SpannableString(s);
TextUtils.copySpansFrom((Spanned) source, start, end, null, sp, 0);
return sp;
}
else
{
return s;
}
}
else
{
return null; // keep original
}
}
private String replaceEmoji(CharSequence source)
{
String notAllowedCharactersRegex = "[^a-zA-Z0-9@#\$%\&\-\+\(\)\*;:!\?\~`£\{\}\[\]=\.,_/\\\s'\\"<>\^\|÷×]";
return source.toString()
.replaceAll(notAllowedCharactersRegex, "");
}
};
}
Then set it as EditText filters;
然后将其设置为 EditText 过滤器;
InputFilter[] filterArray = new InputFilter[] {getEditTextFilterEmoji()}
editText.setFilters(filterArray);
回答by Gabe Sechan
There is nothing that will 100% disable emoji. The keyboard can act to any mode it sees in whatever way it thinks best, so there is no setting that will prevent emoji. If you must prevent it, do it by white or blacklisting characters with a TextWatcher.
没有什么可以 100% 禁用表情符号。键盘可以以它认为最好的任何方式在它看到的任何模式下运行,因此没有任何设置可以阻止表情符号。如果您必须阻止它,请使用 TextWatcher 将字符列入白名单或黑名单。
回答by Suraj Vaishnav
Add this emoji filter class:
添加此表情符号过滤器类:
public class EmojiFilter {
public static InputFilter[] getFilter()
{
InputFilter EMOJI_FILTER = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
for (int index = start; index < end; index++) {
int type = Character.getType(source.charAt(index));
if (type == Character.SURROGATE || type==Character.NON_SPACING_MARK
|| type==Character.OTHER_SYMBOL) {
return "";
}
}
return null;
}
};
return new InputFilter[]{EMOJI_FILTER};
}
}
And to disable emoji in edit text, do this:
要在编辑文本中禁用表情符号,请执行以下操作:
editText.setFilters(EmojiFilter.getFilter());
There were some emoji which were able to type so i added:
有一些表情符号可以输入,所以我补充说:
type==Character.NON_SPACING_MARK || type==Character.OTHER_SYMBOL
in the if condition.
在 if 条件下。
回答by user2851150
You can use input filter for removing un-wanted characters, Here I used ascci values for filtering.
您可以使用输入过滤器来删除不需要的字符,这里我使用 ascci 值进行过滤。
public class CustomEditText extends AppCompatEditText {
private InputFilter unwantedCharacterFilter;
public CustomEditText(Context context) {
super(context);
}
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
private void init() {
setFilters(new InputFilter[]{});
}
private InputFilter getUnwantedCharacterFilter() {
if (null == unwantedCharacterFilter) {
unwantedCharacterFilter = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (!TextUtils.isEmpty(source)) {
for (int index = start; index < end; index++) {
if (source.charAt(index) < 0 || source.charAt(index) > 177) {
return "";
}
}
}
return null;
}
};
}
return unwantedCharacterFilter;
}
@Override
public void setFilters(InputFilter[] filters) {
List<InputFilter> filterList = new ArrayList<>(Arrays.asList(filters));
filterList.add(getUnwantedCharacterFilter());
InputFilter specifiedFilters[] = filterList.toArray(new InputFilter[]{});
super.setFilters(specifiedFilters);
}
}
}
回答by Flavio Battocchio
Another solution:
另一种解决方案:
mEtMessageText.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS| InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
mEtMessageText.setInputType( InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS| InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
In this example it shows the ENTER button
在这个例子中,它显示了 ENTER 按钮
InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESSis equivalent to android:inputType="textEmailAddress" in XML layout
InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS相当于XML 布局中的android:inputType=" textEmailAddress"
回答by user3587677
Try android:inputtype="textUri|textMultiLine"
.
试试android:inputtype="textUri|textMultiLine"
。
In this parameter, the keyboard will change the voice input button to '/' button.
在此参数中,键盘会将语音输入按钮更改为“/”按钮。