【问题标题】:How to determine when text is selected [Android [EditText]]如何判断文本何时被选中[Android [EditText]]
【发布时间】:2015-02-13 21:18:47
【问题描述】:

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

我在谷歌上搜索,我 find onSelectionChanged() 方法或 setOnLongClickListener() 用于确定用户 longClicks 何时编辑文本,因此当他进行选择时,但在这两种情况下它都无法帮助我确定用户何时不是选择任何文本(我可以设置按钮不可见)...

【问题讨论】:

    标签: java android android-edittext textselection


    【解决方案1】:

    你可以使用

    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;
              }
            });
    


    应该这样做。

    【讨论】:

      【解决方案2】:
      int startSelection=et.getSelectionStart();
      int endSelection=et.getSelectionEnd();  
      

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

      【讨论】:

      • 如何每次都进行测试?我应该把它放在某个循环中?对不起,但我是 android 编程新手 :D :/
      • 这取决于您何时要检查。您可以在获得/失去焦点等时检查它。
      【解决方案3】:

      您可以检查 editText.getSelectionStart() 和 editText.getSelectionEnd() 并检查这些值是否为:

      • 相同:用户没有选择文本
      • 不同:用户选择的文本

      所以:

      int startSelection= textAbove.getSelectionStart();
      int endSelection= textAbove.getSelectionEnd();
      
      if (startSelection != endSelection) {
          ... DO SOMETHING ...
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多