Подтвердить что ты не робот

Как кнопка может получить фокус?

Есть довольно много сообщений, затрагивающих эту тему. Я думал, что должен задать этот простой вопрос, надеясь прояснить это.

Мне не удается установить фокус на кнопке. Я знаю, что я, наверное, пропустил нечто фундаментальное. Вот простой макет:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    android:focusable="true" />

 </LinearLayout>

Ниже приведен простой код в onCreate():

        Button button = (Button)findViewById(R.id.button1);
        button.setFocusable(true);
        button.requestFocus();
        button.setText("Debug");  //Just to show the code here has been executed

Он просто не работает (т.е. кнопка не получает фокуса).

Будет оценена любая коррекция моей ошибки или недоразумения.

4b9b3361

Ответ 1

обновите свой код:

        Button button = (Button)findViewById(R.id.button1);
        button.setFocusable(true);
        button.setFocusableInTouchMode(true);///add this line
        button.requestFocus();
        button.setText("Debug");