在 Android Activity 中聚焦单击的按钮

发布于 2025-01-06 09:38:30 字数 1607 浏览 5 评论 0原文

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

我想澄清这不是因为设置 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 ,但该按钮没有获得焦点。 有什么想法吗

I have multiple buttons in an Activity. Each button opens a separate AlertDialog. When the app starts and i CLICK on a button through mouse, i want the clicked button to get focus .. so i can navigate through D-Pad buttons too afterwards. But it does not and , even worse, the focus sometimes is lost forever if any other item in the activity had no focus... so i cannot navigate using D-Pad buttons...

I want to clarify that this is NOT because of the AlertDialog that is opened after actionItem.requestFocus() is set .. I checked by commenting the Dialog opening code too .. but no luck

I am trying to do it as follows:

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;
    }
}

I want the clicked button to have focus before any AlertDialog opens.. so when the AlertDialog is closed, the clicked button will have focus.

this buttonClicked event is registered with multiple buttons .. i set it as follows:

<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" />

the function buttonClicked is called on each button'c click but that button does not get focus...

any idea y?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

百善笑为先 2025-01-13 09:38:30

尝试不要在方法开始时调用 requestFocus() ,而是在每种情况下调用它。然后您就可以确定它是您设置的焦点的正确元素。

示例:

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

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

祝你好运 :)

Try instead of calling requestFocus() in start of the method, call it inside each case. Then you can be sure that its the right element you set in focus.

Example:

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

That's my best guess, it works for me.

Good Luck :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文