我将重点放在视图上但是当我改变方向时,焦点会从该视图中消失。 onConfigurationChanged()处理焦点? 或者我需要明确设置它,如果是,那么我该如何设置焦点? 我需要跟踪当前的聚焦视图。如何做到这一点?
答案 0 :(得分:1)
覆盖Activity中的onSaveInstanceState和onRestoreInstanceState方法。然后,您可以通过抓取视图的ID并将其保存到onSaveInstanceState方法中的Bundle来跟踪任何焦点视图。然后在onRestoreInstanceState方法中,您只需获取ID并找到具有该ID的视图。你甚至不需要施放它或任何东西。然后,您只需要为该视图请求焦点。它看起来像这样:
@Override
protected void onSaveInstanceState(Bundle outState) {
int viewId = this.getCurrentFocus().getId();
outState.putInt("hasFocus", viewId);
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
int viewId = savedInstanceState.getInt("hasFocus");
View view = findViewById(viewId);
view.requestFocus();
}