این درس نحوه ثبت شنونده را شرح میدهد تا برنامه شما بتواند از تغییرات نمایان بودن میانای کاربر سیستم مطلع شود. این کار زمانی مفید است که بخواهید بخشهای دیگر میانای کاربر را با پنهان کردن/نمایش دادن نوارهای سیستم همگامسازی کنید.
ثبت کردن شنونده
برای مطلع شدن از تغییرات رؤیتپذیری واسط کاربر سیستم،
View.OnSystemUiVisibilityChangeListener را در نمای خود ثبت کنید.
این معمولاً نمایی است که برای کنترل رؤیتپذیری ناوبری استفاده میکنید.
برای مثال، میتوانید این کد را به روش
onCreate() فعالیتتان اضافه کنید:
کاتلین
window.decorView.setOnSystemUiVisibilityChangeListener { visibility -> // Note that system bars will only be "visible" if none of the // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set. if (visibility and View.SYSTEM_UI_FLAG_FULLSCREEN == 0) { // TODO: The system bars are visible. Make any desired // adjustments to your UI, such as showing the action bar or // other navigational controls. } else { // TODO: The system bars are NOT visible. Make any desired // adjustments to your UI, such as hiding the action bar or // other navigational controls. } }
جاوا
View decorView = getWindow().getDecorView(); decorView.setOnSystemUiVisibilityChangeListener (new View.OnSystemUiVisibilityChangeListener() { @Override public void onSystemUiVisibilityChange(int visibility) { // Note that system bars will only be "visible" if none of the // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set. if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) { // TODO: The system bars are visible. Make any desired // adjustments to your UI, such as showing the action bar or // other navigational controls. } else { // TODO: The system bars are NOT visible. Make any desired // adjustments to your UI, such as hiding the action bar or // other navigational controls. } } });
بهطور کلی، همگام نگه داشتن واسط کاربر با تغییرات در نمایان بودن نوار سیستم، روش خوبی است. برای مثال، میتوانید از این شنونده برای پنهان کردن و نمایش دادن نوار اقدام همزمان با پنهان کردن و نمایش دادن نوار وضعیت استفاده کنید.