¿Cómo configurar el foco en un widget de botón mediante programación?


¿Es posible establecer un foco en un widget de botón que se encuentra en algún lugar de mi diseño? onCreate de la actividad mi control/enfoque debe estar en ese botón programáticamente.

Author: Jonik, 2010-07-13

2 answers

Sí es posible.

Button myBtn = (Button)findViewById(R.id.myButtonId);
myBtn.requestFocus();

O en XML

<Button ...><requestFocus /></Button>

Nota importante: El widget de botón debe ser focusable y focusableInTouchMode. La mayoría de los widgets son focusable pero no focusableInTouchMode por defecto. Así que asegúrese de establecerlo en code

myBtn.setFocusableInTouchMode(true);

O en XML

android:focusableInTouchMode="true"
 162
Author: Pentium10,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-12-04 17:07:11

Prueba esto:

btn.requestFocusFromTouch();
 1
Author: Zan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-05-19 05:15:21