我有一个EditText与一些虚拟文本在其中。当用户点击它时,我希望它被选中,以便当用户开始输入虚拟文本被删除。

我怎样才能做到这一点呢?


当前回答

通过调用程序上的焦点和选择来管理它

{
editabletext.requestFocus();
editabletext.selectAll();
}

其他回答

你为什么不试试android:hint="hint"来给用户提供提示!!

当用户单击编辑文本框时,“提示”将自动消失。 这是最好的解决办法。

editText.setSelectAllOnFocus(true);

如果您想以编程的方式进行,这是可行的。

只需将其添加到.xml文件中的editText中

android:selectAllOnFocus=“true”

通过调用程序上的焦点和选择来管理它

{
editabletext.requestFocus();
editabletext.selectAll();
}

这就像一个魅力:

XML

android:selectAllOnFocus="true"

Java

editText.setSelectAllOnFocus(true);

了解更多在TextView - android:selectAllOnFocus