如何从视图中获取托管活动? [英] How to get hosting Activity from a view?
问题描述
我有一个带有 3 个 EditText
的 Activity
和一个自定义视图,它充当专门的键盘将信息添加到 EditText
中.
I have an Activity
with 3 EditText
s and a custom view which acts a specialised keyboard to add information into the EditText
s.
目前我正在将 Activity
传递到视图中,以便我可以获取当前聚焦的编辑文本并从自定义键盘更新内容.
Currently I'm passing the Activity
into the view so that I can get the currently focused edit text and update the contents from the custom keyboard.
有没有一种方法可以在不将活动传递到视图的情况下引用父活动并获取当前聚焦的 EditText
?
Is there a way of referencing the parent activity and getting the currently focused EditText
without passing the activity into the view?
推荐答案
我刚刚从 官方支持库 到目前为止它工作正常:
I just pulled that source code from the MediaRouter in the official support library and so far it works fine:
private Activity getActivity() {
Context context = getContext();
while (context instanceof ContextWrapper) {
if (context instanceof Activity) {
return (Activity)context;
}
context = ((ContextWrapper)context).getBaseContext();
}
return null;
}
这篇关于如何从视图中获取托管活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!