之前的EditText光标位置清理整个文本 [英] Clearing the entire text before cursor position on EditText

查看:137
本文介绍了之前的EditText光标位置清理整个文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要清除的EditText整个文本是在光标位置之前。假设我文字的 1234567890 光标在字符后4 这样的 1234 | 567890 现在我的要求是我有一个自定义按钮,这之前删除光标位置的文本。

I want to clear the entire text on EditText which is before the cursor position. Suppose i the text is 1234567890, the cursor is after the character 4 like this 1234|567890 Now my requirement is I have a custom button, which deletes the text before cursor position.

我使用 editext.getText()清(); 来清除文本,但它的清除所有文本。如果光标在文字的最后,它还是不错的。

I am using editext.getText().clear(); to clear the text, but its clearing entire text. If the cursor is at end of text, it is good.

是否有可能实现我的要求吗?如果是的话怎么样?请帮我对此。

Is it possible to achieve my requirement ? If yes how ? Please help me regarding this.

推荐答案

下面是如何对付它:

您将使用光标位置获得:

You will get Cursor Position using:

editText.getSelectionStart();

editText.getSelectionEnd();

注:如果没有选择文本,两种方法将返回相同指数

然后子串文字的EditText
然后再设置为的EditText 。是这样的:

Then sub-string the text EditText and then set again to EditText. something like this:

int pos = editText.getSelectionStart();
String myText = editText.getText().toString();
//sub-string it.
String subStringed = myText.substring(pos, myText.length());
//set it again..
editText.setText(subStringed);

这篇关于之前的EditText光标位置清理整个文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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