Android 按钮如何获得焦点?

Android 按钮如何获得焦点?,android,button,Android,Button,有相当多的帖子涉及这个话题。我想我应该问这个简单的问题,希望能澄清这一点 我无法将焦点设置在按钮上。我知道我可能错过了一些基本的东西。以下是简单的布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:lay

有相当多的帖子涉及这个话题。我想我应该问这个简单的问题,希望能澄清这一点

我无法将焦点设置在按钮上。我知道我可能错过了一些基本的东西。以下是简单的布局:

<?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>
它根本不起作用(即按钮无法获得焦点)

如能更正我的错误或误解,我们将不胜感激。

请更新您的代码:

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

按钮也会有焦点,但你看不到。视觉焦点仅限于感谢大家的及时回复和澄清。显然,我不理解“焦点”在特定语境中的含义。现在我想知道一个按钮在没有显示的情况下获得焦点有什么效用。假设你的设备上连接了一个键盘(就像emulator一样),或者你有一个带有d-pad的设备,然后你点击回车/点击dpad。任何有焦点的都叫现在。如果你只是用手指使用你的应用程序,那么你通常可以忽略焦点。谢谢zapl的解释。我必须承认,我仍然很难理解这种行为。如果我使用键盘,我个人更喜欢看到视觉反馈,告诉我哪个小部件是焦点。当我在模拟器上的另一个带有按钮和编辑文本的布局上尝试此操作时,我可以通过使用tab键获得一个接收焦点的按钮(橙色背景),但不是在代码中(在使用setFocusableInTouchMode(true)之前)。但是,我可以在代码中使用requestFocus()直观地获取焦点中的edittext,而不使用setFocusableInTouchMode(true)。对我来说有点困惑。@Hong:幸运的话……我也面临着同样的问题……在我的情况下,它有时会表现出专注,有时则不会。
        Button button = (Button)findViewById(R.id.button1);
        button.setFocusable(true);
        button.setFocusableInTouchMode(true);///add this line
        button.requestFocus();
        button.setText("Debug");