为 android 创建自定义输入类型

发布于 2024-10-30 19:40:51 字数 174 浏览 7 评论 0原文

我的要求是,用户应该只能输入数字 0 到 9 ,并且每 4 个字符之后,会自动在 edittext 中附加一个“-”符号。用户不应该能够删除 edittext 的任何部分除了最后。请建议锄头这样做。

用户不应该能够将光标移动到键入文本中间的任何位置,并且能够删除它。如何做到这一点?当用户移动光标位置时会调用什么事件?

my requirement is that, the user should only be able to type in digits 0 throu 9 , and that after every 4 characters, a "-" sign is automatically appended to the edittext.user should not be able to delete any part of the edittext except at end. Please suggest hoe to do this.

The user should not be able to move the cursor to anywhere in the middle of the typed text, and be able to delete it. How to accomplish this? What event is called when the user moves the position of the cursor?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘你却要生生世世 2024-11-06 19:40:51

您的第一个要求是 0-9 仅通过在 XML 用户类型数字中设置编辑文本属性来满足
并计算编辑文本中的文本,在编辑文本对象中设置文本观察器侦听器并计算单词,您可以在其中附加“-”字符。

your first requirement is 0-9 is fulfill by set edit text property in XML user type number only
and to count your text in edit-text set text watcher listener in edit text object and count word and there you can append "-" character.

混浊又暗下来 2024-11-06 19:40:51

我找到了解决问题的方法:用户不应将光标移动到键入文本中间的任何位置。我们需要扩展 EditText 并添加覆盖以下函数:

@Override
protected void onSelectionChanged(int selStart, int selEnd) {
    // TODO Auto-generated method stub
    // this method will check if the cursor is moved. if yes then bring back
    // the cursor to the end so that the user cannot delete anythign from
    // the middle of the text(here sub id). Any editing will only be
    // possible at the end of the text
    if (selStart == selEnd) {
        int length = getText().toString().length();
        setSelection(length);
    }
    super.onSelectionChanged(selStart, selEnd);
}

I found a solution to my problem : user should not be able to move the cursor to anywhere in the middle of the typed text. We need to extend EditText and add override the following function:

@Override
protected void onSelectionChanged(int selStart, int selEnd) {
    // TODO Auto-generated method stub
    // this method will check if the cursor is moved. if yes then bring back
    // the cursor to the end so that the user cannot delete anythign from
    // the middle of the text(here sub id). Any editing will only be
    // possible at the end of the text
    if (selStart == selEnd) {
        int length = getText().toString().length();
        setSelection(length);
    }
    super.onSelectionChanged(selStart, selEnd);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文