简体   繁体   English

如何在onTouchEvent()中检测屏幕是否被触摸

[英]How to detect if screen is touched in onTouchEvent()

I need to understand on how to detect if user has touched the screen. 我需要了解如何检测用户是否触摸了屏幕。

Expected Result:-Whenever user touches screen it should skip Splash Screen and move to main activity. 预期结果:-每当用户触摸屏幕时,都应跳过启动屏幕并移至主要活动。

Problem:-Whenever user touches the screen Splash Screen is skipped but in the background sleep(10500) in try block keeps running and as it elapses the Main Activity starts again ie it opens two times. 问题:-每当用户触摸屏幕时,都会跳过启动屏幕,但在try块的后台睡眠(10500)中,它会继续运行,并且随着时间的流逝,Main Activity再次开始,即打开两次。

What have I done so far:-I tried do while loop and gave a condition,if the condition is satisfied(of Touch) then break.But I don't seem to get the correct working condition. 到目前为止,我已经做了什么:-我尝试在while循环中执行一个操作,并给出了一个条件,如果该条件得到满足(Touch),然后会中断。但是我似乎没有得到正确的工作条件。 Splash Screen Code:- 开机画面代码:-

@Override
protected void onCreate(Bundle splashState) {
    // TODO Auto-generated method stub
    super.onCreate(splashState);
    setContentView(R.layout.splash);
    ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound);
    ourSong.start();
    Thread timer = new Thread() {
        public void run() {
            do
            {
            try {
                //if(onTouchEvent(null))
                //  break;
                sleep(10500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                startActivity(new Intent("com.first.MAINACTIVITY"));
            }
        }while(false);
        }
    };

    timer.start();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        startActivity(new Intent("com.first.MAINACTIVITY"));
        finish();
        ourSong.release();
    }
    return super.onTouchEvent(event);
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    finish();
    ourSong.release();
}

If Statement is provided in the try block so if condition is satisfied it would break.But the condition is unknown to me.Need help with the condition. 如果在try块中提供了Statement,则如果满足条件,它将被破坏,但是条件对我来说是未知的,需要该条件的帮助。 Thanks. 谢谢。

private boolean isSplashRunning = true;

@Override
protected void onCreate(Bundle splashState) {
    // TODO Auto-generated method stub
    super.onCreate(splashState);
    setContentView(R.layout.splash);
    ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound);
    ourSong.start();
    Thread timer = new Thread() {
        public void run() {
            do
            {
            try {
                //if(onTouchEvent(null))
                //  break;
                sleep(10500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                if(isSplashRunning)
                    startActivity(new Intent("com.first.MAINACTIVITY"));
            }
        }while(false);
        }
    };

    timer.start();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        isSplashRunning = false; //or in onPause
        startActivity(new Intent("com.first.MAINACTIVITY"));
        finish();
        ourSong.release();
    }
    return super.onTouchEvent(event);
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    isSplashRunning = false;
    super.onPause();
    finish();
    ourSong.release();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM