【问题标题】:How to get selected text from edittext in android?如何从android中的edittext中获取选定的文本?
【发布时间】:2012-04-21 16:21:34
【问题描述】:

我有一个编辑文本,其中选择了一些文本。我只想在单击按钮时从 edittext 中获取选定的文本。

请给我推荐可用的链接或示例代码。

【问题讨论】:

标签: android android-layout android-intent android-emulator android-widget


【解决方案1】:
EditText et=(EditText)findViewById(R.id.edit);

int startSelection=et.getSelectionStart();
int endSelection=et.getSelectionEnd();

String selectedText = et.getText().toString().substring(startSelection, endSelection);

【讨论】:

  • 如果你想检查 selectedtext 是否为空,在它之后。你必须使用 isEmpty 函数。
【解决方案2】:
getSelectionStart()
getSelectionEnd()

看到这个link

【讨论】:

    【解决方案3】:

    科特林

    val startSelection: Int = editText.selectionStart
    val endSelection: Int = editText.selectionEnd
    
    val selectedText: String = editText.text.substring(startSelection, endSelection)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-25
      • 1970-01-01
      • 2012-05-25
      • 2014-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多