禁用焦点以进行背景布局

时间:2018-03-27 08:10:52

标签: android layout focusable

单击按钮显示设置框。我使用这段代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="be.mforgetngroup.radioplayer.MainMenuFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

 <LinearLayout android:id="@+id/main_menu_buttons">
</LinearLayout>

    <RelativeLayout android:id="@+id/settings_box"
        style="@style/settings_box">
        <LinearLayout style="@style/settings">
            <TextView
                android:text="@string/settings"
                style="@style/settings_title" />
            <RadioGroup
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end">
                <RadioButton
                    android:text="@string/dark_theme"
                    style="@style/settings_options" />
                <RadioButton
                    android:text="@string/light_theme"
                    style="@style/settings_options"/>
            </RadioGroup>
        </LinearLayout>
    </RelativeLayout>

    <ImageButton
        android:id="@+id/settings_button"
        style="@style/settings_button" />

</RelativeLayout>

&#34; main_menu_buttons&#34;中有按钮。我想禁用除设置框之外的所有屏幕项目(如果此项目可见)。现在,焦点可以从设置框转到main_menu的按钮。我不想要那个。 有没有办法做到这一点,而不是逐个禁用每个项目?

以下是显示/隐藏设置框的代码

                View settingsBox = getView().findViewById(R.id.settings_box);
                if(settingsBox.getVisibility() == View.GONE){
                    settingsBox.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.fade_in));
                    settingsBox.setVisibility(View.VISIBLE);
                }
                else{
                    settingsBox.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.fade_out));
                    settingsBox.setVisibility(View.GONE);
                }

谢谢

1 个答案:

答案 0 :(得分:0)

我会将匹配父视图中的“设置”框包装起来,当我显示该视图的设置框setOnClickListener时,不执行任何操作会阻止其下方的任何单击。

<RelativeLayout android:id="@+id/stop_clicks"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

  <RelativeLayout android:id="@+id/settings_box"
    style="@style/settings_box"
    centerInParent="true">
    <LinearLayout style="@style/settings">
        <TextView
            android:text="@string/settings"
            style="@style/settings_title" />
        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end">
            <RadioButton
                android:text="@string/dark_theme"
                style="@style/settings_options" />
            <RadioButton
                android:text="@string/light_theme"
                style="@style/settings_options"/>
        </RadioGroup>
    </LinearLayout>
  </RelativeLayout>
</RelativeLayout>

       View settingsBox = getView().findViewById(R.id.settings_box);
       View stopClicks = getView().findViewById(R.id.stop_clicks);
            if(settingsBox.getVisibility() == View.GONE){
                settingsBox.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.fade_in));
                settingsBox.setVisibility(View.VISIBLE);
stopClicks.setVisibility(View.VISIBLE);
stopClicks.setOnClickListener(v -> null);
            }
            else{
                settingsBox.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.fade_out));
                settingsBox.setVisibility(View.GONE);
stopClicks.setVisibility(View.GONE);
stopClicks.setOnClickListener(null);
            }