简体   繁体   English

触摸外部时按钮失去焦点

[英]Button looses focus when touched outside

I have a button as an itemview in recyclerview which has both background and text selectors. 我在recyclerview中有一个按钮作为itemview,它同时具有背景和文本选择器。

<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/channel"
android:layout_width="wrap_content"
android:layout_height="32dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="@drawable/channel_bg"
android:focusableInTouchMode="true"
android:gravity="center"
android:textColor="@color/channel_text_selector"
android:textSize="12sp" />

Here is my @drawable/channel_bg 这是我的@ drawable / channel_bg

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true">
    <shape android:shape="rectangle">
        <solid android:color="@color/colorPrimary" />
        <corners android:radius="18dp" />
    </shape>
</item>
<item android:state_pressed="true">
    <shape android:shape="rectangle">
        <solid android:color="@color/colorPrimary" />
        <corners android:radius="18dp" />
    </shape>
</item>
<item>
    <shape android:shape="rectangle">
        <stroke android:width="1dp" android:color="@color/colorPrimary" />
        <solid android:color="@android:color/white" />
        <corners android:radius="18dp" />
    </shape>
</item>

Here is my @color/channel_text_selector 这是我的@ color / channel_text_selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@android:color/white" android:state_pressed="true" />
<item android:color="@android:color/white" android:state_selected="true" />
<item android:color="@android:color/white" android:state_focused="true" />
<item android:color="@color/colorPrimary" />

When I touch/click on any other view on the same screen, button looses it's focus and goes in unselected state. 当我触摸/单击同一屏幕上的任何其他视图时,按钮将失去其焦点并处于未选中状态。 Please help me on the same. 请同样帮我。

Your button loses focus as it's default behavior, if you don't touch it it's not focused. 您的按钮会失去焦点,因为它是默认行为,如果您不触摸它,它就不会成为焦点。 If you want your button to be focused after you click on other views you should request focus programmatically like this in OnFocusChangeListener 如果您希望在单击其他视图后将按钮聚焦,则应在OnFocusChangeListener编程方式请求聚焦

 btn.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean b) {
            btn.requestFocus();
        }
    });

So when the button gets "unfocused" the focus will be still requested but there's a caveat. 因此,当按钮“未聚焦”时,仍会请求聚焦,但有一个警告。 You'll need to remove it when you need it too. 您也需要在需要时将其删除。 Or it'll get stuck focused. 否则会陷入困境。

button.clearFocus();

You should also keep a boolean variable (as it was suggested in the comments) and then in case your recycler gets re-drawn check the boolean value and request focus or remove it later if you like 您还应该保留一个布尔变量(如注释中所建议),然后在重新绘制回收站的情况下,检查布尔值并请求焦点,或者如果需要,可以稍后将其删除

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM