我尝试存储最后一次单击的视图并将其存储在临时变量 requestFocus() 中重新加载,但它不起作用。
什么不工作?您的活动是否正在重新创建,因此临时变量正在丢失?如果是这样,那么这个答案可能会对您有所帮助。
我以前没有使用过 ExpandableListView,但我不得不在我的代码中解决类似的问题。当方向改变发生时,活动被破坏然后重新创建,所以我使用onRetainNonConfigurationInstance()
用来存储我想要保留的信息。
这是一些示例代码:
protected StoredState mStoredState;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
final Object data = getLastNonConfigurationInstance();
if (data == null)
{
// First time initialization
mStoredState = new StoredState();
}
else
{
// StoredState data exists
mStoredState = (StoredState)data;
if (mStoredState.mFieldToGiveFocusTo != -1)
{
// Give focus to the field that had focus before the
// activity was recreated
View viewToFocus = findViewById(mStoredState.mFieldToGiveFocusTo);
viewToFocus.requestFocus();
}
}
}
@Override
public Object onRetainNonConfigurationInstance()
{
return mStoredState;
}
/**
* Class to store the information about this activity when the orientation
* is changed
*/
private static class StoredState
{
int mFieldToGiveFocusTo;
private StoredState()
{
mFieldToGiveFocusTo = -1;
}
}
然后你只需要设置mStoredState.mFieldToGiveFocusTo
用户第一次触摸 EditText 的时间。