相关疑难解决方法(0)

按钮如何获得焦点?

有很多帖子涉及这个话题.我想我应该问这个简单的问题,希望澄清一下.

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

<?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>
Run Code Online (Sandbox Code Playgroud)

以下是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
Run Code Online (Sandbox Code Playgroud)

它根本不起作用(即按钮没有得到焦点).

任何纠正我的错误或误解将不胜感激.

android button

16
推荐指数
1
解决办法
3万
查看次数

标签 统计

android ×1

button ×1