如何从android中的edittext中选择文本?

Sus*_*gar 10 android android-widget android-emulator android-intent android-layout

我有一个edittext,其中选择了一些文本.我想在点击按钮时只从edittext中获取所选文本.

请建议我可用的链接或示例代码.

Moh*_*ikh 26

EditText et=(EditText)findViewById(R.id.edit);

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

String selectedText = et.getText().toString().substring(startSelection, endSelection);
Run Code Online (Sandbox Code Playgroud)

  • 之后,如果你想检查selectedtext是否为空.你必须使用isEmpty函数. (3认同)