简体   繁体   English

Android:如何从子自定义视图调用父Activity方法?

[英]Android: How to call a parent Activity method from a child custom view?

Please, give me a hint how to solve this situation: 请给我一个提示如何解决这种情况:

I have two custom views in my Activity and I'd like to call an Activity method after clicking a button in one of custom views (Y). 我的Activity中有两个自定义视图,我想在单击一个自定义视图(Y)中的按钮后调用一个Activity方法。
I can get the X view from parent Acitivity by findViewById() and call it's public method. 我可以通过findViewById()从父级Acitivity获取X视图,并调用它的公共方法。 But how can I let the parent Acitivy know that the button was pressed? 但是我怎么能让父母Acitivy知道按钮被按下了?

Do I have to pass Activity reference to view Y to call an Activity method from OnClickListener? 我是否必须传递Activity引用以查看Y以从OnClickListener调用Activity方法?

例 Thank you! 谢谢!

button = (Button)customviewY.findViewById(R.id.btnClick);

button.setOnClickListener(new OnClick..(){
    onClick(View v)
    {
        textView= (TextView)customviewY.findViewById(R.id.txtView);
        textView.setText("");
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM