9

Tengo un texto de edición en el que se selecciona texto. Quiero obtener solo el texto seleccionado de edittext al hacer clic en el botón.¿Cómo obtener el texto seleccionado de edittext en android?

Por favor sugiérame un enlace utilizable o código de muestra.

+0

[Buscar] (http://stackoverflow.com/a/4531500/420015) – adneal

+1

[un vistazo a esta respuesta] (http: // stackoverflow. com/a/8909122/593709) –

Respuesta

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

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

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

después, si desea verificar si el texto seleccionado está vacío o no. debes usar la función de Vacío. –

+0

trim() también lo hará. –

0
getSelectionStart() 
getSelectionEnd() 

ver este link

Cuestiones relacionadas