0

我经历过 iOS 开发,但对 Android 开发很陌生,在这里问一些新手问题......

我正在制作一个应用程序,它可以对 png 和动画进行大量自定义绘制,并且根本没有标准的 UI 元素,我选择走 SurfaceView 之路。我也处理我的 SurfaceView 代码中触摸的所有检测。

但是,我究竟如何在 SurfaceView 代码中处理视图之间的导航?例如,我如何导航到名为 QuizActivity 的活动?在“正常”视图/活动中,我这样做:

Intent intent = new Intent(getBaseContext(), QuizActivity.class);
startActivity(intent);

但是我无法从 SurfaceView 中访问 getBaseContext 和 startActivity,即使这样做会导致同时加载多个视图?

底线:如何在 SurfaceView 中的代码中手动实现此导航?


谢谢索伦_

4

1 回答 1

2

从您的表面视图只需调用:

    Intent intent = new Intent(getContext(), QuizActivity.class);
    getContext().startActivity(intent)

每个视图都有对它们正在运行的上下文的引用,并且上下文总是可以启动新的活动、服务、获取资源等。

编辑

在您的表面视图中,您包括以下内容:

    private SurfaceCallbacks listener;

    public interface SurfaceCallbacks{
       public void onTouch(/* any data you want to pass to the activity*/);
    }

    public void registerSurfaceCallbacksListener(SurfaceCallbacks l){
       listener = l;
    }

    // and then whenever the surface being touched and you want to call something outside of the surface you do:

    if(listener!=null)
       listener.onTouch(/* pass the parameters you declared on the interface */);

并在保持表面的活动上执行以下操作:

    public ActivityThatHoldsSurface extends Activity implements SurfaceCallbacks{

       // that comes form the surface
       @Override
       onTouch(/* your parameters */){
          // do the navigation stuff
       }

       // and immediately after you inflate or instantiate your surface you do:
       mySurface.registerSurfaceCallbacksListener(ActivityThatHoldsSurface.this);

    }

是否有意义???

于 2012-11-08T10:16:50.587 回答