我有一个EditText与一些虚拟文本在其中。当用户点击它时,我希望它被选中,以便当用户开始输入虚拟文本被删除。
我怎样才能做到这一点呢?
我有一个EditText与一些虚拟文本在其中。当用户点击它时,我希望它被选中,以便当用户开始输入虚拟文本被删除。
我怎样才能做到这一点呢?
当前回答
editText.setSelectAllOnFocus(true);
如果您想以编程的方式进行,这是可行的。
其他回答
这就像一个魅力:
XML
android:selectAllOnFocus="true"
Java
editText.setSelectAllOnFocus(true);
了解更多在TextView - android:selectAllOnFocus
您可以在main.xml文件中尝试:
android:selectAllOnFocus="true"
或者,在Java中,使用
editText.setSelectAllOnFocus(true);
EditText dummy = ...
// android.view.View.OnFocusChangeListener
dummy.setOnFocusChangeListener(new OnFocusChangeListener(){
public void onFocusChange(View v, boolean hasFocus){
if (hasFocus) && (isDummyText())
((EditText)v).selectAll();
}
});
我知道你已经找到了一个解决方案,但真正正确的方法来做你所要求的只是使用android:提示属性在你的EditText。该文本在框为空且未聚焦时显示,但在选择EditText框时消失。
只需将其添加到.xml文件中的editText中
android:selectAllOnFocus=“true”