【问题标题】:How to clear the selection in an EditText?如何清除 EditText 中的选择?
【发布时间】:2012-07-17 01:20:07
【问题描述】:

如何以编程方式从EditText 中删除选择?我试过了:

myEditText.setSelection(selectionEnd);

但这只会在当前选择的末尾创建一个长度为 0 的选择。我也试过Selection.removeSelection(mySpannable),它只是在文本的开头放置了一个空选择。

【问题讨论】:

  • 你想从编辑文本中清除文本
  • 不,只是取消文本选择突出显示
  • 你得到答案了吗?

标签: android android-edittext textselection


【解决方案1】:

致电myEditText.clearFocus();。我想这就是你需要的

【讨论】:

  • 这也隐藏了光标。如果我想删除选择但保留闪烁的光标怎么办?
【解决方案2】:

要将选择移到顶部:

edit_text.setSelection(0);

将选择移至底部:

edit_text.setSelection(edit_text.getText().length());

【讨论】:

    【解决方案3】:
    EditText message = (EditText) findViewById(R.id.sendText);
    String text = message.getText().toString();
    message.setText(null);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-26
      • 2019-07-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多