追加退格中的EditText [英] append backspace in edittext

查看:187
本文介绍了追加退格中的EditText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的活动。只有一个的EditText和一个button.After写在EditText上一些文字,如果我preSS我想删除文本的最后一个字符的按钮。
我曾尝试这样的:

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.

推荐答案

我不知道,如果你仍然需要一个答案或没有,但最近我同样需要你和我用在可编辑删除methond Android的类型:

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:

<一个href=\"http://developer.android.com/reference/android/text/Editable.html#delete%28int,%20int%29\">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());
    }

删除方法有两个参数,文本的开始和结束位置删除。在我的例子,我只是做了删除按钮删除最后一个字符(不管光标)。如果你想更大胆的尝试,你可以把逻辑基础上的光标位置找出开始和结束位置。

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.

这篇关于追加退格中的EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆