能否确定文本何时被选中,何时未被选中?
我在谷歌上搜索并找到了onSelectionChanged()
方法或setOnLongClickListener()
来确定用户是否longClick
编辑框以进行选择,但是在这两种情况下都无法帮助我确定用户是否没有选择任何文本(我可以将按钮设置为不可见)...
能否确定文本何时被选中,何时未被选中?
我在谷歌上搜索并找到了onSelectionChanged()
方法或setOnLongClickListener()
来确定用户是否longClick
编辑框以进行选择,但是在这两种情况下都无法帮助我确定用户是否没有选择任何文本(我可以将按钮设置为不可见)...
标签
。yourEditText.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (MotionEvent.ACTION_UP == event.getAction()) {
if (yourEditText.hasSelection()) {
// if true, the text in the EditText is selected
}
return false;
}
});
那就可以了。
int startSelection=et.getSelectionStart();
int endSelection=et.getSelectionEnd();
< p> getSelectionStart()
方法返回选择锚点/光标的起始位置,如果用户没有选择任何文本,则返回-1。您可以尝试使用此方法。
您可以检查editText.getSelectionStart()和editText.getSelectionEnd()并检查这些值是否为:
因此:
int startSelection= textAbove.getSelectionStart();
int endSelection= textAbove.getSelectionEnd();
if (startSelection != endSelection) {
... DO SOMETHING ...
}