【问题标题】:focusing the clicked Button in an Android Activity将单击的按钮聚焦在 Android Activity 中
【发布时间】:2012-02-16 14:33:40
【问题描述】:

我在一个 Activity 中有多个按钮。每个按钮都会打开一个单独的 AlertDialog。当应用程序启动并且我通过鼠标单击按钮时,我希望单击的按钮获得焦点..这样我之后也可以通过 D-Pad 按钮导航。但它没有,更糟糕的是,如果活动中的任何其他项目没有焦点,焦点有时会永远丢失......所以我无法使用 D-Pad 按钮导航......

我想澄清这不是因为 actionItem.requestFocus() 设置后打开的 AlertDialog .. 我通过评论对话框打开代码进行了检查太..但没有运气

我正在尝试这样做:

public void buttonClicked(View actionItem){

    //Set focus on clicked button -- but this does not work
    actionItem.requestFocus();

    switch(actionItem.getId()){
    case R.id.btnLogin:
        //Show AlertDialog 1
        break;
    case R.id.btnInfo:
        //Show AlertDialog 2
        break;
    case R.id.btnClose:
        //Show AlertDialog 3
        break;
    }
}

我希望单击的按钮在任何 AlertDialog 打开之前具有焦点。所以当 AlertDialog 关闭时,单击的按钮将具有焦点。

这个buttonClicked事件注册了多个按钮..我设置如下:

<Button
    android:id="@+id/btnClose"
    style="@style/button"
    android:onClick="buttonClicked"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:drawableLeft="@drawable/close"
    android:focusable="true"
    android:text="Exit" />

<Button
    android:id="@+id/btnInfo"
    style="@style/button"
    android:onClick="buttonClicked"                
    android:layout_marginRight="5dp"
    android:layout_toLeftOf="@+id/btnClose"
    android:layout_centerVertical="true"
    android:drawableLeft="@drawable/information"
    android:focusable="true"
    android:text="About" />

函数 buttonClicked 在每次按钮单击时被调用,但该按钮没有获得焦点...

你有什么想法吗?

【问题讨论】:

    标签: android google-tv android-button


    【解决方案1】:

    尝试不要在方法的开头调用 requestFocus(),而是在每个案例中调用它。然后你可以确定它是你设置的正确元素。

    例子:

    switch(actionItem.getId()){
    case R.id.btnLogin:
    actionItem.requestFocus()
        break;
    }
    

    这是我的最佳猜测,它对我有用。

    祝你好运:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-29
      • 1970-01-01
      • 2016-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-13
      相关资源
      最近更新 更多