6

我使用沉浸式粘性模式来隐藏导航栏和操作栏:

@TargetApi(19)
private void setImmersiveMode() {
    if (Build.VERSION.SDK_INT >= 19) {
        View decorView = getWindow().getDecorView();
        int uiOptions = getImmersiveUiOptions(decorView);
        decorView.setSystemUiVisibility(uiOptions);
        ActionBar actionBar = getActionBar();
        if (null!=actionBar) {
            actionBar.hide();
        }
    }
}

Spinner触摸a 时,navigationBar会显示 并禁用沉浸式模式。

此解决方案适用于对话框:

dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
dialog.show();
dialog.getWindow().getDecorView().setSystemUiVisibility(
context.getWindow().getDecorView().getSystemUiVisibility());
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);

但是Spinner没有show()我可以覆盖的方法。

如何防止在触摸 Spinner 时显示系统 UI?

编辑:这个问题是关于隐藏导航栏(BackButton、HomeButton 和RecentTasksButton)。我已经在使用FLAG_FULLSCREEN

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);   
4

2 回答 2

4

我知道这已经很晚了,但我终于在这里找到了解决方案:

在使用它之前,只需在你的微调器上调用它:

import android.widget.PopupWindow
import android.widget.Spinner

fun Spinner.avoidDropdownFocus() {
    try {
        val isAppCompat = this is androidx.appcompat.widget.AppCompatSpinner
        val spinnerClass = if (isAppCompat) androidx.appcompat.widget.AppCompatSpinner::class.java else Spinner::class.java
        val popupWindowClass = if (isAppCompat) androidx.appcompat.widget.ListPopupWindow::class.java else android.widget.ListPopupWindow::class.java

        val listPopup = spinnerClass
                .getDeclaredField("mPopup")
                .apply { isAccessible = true }
                .get(this)
        if (popupWindowClass.isInstance(listPopup)) {
            val popup = popupWindowClass
                    .getDeclaredField("mPopup")
                    .apply { isAccessible = true }
                    .get(listPopup)
            if (popup is PopupWindow) {
                popup.isFocusable = false
            }
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }
}
于 2020-02-29T07:02:39.447 回答
1

对于像我一样将开始将@Quinn 粘贴的解决方案从 Kotlin 重写为 Java 的每个人,稍后他们会发现链接的 git repo上也是 Java 中的解决方案,我将其粘贴在这里:

import android.widget.ListPopupWindow;
import android.widget.PopupWindow;
import android.widget.Spinner;

public static void avoidSpinnerDropdownFocus(Spinner spinner) {
    try {
        Field listPopupField = Spinner.class.getDeclaredField("mPopup");
        listPopupField.setAccessible(true);
        Object listPopup = listPopupField.get(spinner);
        if (listPopup instanceof ListPopupWindow) {
            Field popupField = ListPopupWindow.class.getDeclaredField("mPopup");
            popupField.setAccessible(true);
            Object popup = popupField.get((ListPopupWindow) listPopup);
            if (popup instanceof PopupWindow) {
                ((PopupWindow) popup).setFocusable(false);
            }
        }
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}
于 2020-03-05T08:29:31.713 回答