如何从视图获取托管活动? [英] How to get hosting Activity from a view?
本文介绍了如何从视图获取托管活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有3个EditText
的Activity
和一个自定义视图,该视图充当专用键盘将信息添加到EditText
s中.
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屋!
查看全文