android获去活动,从android中的上下文获取活动
从android中的上下文获取活动
这个让我难过。
我需要在自定义布局类中调用activity方法。 这个问题是我不知道如何从布局中访问活动。
ProfileView
public class ProfileView extends LinearLayout
{
TextView profileTitleTextView;
ImageView profileScreenImageButton;
boolean isEmpty;
ProfileData data;
String name;
public ProfileView(Context context, AttributeSet attrs, String name, final ProfileData profileData)
{
super(context, attrs);
......
......
}
//Heres where things get complicated
public void onClick(View v)
{
//Need to get the parent activity and call its method.
ProfileActivity x = (ProfileActivity) context;
x.activityMethod();
}
}
ProfileActivity
public class ProfileActivityActivity extends Activity
{
//In here I am creating multiple ProfileViews and adding them to the activity dynamically.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.profile_activity_main);
}
public void addProfilesToThisView()
{
ProfileData tempPd = new tempPd(.....)
Context actvitiyContext = this.getApplicationContext();
//Profile view needs context, null, name and a profileData
ProfileView pv = new ProfileView(actvitiyContext, null, temp, tempPd);
profileLayout.addView(pv);
}
}
正如您在上面所看到的,我以编程方式实例化View配置文件并使用它传递活动Context。 2个问题:
我将正确的上下文传递给Profileview吗?
如何从上下文中获取包含活动?
9个解决方案
411 votes
从您的Context,只需传递Activity作为您的布局的Context:
ProfileView pv = new ProfileView(this, null, temp, tempPd);
之后你的布局会有一个Context,但你知道它实际上是你的Activity,你可以把它投射到你需要的东西:
Activity activity = (Activity) context;
Boris Strandjev answered 2019-05-12T14:28:04Z
22 votes
没有
你不能
Android中有两种不同的上下文。 一个用于您的应用程序(我们称之为BIG一个),每个视图一个(让我们称之为活动上下文)。
linearLayout是一个视图,因此您必须调用活动上下文。 要从活动中调用它,只需调用“this”即可。 这么容易不是吗?
当你使用
this.getApplicationContext();
您调用BIG上下文,该上下文描述您的应用程序并且无法管理您的视图。
Android的一个大问题是上下文无法调用您的活动。 当有人开始使用Android开发时,这是一个很大的避免这个问题。 您必须找到一种更好的方法来编写您的类(或者通过“Activity activity”替换“Context context”并在需要时将其转换为“Context”)。
问候。
只是为了更新我的答案。 获取Task的最简单方法是在View中定义Dialog实例。例如
public class DummyActivity extends Activity
{
public static DummyActivity instance = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Do some operations here
}
@Override
public void onResume()
{
super.onResume();
instance = this;
}
@Override
public void onPause()
{
super.onPause();
instance = null;
}
}
然后,在您的Task,Dialog,View中,您可以使用这种代码来获取Activity context:
if (DummyActivity.instance != null)
{
// Do your operations with DummyActivity.instance
}
Manitoba answered 2019-05-12T14:29:32Z
16 votes
这是我在片段或自定义视图中在UI中操作时成功用于将Context转换为Activity的内容。 它将递归解包ContextWrapper,如果失败则返回null。
public Activity getActivity(Context context)
{
if (context == null)
{
return null;
}
else if (context instanceof ContextWrapper)
{
if (context instanceof Activity)
{
return (Activity) context;
}
else
{
return getActivity(((ContextWrapper) context).getBaseContext());
}
}
return null;
}
Theo answered 2019-05-12T14:30:02Z
6 votes
如果您想从自定义布局类(非活动类)中调用活动方法。您应该使用接口创建委托。
它未经测试,我编码正确。 但我正在传达一种方法来实现你想要的。
首先是创建和接口
interface TaskCompleteListener {
public void onProfileClicked(T result);
}
public class ProfileView extends LinearLayout
{
private TaskCompleteListener callback;
TextView profileTitleTextView;
ImageView profileScreenImageButton;
boolean isEmpty;
ProfileData data;
String name;
public ProfileView(Context context, AttributeSet attrs, String name, final ProfileData profileData)
{
super(context, attrs);
......
......
}
public setCallBack( TaskCompleteListener cb)
{
this.callback = cb;
}
//Heres where things get complicated
public void onClick(View v)
{
callback.onProfileClicked("Pass your result or any type");
}
}
并将此实现到任何Activity。
并称之为
ProfileView pv = new ProfileView(actvitiyContext, null, temp, tempPd);
pv.setCallBack(new TaskCompleteListener
{
public void onProfileClicked(String resultStringFromProfileView){}
});
Xar E Ahmer answered 2019-05-12T14:31:00Z
4 votes
上下文可以是应用程序,服务,活动等。
通常,Activity中的Views的上下文是Activity本身,因此您可能认为您可以将此Context转换为Activity,但实际上您不能总是这样做,因为在这种情况下,Context也可以是ContextThemeWrapper。
ContextThemeWrapper在AppCompat和Android的最新版本中被大量使用(感谢布局中的android:theme属性)所以我个人永远不会执行此演员。
所以简短的回答是:您无法从视图中的上下文中可靠地检索活动。 通过调用Activity上的方法将Activity传递给视图,该方法将Activity作为参数。
BladeCoder answered 2019-05-12T14:31:54Z
2 votes
永远不要将getApplicationContext()与视图一起使用。
它应始终是活动的上下文,因为视图附加到活动。 此外,您可能有自定义主题集,并且在使用应用程序的上下文时,所有主题都将丢失。 在此处阅读有关不同版本的上下文的更多信息。
lomza answered 2019-05-12T14:32:28Z
0 votes
一个Activity是Context的特化,所以,如果你有一个Context,你已经知道你打算使用哪个活动,并且可以简单地将一个活动转换为c; 其中a是Activity,c是Context。
Activity a = (Activity) c;
ACLima answered 2019-05-12T14:32:55Z
0 votes
我用过convert Activity
Activity activity = (Activity) context;
Samuel Ivan answered 2019-05-12T14:33:23Z
0 votes
这个方法应该有帮助..!
public Activity getActivityByContext(Context context){
if(context == null){
return null;
}
else if((context instanceof ContextWrapper) && (context instanceof Activity)){
return (Activity) context;
}
else if(context instanceof ContextWrapper){
return getActivity(((ContextWrapper) context).getBaseContext());
}
return null;
}
我希望这有帮助..快乐编码!
Taslim answered 2019-05-12T14:33:53Z
android获去活动,从android中的上下文获取活动相关推荐
- android 微信 去广告插件,Android"挂逼"修练之行--微信小程序逆向辅助插件工具开发详解 ......
一.前言 之前一篇文章已经详细介绍了 微信小程序包的格式 ,在之前文章中也了解到小程序包存放在沙盒目录,但是微信为了让小程序包安全就对文件名做了一个处理,导致我们很难找到对应的小程序对应的包文件,所以 ...
- 计算机教研组活动简讯,思中信息技术组教研活动简报
2020年9月第3期总50期 9月21日下午信息技术组全体教师在科技楼四楼开展了教研活动,本次活动由何林莲老师主持. 第5节课安红翠老师在微机室3为高一(1)班讲授了信息技术基础第三章第一节<信 ...
- android中屏幕宽高显示不全,Android 获取屏幕宽度跟高度
Android 获取屏幕宽度跟高度 在android开发过程中,对于控件的高度,宽度,通过下面的函数调用,轻松实现编程中设置控件的相对宽度跟高度: // 获得屏幕的宽度 public static i ...
- android 清单文件注册,在AndroidManifest(清单文件)中注册activity(活动)及配置主活动、更改App图标、App名称、修改隐藏标题栏...
打开app/src/main/AndroidManifest. package="com.example.administrator.myapplication" > and ...
- 如何在Android 10中从后台启动活动
如何在Android 10中从后台启动活动? https://www.it1352.com/1922748.html 小米手机"后台弹出界面(允许应用在后台弹出界面)"权限问题解决 ...
- android 数据库 字节数组,java - 如何使用活动的android序列化字节数组并将其存储到数据库中? - 堆栈内存溢出...
我有一个图像作为byte[] ,我需要将此图像保存在数据库中. 我为此使用Active Android库. 我知道db中用于此目的的数据类型应该是BLOB. 我知道byte[]不能直接存储,我知道它应 ...
- android 不保留活动,Android5.0之后打开开发者选项中的不保留活动,解决方案
在Andorid5.0之后,在开发者选项中有一项 不保留活动(用户离开后立即清除每个活动) 这是什么意思呢? 假如你从A 界面跳转到B界面. 这个时候你再按返回键,是直接退出了整个应用程序.意思就是销 ...
- android p preview_细数 Android P 开发者预览版中最不能错过的新特性
原标题:细数 Android P 开发者预览版中最不能错过的新特性 2018年安卓巴士全球开发者论坛-重庆站 [线下活动]春天到了 跟小编一起去重庆嗨皮吧~ Android P 应用适配新特性1.Pr ...
- android 活动生命,Android 活动生命周期
一.活动生命周期的意义 Activity(活动)是一种可以包含用户界面的组件,主要用于与用户进行交互.也就是说,在用户与界面进行交互之前,必须要进行特定的初始化操作.再者,Android 操作系统是一 ...
最新文章
- Udacity机器人软件工程师课程笔记(二十四) - 控制(其二) - PID优化,梯度下降算法,带噪声的PID控制
- RocketMQ3.2.2生产者发送消息自动创建Topic队列数无法超过4个
- php 删除指定html标签,总结php删除html标签和标签内的内容的方法
- cloud foundry部署报错TypeError: can't convert Hash into String
- 使用远程工具连接提示**Host *** is not allowed to connect to this mysql server**拒绝连接错误
- 二叉搜索树(BFS)总结
- jupyter notebook使用入门2——创建一个基于scikit-Learn的线性预测ipynb文件
- C---队列,栈的实现
- popwindow 加个边框_利用popupwindow生成带有列表的对话框,并设置对话框列表的点击事件...
- ExtJs gridPanel Column 时间格式化
- root android手机型号,兼容支持各种型号手机的Root权限获取方法
- 使用JMeter录制脚本并调试
- 《机械基础》记忆性内容总结
- vuepress-theme-reco评论管理
- R语言使用cph函数和rcs函数构建限制性立方样条cox回归模型、使用cox.zph函数执行PH检验、检验模型是否满足等比例风险
- 大公司,一般都怎么开会?
- GPRS手机+笔记本电脑无线上网
- debian安装java
- 利用Node-js搭建前端自动化平台
- 深入理解java虚拟机-第五章:虚拟机字节码执行引擎
热门文章
- python无法初始化设备_无法初始化图形设备什么意思
- 题解:[USACO12MAR]花盆Flowerpot 【单调队列】
- 实现人生梦想,共同创造人生辉煌!
- Unity中实现赛车游戏
- J0ker的CISSP之路: How CISSP(1)
- matlab工程应用基础,Matlab工程应用基础_2_214090
- spark系列11:RDD之间的依赖关系,窄依赖和宽依赖
- 百度云网盘批量分享独立链接,简单暴力!!! 不用下载软件,直接在网页上搞定!
- 居然有人问,软件测试算是程序员吗?
- Android Clipping