Android:将数据从surfaceView传递到其父活动

时间:2014-01-20 23:48:32

标签: java android android-actionbar surfaceview

我无法将信息从surfaceView类发送到其父类

叠加活动将其视图设置为绘图面板:

public class Overlay extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_Overlay);
       drawingPanel dPanel = new drawingPanel(this);
       setContentView(dPanel);

    }
}

然后在绘图板中:

public class drawingPanel extends SurfaceView implements SurfaceHolder.Callback {
    Context context = context;
    public Player(Context context, int num) throws ClassNotFoundException, IOException {
       super(context);
       this.context = context;
       SurfaceHolder holder = getHolder();
       holder.addCallback(this);
    }
    public boolean onTouchEvent(MotionEvent e){
       if(e.getAction()==MotionEvent.ACTION_DOWN){
           //send info to Overlay that drawingPanel was touched
       }
    }
} 

触摸surfaceView时,我想将该信息发送到叠加层。我不能简单地在Overlay活动中使用onTouchEvent,因为我需要使用drawingPanel绘制内容。我的主要目标是在使用surfaceView时触摸屏幕时隐藏/显示操作栏。如果还有其他方法可以实现,请在下面说明。

1 个答案:

答案 0 :(得分:1)

尝试将此行添加到onCreate

Overlay方法中
   dPanel.setActivity(this);

setActivity()类中实现drawingPanel,如下所示:

public void setActivity(Activity overlayActivity) {
    mActivity = overlayActivity;
}

这样,您可以使用mActivity来引用您的Activity并调用公共方法来“隐藏/显示操作栏”,例如:

mActivity.getActionBar().hide();

mActivity.getActionBar().show();