从类通过的TouchEvent鉴于Android的整理活动 [英] Android finishing activity from class view by touchEvent

查看:114
本文介绍了从类通过的TouchEvent鉴于Android的整理活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类GameActivity,并在那里的方法的setContentView(GameView)。在课堂上 GameView 延伸查看我有一个方法:

I have got class GameActivity and in there a method setContentView(GameView). In class GameView which extends View I have got a method:

public class GameView extends View{
...
    public boolean onTouchEvent(MotionEvent event){
         switch(event.getAction()){ 
           case MotionEvent.ACTION_DOWN:    
            Intent intent = new Intent (contexTmp, MainActivity.class); 
            contexTmp.startActivity(intent); 
            //finish(); //->how to finish this activity from class view 
        }
    }
}

正如你可以在方法类 GameView 的onTouchEvent()当我pressed按钮我我改变了活动 MainActivity 。我的问题是:如何从类视图完成活动(首先我必须要完成present活动,之后前往下一个活动),因为方法: 完成()不工作?

As you can see in class GameView in method onTouchEvent() when I pressed the button I am changing the activity to MainActivity. My problem is: how to finish the activity from class view (first I have to finished the present activity and after that go to next activity), because method: finish() doesn't work?

推荐答案

使用的 的getContext() 返回用于创建视图的活动背景:

Use getContext() which returns the activity context used to create the view :

public boolean onTouchEvent(MotionEvent event) {
    switch(event.getAction()) { 
       case MotionEvent.ACTION_DOWN: 
          ...   
          ((Activity)getContext()).finish();
    }
}

这篇关于从类通过的TouchEvent鉴于Android的整理活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆