获取活动对象,而在查看上下文 [英] get Activity object while in View context

查看:155
本文介绍了获取活动对象,而在查看上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个随访到这篇文章:

This is a followup to this post:

<一个href="http://stackoverflow.com/questions/3897176/findviewbyid-in-a-subclassed-surfaceview-throwing-runtimeexception">http://stackoverflow.com/questions/3897176/findviewbyid-in-a-subclassed-surfaceview-throwing-runtimeexception

(我会接受短期内,因为它是一个解决方案),我想从视图中获得调用活动,这样我就可以用它来获得所需的TextView资源。

Based on Romain Guy's feedback (which I'll accept shortly as it is a solution), I'd like to obtain the calling Activity from within the View, so that I can use it to obtain the desired TextView resource.

我没有看到视图返回活动的任何方法。什么是正确的方式做到这一点?或者是有与TextViews从另一个查看环境中工作更好的选择。

I don't see any methods in View that return Activity. What is the proper way to do this? Or is there a better alternative for working with TextViews from within another View context.

基本上,我打电话的setContentView(R.layout.xxx)中的onCreate()(像往常一样),所以我真的没有办法传递引用了其它TextViews除非我笨拙地检索后的setContentView查看和然后就可以拨打电话。

Basically, I am calling setContentView(R.layout.xxx) in onCreate() (as usual), so I don't really have a way to pass in references to additional TextViews unless I awkwardly retrieve the View after setContentView and then make calls on it.

推荐答案

这是活动是一个上下文,但不能保证使用的视图中的上下文始终是一个活动。获得来自的onCreate()的意见,做一些设置是完全有效的,是如何Android应用程序通常写。你可以做这样的事情,例如:

An Activity is a Context, but there is no guarantee that the Context used by a View is always an Activity. Getting the views from onCreate() to do some setup is perfectly valid and is how Android applications are usually written. You could do something like this for instance:

setContentView(...);
MySurfaceView v = findViewById(R.id.theusrface);
TextView t = findViewById(R.id.thecontent);
v.setContent(v);

逻辑不应该在你的看法。

The logic should not go in your Views.

这篇关于获取活动对象,而在查看上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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