【问题标题】:How to detect if screen is touched in onTouchEvent()如何在 onTouchEvent() 中检测屏幕是否被触摸
【发布时间】:2015-04-07 05:47:31
【问题描述】:

我需要了解如何检测用户是否触摸了屏幕。

预期结果:-当用户触摸屏幕时,它应该跳过启动屏幕并移动到主要活动。

问题:-每当用户触摸屏幕时,就会跳过启动画面,但在后台 sleep(10500) 中的 try 块会继续运行,并且随着时间的推移,主 Activity 会再次启动,即它会打开两次。

到目前为止我做了什么:-我尝试做while循环并给出一个条件,如果条件满足(触摸)然后中断。但我似乎没有得到正确的工作条件。 启动画面代码:-

@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();
}

如果在 try 块中提供了语句,因此如果满足条件,它将中断。但我不知道该条件。需要有关条件的帮助。 谢谢。

【问题讨论】:

    标签: android touch-event


    【解决方案1】:
    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();
    }
    

    【讨论】:

    • 非常感谢!真的很有帮助!
    猜你喜欢
    • 2012-02-14
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多