【问题标题】:accessing activity variables from View从 View 访问活动变量
【发布时间】:2014-11-13 12:07:08
【问题描述】:

我有一个设置了自定义视图的 android 活动。我需要在我的 View 类中访问一些活动变量。我怎样才能做到这一点?

【问题讨论】:

  • 将您的视图上下文投射到您的活动中。考虑更改您的架构,因为这是一种不好的做法。
  • 我是新人。能举个例子吗?

标签: android android-activity view


【解决方案1】:

你的 View 应该有 Activity 的上下文,所以在你的 CustomView 里面

((MyActivity) getContext()).executeCustomMethod();

如果你在 xml 中添加视图,你可以使用这样的主父视图行

xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.test.MyActivity"

【讨论】:

  • 是否有类似的方法可以从 Activity 调用 View 方法?
  • 假设您的 View 在您的 Activity 中被夸大然后:是的,当然。最佳方法:在 onCreate ((CustomView) findViewById(R.id.cv)).executeViewsCustomMethod();
猜你喜欢
  • 2011-10-29
  • 1970-01-01
  • 2018-08-30
  • 2011-08-26
  • 2012-10-15
  • 1970-01-01
  • 2015-09-08
  • 2019-08-14
  • 1970-01-01
相关资源
最近更新 更多