简体   繁体   中英

How to get selected text from edittext in android?

I have a edittext in which some text are selected. I want to get only selected text from edittext on click of button.

Please suggest me usable link or sample code.

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

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

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

Kotlin

val startSelection: Int = editText.selectionStart
val endSelection: Int = editText.selectionEnd

val selectedText: String = editText.text.substring(startSelection, endSelection)
getSelectionStart()
getSelectionEnd()

see this link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM