【问题标题】:How to start one activity from Customized View如何从自定义视图开始一项活动
【发布时间】:2011-03-05 09:52:33
【问题描述】:

如何从另一个视图(另一个活动视图)开始一个活动

例如,

public class CorrectSmoothGloflo extends Activity {
  .......................
  setContentView(new Panel(this));
}


public class Panel extends View {

   //This view class contains some drawable operation
   // Here i want to start another Activity like this

   Intent i=new Intent(CorrectSmoothGloflo.this,Screen.class);
    startActivity(i);   
}

我无法执行此操作。因为这是视图,那将不起作用,因为视图没有startActivity()。如何实施?请给出一些指导。

【问题讨论】:

    标签: android view android-activity android-intent


    【解决方案1】:

    获取一个Context对象并使用它的startActivity()方法:

    Context context = getContext();
    Intent i = new Intent(context, Screen.class);
    context.startActivity(i);
    

    【讨论】:

      【解决方案2】:

      为您的“另一个活动视图”设置一个事件处理程序,并将活动调用语句放入其中。

      【讨论】:

        【解决方案3】:
        Intent i=new Intent(CorrectSmoothGloflo.this,Screen.class); 
        startActivity(i); 
        

        因为你想开始另一个活动,所以你需要传递当前上下文而不是之前的上下文,就像我提到你的例子一样,你提到了 Correctsmoothgloflo,但它是 panel.class

        检查这是否对你有帮助...

        【讨论】:

        • 我的 PAnel 类是非活动类。这就是调用另一个活动的问题。在这种情况下,我能做什么?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-11
        • 2017-08-10
        • 1970-01-01
        • 1970-01-01
        • 2018-12-22
        相关资源
        最近更新 更多