从上下文的android

从上下文的android

本文介绍了四处活动,从上下文的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这人有我难住了。

我需要从一个自定义布局类中调用一个活动的方法。这样做的问题是,我不知道如何从布局中访问活动。

ProfileView

 公共类ProfileView扩展的LinearLayout
{
    TextView的profileTitleTextView;
    ImageView的profileScreenImageButton;
    布尔的isEmpty;
    ProfileData数据;
    字符串名称;

    公共ProfileView(上下文的背景下,ATTRS的AttributeSet,字符串名称,最终ProfileData profileData)
    {
        超(背景下,ATTRS);
        ......
        ......
    }

    //继承人,事情变得复杂
    公共无效的onClick(视图v)
    {
        //需要得到家长的活动,并调用它的方法。
        ProfileActivity X =(ProfileActivity)上下文;
        x.activityMethod();
    }
}
 

ProfileActivity

 公共类ProfileActivityActivity扩展活动
{
    //在这里我创建多个ProfileViews并动态将它们添加到活动中。

    公共无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.profile_activity_main);
    }

    公共无效addProfilesToThisView()
    {
        ProfileData tempPd =新tempPd(.....)
        上下文actvitiyContext = this.getApplicationContext();
        //资料视图需要背景下,空,名称和profileData
        ProfileView PV =新ProfileView(actvitiyContext,空,温度,tempPd);
        profileLayout.addView(PV);
    }
}
 

正如你可以在上面看到的,我实例化profileView编程并传入activityContext它。 2个问题:

  1. 我是通过正确的上下文到Profileview?
  2. 如何获得从上下文中含活性?
解决方案

只是通过在本作的上下文为您布局:

  ProfileView PV =新ProfileView(本,空,温度,tempPd);
 

然后你将不得不在布局的背景下,但你会知道它实际上是在那里的活动,你可以施放它,让你有你需要提供:

 活动活动=(活动)范围内;
 

This one has me stumped.

I need to call an activity method from within a custom layout class. The problem with this is that I don't know how to access the activity from within the layout.

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);
    }
}

As you can see above, I am instantiating the profileView programatically and passing in the activityContext with it. 2 questions:

  1. Am i passing the correct context into the Profileview?
  2. How do I get the containing activity from the context?
解决方案

Just pass in this as the Context for you layout:

ProfileView pv = new ProfileView(this, null, temp, tempPd);

Afterwards you will have the context in the Layout, but you will know it is actually the activity there and you can cast it so that you have what you need available:

Activity activity = (Activity) context;

这篇关于四处活动,从上下文的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 04:59