安卓监听焦点变化

张开发
2026/4/17 11:36:07 15 分钟阅读

分享文章

安卓监听焦点变化
// 获取根布局 (DecorView) View rootView getWindow().getDecorView(); // 添加全局焦点变化监听器 rootView.getViewTreeObserver().addOnGlobalFocusChangeListener( new ViewTreeObserver.OnGlobalFocusChangeListener() { Override public void onGlobalFocusChanged(View oldFocus, View newFocus) { // 获取类名注意处理空指针 String oldName (oldFocus ! null) ? oldFocus.getClass().getSimpleName() : null; String newName (newFocus ! null) ? newFocus.getClass().getSimpleName() : null; Log.d(GlobalFocus, 焦点从 oldName 切换到了 newName); } } );加在onCreate中

更多文章