专注于最近的可聚焦兄弟姐妹

时间:2019-09-17 13:52:00

标签: android android-layout focus android-tablelayout

在AndroidTV上的应用中,我有TableLayoutTableRow。这些行包含Button。当用户点击按钮时,该按钮被禁用。发生这种情况时,我希望焦点应移至TableLayout中最接近的焦点视图。

布局是这样的:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:shrinkColumns="*"
    android:stretchColumns="*">
    <TableRow
        android:id="@+id/input_buttons_row"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_width="match_parent">

        <Button
            android:id="@+id/it1"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@drawable/selection_background"
            android:focusable="true" />

        <Button
            android:id="@+id/it2"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@drawable/selection_background"
            android:focusable="true" />

        <Button
            android:id="@+id/it3"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@drawable/selection_background"
            android:focusable="true" />

        <Button
            android:id="@+id/it4"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@drawable/selection_background"
            android:focusable="true" />

        <Button
            android:id="@+id/it5"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@drawable/selection_background"
            android:focusable="true" />

        <Button
            android:id="@+id/it6"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@drawable/selection_background"
            android:focusable="true" />

        <Button
            android:id="@+id/it7"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@drawable/selection_background"
            android:focusable="true" />
    </TableRow>
</TableLayout>

显示如下: enter image description here

我在按下按钮时禁用了该按钮。因此,我希望焦点应移至视图中最近的按钮。示例:如果我按下“按钮5”,则焦点应转到“按钮4”或“按钮6”。我该如何实现?

我尝试了将请求集中在表行上的各种组合。示例:

mInputButtonsRow.requestFocus(View.FOCUS_DOWN);

也尝试过requestFocus(View.FOCUS_BACKWARD)requestFocus(View.FOCUS_FORWARD)。焦点跳到该行的第一个或最后一个按钮,而不是最近的可聚焦按钮! 但是,如果焦点仍处于启用状态,则始终将焦点放在“按钮1”上。如果不是,则转到“按钮2”,依此类推。我如何才能将注意力集中在最近的按钮上,而不是一直在第一个启用的同级按钮上?

1 个答案:

答案 0 :(得分:0)

我尝试了几种方法,但无法做到。因此,这是对我有用的解决方法。这可能不是最好的方法。我仍然对更好的解决方案持开放态度。这是我所做的: 当用户点击按钮时,

// Keep the focus on the same button.
mButton1.requestFocus();

// set it to clickable=false
mButton1.setClickable(false);

// Change the background of the button to the disabled button color.
mButton1.setBackground(getDrawable(R.drawable.disabled_button_background));

//Set a FocusChangeListener on the button to disable the button if the focus is changed from it.
mButton1.setOnFocusChangeListener(getButtonFocuslistener());

FocusChangeListener如下:

private View.OnFocusChangeListener getButtonFocuslistener() {
        return new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                v.setEnabled(false);
            }
        };
    }

现在发生的事情: 当用户单击按钮时,焦点仍保留在按钮上,但是其颜色已更改,因此看起来像已禁用。由于在其上设置了clickable = false,因此用户无法再次单击它。当用户将焦点移到其他任何视图时,此按钮将被禁用,因此无法再次获得焦点。因此,我可以实现我想要的。