如何从视图中获取托管活动?

发布于 2024-12-18 00:56:53 字数 241 浏览 0 评论 0原文

我有一个带有 3 个 EditText 的 Activity 和一个自定义视图,该视图充当专用键盘以将信息添加到 EditText 中。

目前,我正在将 Activity 传递到视图中,以便我可以获得当前聚焦的编辑文本并从自定义键盘更新内容。

有没有一种方法可以引用父活动并获取当前聚焦的 EditText 而不将活动传递到视图中?

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

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.

Is there a way of referencing the parent activity and getting the currently focused EditText without passing the activity into the view?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

西瓜 2024-12-25 00:56:53

我刚刚从 官方支持库 到目前为止,它工作正常:

private Activity getActivity() {
    Context context = getContext();
    while (context instanceof ContextWrapper) {
        if (context instanceof Activity) {
            return (Activity)context;
        }
        context = ((ContextWrapper)context).getBaseContext();
    }
    return null;
}

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;
}
小瓶盖 2024-12-25 00:56:53

以下方法可能会帮助您

  1. Activity host = (Activity) view.getContext();和
  2. view.isFocused()

在使用它之前,您至少应该首先检查它是否是一个 Activity:

if (view.getContext() instanceof Activity){
     Activity host = (Activity) view.getContext();
     //do something with host now
}

following methods may help you

  1. Activity host = (Activity) view.getContext(); and
  2. view.isFocused()

You should first check if it is an Activity at minimum before using it:

if (view.getContext() instanceof Activity){
     Activity host = (Activity) view.getContext();
     //do something with host now
}
青朷 2024-12-25 00:56:53

已更新

tailrec fun Context.getActivity(): Activity? = this as? Activity
    ?: (this as? ContextWrapper)?.baseContext?.getActivity()

感谢@Westy92

用法:

context.getActivity()

UPDATED

tailrec fun Context.getActivity(): Activity? = this as? Activity
    ?: (this as? ContextWrapper)?.baseContext?.getActivity()

thanks to @Westy92

Usage:

context.getActivity()
℡寂寞咖啡 2024-12-25 00:56:53

我采用了 Gomino答案 并将其修改为完全适合 myUtils.java,这样我就可以随时随地使用它。希望有人觉得它有帮助:)

abstract class myUtils {
    public static Activity getActivity(View view) {
        Context context = view.getContext();
        while (context instanceof ContextWrapper) {
            if (context instanceof Activity) {
                return (Activity)context;
            }
            context = ((ContextWrapper)context).getBaseContext();
        }
        return null;
    }
}

I took Gomino's answer and modified it to fit perfectly in myUtils.java so I can use it wherever and whenever I need. Hope someone finds it helpful :)

abstract class myUtils {
    public static Activity getActivity(View view) {
        Context context = view.getContext();
        while (context instanceof ContextWrapper) {
            if (context instanceof Activity) {
                return (Activity)context;
            }
            context = ((ContextWrapper)context).getBaseContext();
        }
        return null;
    }
}
情绪少女 2024-12-25 00:56:53

只是想注意,如果您知道您的视图位于 Fragment 内,您可以这样做:

FragmentManager.findFragment(this).getActivity();

onAttachedToWindowonDraw 调用此方法,您不能提前调用它(例如 onFinishInflate),否则您将收到异常。

如果您不确定您的视图是否在 Fragment 内,请参考其他答案,例如 Gomino,只是想将其扔在那里。

Just want to note that, if you know that your view is inside a Fragment, you can just do:

FragmentManager.findFragment(this).getActivity();

Call this from your onAttachedToWindow or onDraw, you can't call it earlier (e.g. onFinishInflate) or you will get an exception.

If you are not sure your view is inside a Fragment then refer to other answers e.g. Gomino, just wanted to throw it out there.

坦然微笑 2024-12-25 00:56:53

基于 @gomino 的 Kotlin 答案

fun View?.getActivitySafely():Activity?{

    var context: Context = this!!.context
    while (context is ContextWrapper) {
        if (context is Activity) {
            return context
        }
        context = context.baseContext
    }
    return null
}

Kotlin answer based on @gomino

fun View?.getActivitySafely():Activity?{

    var context: Context = this!!.context
    while (context is ContextWrapper) {
        if (context is Activity) {
            return context
        }
        context = context.baseContext
    }
    return null
}
原来分手还会想你 2024-12-25 00:56:53

View 的 Kotlin 扩展属性用于检索父活动:

val View.activity: Activity?
get() {
    var ctx = context
    while (true) {
        if (!ContextWrapper::class.java.isInstance(ctx)) {
            return null
        }
        if (Activity::class.java.isInstance(ctx)) {
            return ctx as Activity
        }
        ctx = (ctx as ContextWrapper).baseContext
    }
}

Kotlin extension property for View to retrieve parent activity:

val View.activity: Activity?
get() {
    var ctx = context
    while (true) {
        if (!ContextWrapper::class.java.isInstance(ctx)) {
            return null
        }
        if (Activity::class.java.isInstance(ctx)) {
            return ctx as Activity
        }
        ctx = (ctx as ContextWrapper).baseContext
    }
}
ま昔日黯然 2024-12-25 00:56:53

在 Android 7+ 中,视图无法再访问封闭的 Activity,因此无法再将 view.getContext() 转换为 Activity。

相反,下面的代码适用于 Android 7+ 和 6:

private static Activity getActivity(final View view) {
    return (Activity) view.findViewById(android.R.id.content).getContext();
}

In Android 7+ the view does not have access to the enclosing activity anymore, so view.getContext() can't be cast to an Activity anymore.

Instead, the code below works in Android 7+ and 6:

private static Activity getActivity(final View view) {
    return (Activity) view.findViewById(android.R.id.content).getContext();
}
瑾兮 2024-12-25 00:56:53

@覆盖
公共布尔shouldOverrideUrlLoading(WebView视图,WebResourceRequest请求){
if(request.getUrl().getHost().startsWith("pay.google.com")) {
Intent意图 = new Intent(Intent.ACTION_VIEW, request.getUrl());
view.getContext().startActivity(意图);
返回真;
}
...
...
}

@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if(request.getUrl().getHost().startsWith("pay.google.com")) {
Intent intent = new Intent(Intent.ACTION_VIEW, request.getUrl());
view.getContext().startActivity(intent);
return true;
}
...
...
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文