在edittext中追加退格edittext

由网友(连名带姓最初的样子i)分享简介:我有一个简单的活动.只有一个编辑文本和一个按钮.在编辑文本中写入一些文本后,如果我按下按钮,我想删除文本的最后一个字符.我试过这样:I have a simple activity. Only one edittext and one button.After writing some text in the edi...

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

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);

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

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");

我想知道用什么代替 C 来添加退格键?请帮忙提前致谢.

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

推荐答案

我不确定你是否还需要这个问题的答案,但我最近和你有同样的需求,我在 Editable 上使用了删除方法安卓类型:

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)

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

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());
    }

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

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.

阅读全文

相关推荐

最新文章