1

我通过单击一个按钮来显示一个设置框。我使用这段代码:

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

“main_menu_buttons”中有按钮。如果设置框可见,我想禁用屏幕上的所有项目。现在,焦点可以从设置框转到 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);
                }

谢谢

4

1 回答 1

1

我会将设置框包装在匹配父视图中,并且当我显示该视图的设置框 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);
            }
于 2018-03-27T14:51:59.680 回答