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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值