【问题标题】:Android: Passing data from surfaceView to its parent activityAndroid:将数据从surfaceView传递到其父活动
【发布时间】:2014-01-21 10:42:00
【问题描述】:

我在将信息从表面视图类发送到其父类时遇到问题

覆盖活动将其视图设置为绘图面板:

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。我不能简单地在 Overlay 活动中使用 onTouchEvent,因为我需要用drawingPanel 绘制东西。我的主要目标是在使用 SurfaceView 时触摸屏幕时隐藏/显示操作栏。如果有其他方法可以实现,请在下面说明。

【问题讨论】:

    标签: java android android-actionbar surfaceview


    【解决方案1】:

    尝试将此行添加到 Overlay 类的 onCreate 方法中

       dPanel.setActivity(this);
    

    并像这样在drawingPanel 类中实现setActivity()

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

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

    mActivity.getActionBar().hide();
    

    和,

    mActivity.getActionBar().show();
    

    【讨论】:

    • Dannie 的代码有效! (对于未来的读者)他的最后两行是伪代码,所以这是我在 onTouchEvent 方法中使用的: if(mActivity.getActionBar().isShowing()) mActivity.getActionBar().hide();否则 mActivity.getActionBar().show();
    • 感谢您的反馈,我已经相应地更新了答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-15
    • 1970-01-01
    • 2017-08-25
    • 1970-01-01
    相关资源
    最近更新 更多