编辑文本,在文本更改时移除单词。

huangapple go评论56阅读模式
英文:

Edit Text Remove Word on text Changed

问题

我有一个类似下面的EditText:

    <com.myapp.example.customview.socialView.SocialEditText
        android:id="@+id/edt_des"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:afterTextChanged="@{(editable) -> viewModel.onDescriptionTextChanged(editable)}"
        android:background="@drawable/bg_white_corner_15"
        android:backgroundTint="@color/colorPrimaryDark"
        android:elevation="5dp"
        android:fontFamily="@font/popins_reguler"
        android:gravity="top"
        android:hint="@string/upload_description"
        android:maxLength="175"
        android:padding="10dp"
        android:textColor="@color/light_white"
        android:textColorHint="@color/color_text_light"
        android:textCursorDrawable="@xml/cursor"
        android:textSize="12dp"
        app:socialFlags="hashtag" />

在ViewModel中的函数中获取文本,如下所示:

public void onDescriptionTextChanged(CharSequence text) {
    postDescription = text.toString();
    position.set(postDescription.length());
}

我有一个可以从字符串中删除单词的函数,如下所示:

public void main(String[] args) {
    String word = postDescription;
    String remove = "#WordForRemove";
    finalDescription = removeWords(word, remove);
}

public static String removeWords(String word ,String remove) {
    return word.replace(remove,"");
}

我想在onDescriptionTextChanged中使用这个函数,这样我就可以从编辑框中删除不可接受的单词。如果这里有人可以帮助我实现它,请告诉我。

谢谢!


<details>
<summary>英文:</summary>

I have edittext like below

    &lt;com.myapp.example.customview.socialView.SocialEditText
        android:id=&quot;@+id/edt_des&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        android:afterTextChanged=&quot;@{(editable)-&gt;viewModel.onDescriptionTextChanged(editable)}&quot;
        android:background=&quot;@drawable/bg_white_corner_15&quot;
        android:backgroundTint=&quot;@color/colorPrimaryDark&quot;
        android:elevation=&quot;5dp&quot;
        android:fontFamily=&quot;@font/popins_reguler&quot;
        android:gravity=&quot;top&quot;
        android:hint=&quot;@string/upload_description&quot;
        android:maxLength=&quot;175&quot;
        android:padding=&quot;10dp&quot;
        android:textColor=&quot;@color/light_white&quot;
        android:textColorHint=&quot;@color/color_text_light&quot;
        android:textCursorDrawable=&quot;@xml/cursor&quot;
        android:textSize=&quot;12dp&quot;
        app:socialFlags=&quot;hashtag&quot; /&gt;

and in viewmodel function for get text like below

    public void onDescriptionTextChanged(CharSequence text) {
        postDescription = text.toString();
        position.set(postDescription.length());
    }

I have one function which can remove the word from String like below

    public void main(String[] args) {
        String word = postDescription;
        String remove = &quot;#WordForRemove&quot;;
        finalDescription = removeWords(word, remove);
    }
    
    public static String removeWords(String word ,String remove) {
        return word.replace(remove,&quot;&quot;);
    }

I want to use this function with onDescriptionTextChanged so I can remove unacceptable words from edit text, Let me know if anyone here can help me for achieving it. 

Thanks!


</details>


# 答案1
**得分**: 0

**关于问题:**
- 你并没有说明SocialEditText视图是什么。它看起来像是你的应用中的自定义类,因此在StackOverflow上没有人真正知道它的功能。我会假设它只是一个EditText,但如果该类有任何逻辑,它可能会影响你尝试做的事情。
- onDescriptionTextChanged不是Android中的标准方法。当然,你可以有一个叫做这个名字的方法,但Android不会特别识别它。

**回答:**
你只是想在验证文本时删除这些词。这将允许你检查是否有任何更改,并可能提示用户进行更改。你可以在你的应用程序接受输入时运行你的函数 - 你可能有一个按钮之类的东西来实现这一点(但你没有指定)。

你不想在用户输入时删除文本,原因如下:
- 作为用户,在你输入时让应用程序改变你的输入是非常恼人的。
- 它将阻止用户输入以与不可接受的单词相同的字符开头的可接受单词。如果应用程序在你输入这些字符时立即将其删除,你将无法触摸输入“cumulative”(累积)(或者至少你必须对此进行狡猾处理)。
- 如果在用户输入时删除文本,你就是在与键盘及其组合范围作斗争。这在CJK语言中特别糟糕,但在字母键盘上也非常恼人。许多键盘不处理这种情况。

请注意,从技术上讲,通过在EditText上使用addAfterTextChangedListener方法运行你的删除代码来在用户输入时删除内容是可能的。但正如上面所述,这会导致非常糟糕的用户体验。

最后,请记住,对于公共网站来说,一个不良词汇列表并不是真正过滤语言的有效方法:它不能阻止用户编写粗俗语言等。人类很容易通过更改字符、混用大小写、添加符号、使用Unicode相似字符等来绕过这一点。

<details>
<summary>英文:</summary>

**On the question&#160;:**
- You have not specified what the SocialEditText view is. It looks like a custom class in your app, so nobody on StackOverflow can really know what it does. I will assume it&#39;s just an EditText, but if that class has any logic it may cause issues with what you are trying to do.
- onDescriptionTextChanged is not a standard method in Android. Of course you can have a method with this name, but Android won&#39;t particularly pick it up.


**Answer&#160;:**\
You simply want to remove the words when you validate the text. This will let you check whether there were any changes and possibly prompt the user about the changes. You can just run your function whenever you app accepts the input&#160;– you probably have a button for this or something (but you did not specify).


You do not want to remove text while the user is typing for the following reasons&#160;:
- As a user, it is very annoying to have the app change what you type while you&#39;re typing it.
- It will prevent your users to type acceptable words that happen to start with the same characters as an unacceptable word. You can&#39;t touch-type &quot;cumulative&quot; if the app removes these three first chars as soon as you type them (or at least you have to be cunning about it).
- If you remove text while they are typing, you&#39;re fighting against the keyboard and its composition span. This is particularly bad in CJK languages, but is also very annoying with alphabetic keyboards. Many keyboards don&#39;t handle this well.

Note that technically it&#39;s possible to remove stuff as users are typing using the addAfterTextChangedListener method on the EditText to run your removing code. But as outlined above that will make for a very, very bad user experience.


Finally, keeep in mind that a list of bad words is not really an effective way of filtering language for a public site&#160;: it doesn&#39;t prevent users to write profanity and the like. It&#39;s very easy for humans to work around this by changing a character, mixing up case, adding symbols, using unicode look-alikes and the like.


</details>



huangapple
  • 本文由 发表于 2020年9月5日 09:51:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63749771.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定