当屏幕触摸发生时停止我的计时器
Stop my timer when the screen touch happen
如果触摸屏的动作被执行,我想停止定时器,因为现在当我触摸屏幕时定时器仍然运行并且没有让他停止的功能。
你能帮帮我吗
public class SplashActivity extends Activity {
RelativeLayout splash;
Handler handler;
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
startActivity(new Intent(getApplicationContext(),LoginActivity.class));
finish();
return super.dispatchTouchEvent(ev);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashfile);
handler=new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
startActivity(new Intent(getApplicationContext(),LoginActivity.class));
finish();
}
},10000);
/*splash=findViewById(R.id.splash);
splash.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
startActivity(new Intent(getApplicationContext(),LoginActivity.class));
finish();
return true;
}
});*/
}
}
您可以使用 removeCallbacksAndMessages(null)
删除 Handler
上的所有回调
splash=findViewById(R.id.splash);
splash.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
handler.removeCallbacksAndMessages(null); // this will remove your callback
startActivity(new Intent(getApplicationContext(),LoginActivity.class));
finish();
return true;
}
});
如果触摸屏的动作被执行,我想停止定时器,因为现在当我触摸屏幕时定时器仍然运行并且没有让他停止的功能。 你能帮帮我吗
public class SplashActivity extends Activity {
RelativeLayout splash;
Handler handler;
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
startActivity(new Intent(getApplicationContext(),LoginActivity.class));
finish();
return super.dispatchTouchEvent(ev);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashfile);
handler=new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
startActivity(new Intent(getApplicationContext(),LoginActivity.class));
finish();
}
},10000);
/*splash=findViewById(R.id.splash);
splash.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
startActivity(new Intent(getApplicationContext(),LoginActivity.class));
finish();
return true;
}
});*/
}
}
您可以使用 removeCallbacksAndMessages(null)
Handler
上的所有回调
splash=findViewById(R.id.splash);
splash.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
handler.removeCallbacksAndMessages(null); // this will remove your callback
startActivity(new Intent(getApplicationContext(),LoginActivity.class));
finish();
return true;
}
});