如何确定文本何时被选中 [Android [EditText]]

Mr.*_*bot 2 java android textselection android-edittext

是否可以确定何时选择文本,何时不选择?

我在谷歌搜索和我的find onSelectionChanged()方法或setOnLongClickListener()确定用户longClick何时选择 editText 所以当他进行选择时,但在这两种情况下,它都无法帮助我确定用户何时未选择任何文本(我可以将按钮设置为不可见)。 .

Hed*_*deH 6

您可以使用

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;
          }
        });
Run Code Online (Sandbox Code Playgroud)


那应该这样做。


Lit*_*ild 5

int startSelection=et.getSelectionStart();
int endSelection=et.getSelectionEnd();  
Run Code Online (Sandbox Code Playgroud)

getSelectionStart()如果用户没有选择任何文本,该方法将返回选择锚点/光标的开始或 -1。你可以试试用这个。