如何从视图中获取托管活动? [英] How to get hosting Activity from a view?

查看:23
本文介绍了如何从视图中获取托管活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 3 个 EditTextActivity 和一个自定义视图,它充当专门的键盘将信息添加到 EditText 中.

I have an Activity with 3 EditTexts and a custom view which acts a specialised keyboard to add information into the EditTexts.

目前我正在将 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆