在编辑文本中添加退格键

发布于 2024-11-11 17:30:02 字数 480 浏览 0 评论 0原文

我有一个简单的活动。只有一个编辑文本和一个按钮。在编辑文本中写入一些文本后,如果我按下按钮,我想删除文本的最后一个字符。 我尝试过这样的操作:

String backSpace = txtMsg.getText().toString();
    if(!backSpace.equals(""));
        String mystring=backSpace.substring(0, txtMsg.length()-1)); 
txtMsg.setText("");
txtMsg.append(mystring);

它工作正常,但我想通过移动光标在最后一个位置手动附加退格字符,最后在任何位置附加退格字符(通过 txtMsg.setSelection()); 就像我们在文本末尾附加任何字符一样:

txtMsg.append("C");

我想知道用什么来代替 C 来附加退格键?请帮助 提前致谢。

I have a simple activity. Only one edittext and one button.After writing some text in the edittext if I press the button I want to delete the last character of the text.
I have tried like this:

String backSpace = txtMsg.getText().toString();
    if(!backSpace.equals(""));
        String mystring=backSpace.substring(0, txtMsg.length()-1)); 
txtMsg.setText("");
txtMsg.append(mystring);

It works fine but I want to manually append the backspace character at last position and finally at any position by moving the cursor(by txtMsg.setSelection());
Like we append any character to the end of the text:

txtMsg.append("C");

I want to know what will be in place of C for appending the backspace?Please Help
Thanks in advance.

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

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

发布评论

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

评论(3

守护在此方 2024-11-18 17:30:02

我不确定您是否仍然需要这个问题的答案,但我最近有与您相同的需求,并且我在可编辑 android 类型上使用了删除方法:

http://developer.android.com/reference/android/text/Editable.html#delete(int,%20int)

我有一个密码 EditText,并在“删除”按钮的单击处理程序中执行了以下操作:

        // delete
    if (m_userPass.getText().length() > 0) {
        m_userPass.getText().delete(m_userPass.getText().length() - 1,
                m_userPass.getText().length());
    }

删除方法采用两个参数,即要删除的文本的开始位置和结束位置。在我的示例中,我只是让删除按钮删除最后一个字符(无论光标如何)。如果您想变得更有趣,您可以输入逻辑来根据光标位置计算出开始和结束位置。

开始位置必须在结束位置之前(我一开始不小心犯了这个错误)。并且起始位置必须大于或等于零。

I am not sure if you still need an answer to this or not, but I recently had the same need as you and I used the delete methond on the Editable android type:

http://developer.android.com/reference/android/text/Editable.html#delete(int,%20int)

I had a password EditText and did the following in the click handler for the 'delete' button:

        // delete
    if (m_userPass.getText().length() > 0) {
        m_userPass.getText().delete(m_userPass.getText().length() - 1,
                m_userPass.getText().length());
    }

The delete method takes two arguments, the start and end position of the text to delete. In my example, I am just making the delete button delete the last character (regardless of the cursor). If you wanted to get fancier, you could put in logic to figure out the start and end position based on the cursor position.

The start position has to come before the end position (I accidentally made that mistake at first). And the start position has to be greater than or equal to zero.

幼儿园老大 2024-11-18 17:30:02

有一种简单的方法可以通过以下方式完成您想做的事情:

KeyEvent event = new KeyEvent(0, 0, 0, KeyEvent.KEYCODE_DEL, 0, 0, 0, 0, KeyEvent.KEYCODE_ENDCALL);
input.dispatchKeyEvent(event);

there is a simple way of doing what you want via:

KeyEvent event = new KeyEvent(0, 0, 0, KeyEvent.KEYCODE_DEL, 0, 0, 0, 0, KeyEvent.KEYCODE_ENDCALL);
input.dispatchKeyEvent(event);
长梦不多时 2024-11-18 17:30:02

你可以试试这个,它对我有用:

if(!(text.length()==0))
    text.setText(text.getText().delete(text.length() - 1, text.length()));

You can try this, it worked for me:

if(!(text.length()==0))
    text.setText(text.getText().delete(text.length() - 1, text.length()));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文