如何在 postDelayed 中停止 Runnable Handler
How can I stop Runnable Handler in postDelay
我是 Android 的新手...我开发了一个应用程序,它通过单击按钮来转换图像,并添加了一个 Handler Runnable 来自动转换图像(即使不按下按钮时) 5000ms的延迟时间。但是在我按下按钮的那一刻我必须停止处理程序的延迟,但由于某种原因我不能这样做。
public class MainActivity extends AppCompatActivity {
private Integer images[]={R.drawable.pic1, R.drawable.pic21, ....
public int currImage=0;
public int ThreadCnt = 0;
public boolean backInt=true,forwardInc=false, ThreadInc=true;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
initializeImageSwitcher();
setInitialImage();
setImageRotateListener();
final Handler handler = new Handler();
final Runnable run = new Runnable() {
@Override
public void run() {
//forwardInc is a Boolean indicating that click button occurred
if(forwardInc){
//and in this case (of forwardInc=true) I want to stop runnable for 5sec
handler.removeCallbacks(this);
handler.postDelayed(this, 5000);
forwardInc=false;
setInitialImage();
setImageRotateListener();
currImage++;
ThreadCnt++;
}
handler.postDelayed(this, 5000);
}
};
handler.post(run);
这是点击按钮的代码
public void setImageRotateListener() {
final Button rotatebutton=(Button) findViewById(R.id.btnRotateImage);
rotatebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currImage++;
//forwardInc indicating that click button occurred
forwardInc=true;
...
setCurrentImage();
...
目前还不清楚您要做什么。但据我所知,请尝试以下操作:
首先,在 class.
中使 Runnable
和 Handler
成为全局变量
public class MainActivity extends AppCompatActivity{
private Handler handler = new Handler();
private Runnable runnable = new Runnable(){
public void run(){/**/}
}
//other declarations.
//methods
}
内部 运行 方法 运行nable:
public void run(){
setInitialImage(); //probably set next image
currImage++;
ThreadCnt++;
handler.postDelayed( this, 5000 )
}
现在在 onClick 中:
public void onClick(View view){
handler.removeCallbacks(this);
handler.postDelayed(this, 5000);
//no idea why you are using other code here.
}
并在 onCreate 内部:
//other lines
setImageRotateListener();
handler.post(runnable);
我是 Android 的新手...我开发了一个应用程序,它通过单击按钮来转换图像,并添加了一个 Handler Runnable 来自动转换图像(即使不按下按钮时) 5000ms的延迟时间。但是在我按下按钮的那一刻我必须停止处理程序的延迟,但由于某种原因我不能这样做。
public class MainActivity extends AppCompatActivity {
private Integer images[]={R.drawable.pic1, R.drawable.pic21, ....
public int currImage=0;
public int ThreadCnt = 0;
public boolean backInt=true,forwardInc=false, ThreadInc=true;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
initializeImageSwitcher();
setInitialImage();
setImageRotateListener();
final Handler handler = new Handler();
final Runnable run = new Runnable() {
@Override
public void run() {
//forwardInc is a Boolean indicating that click button occurred
if(forwardInc){
//and in this case (of forwardInc=true) I want to stop runnable for 5sec
handler.removeCallbacks(this);
handler.postDelayed(this, 5000);
forwardInc=false;
setInitialImage();
setImageRotateListener();
currImage++;
ThreadCnt++;
}
handler.postDelayed(this, 5000);
}
};
handler.post(run);
这是点击按钮的代码
public void setImageRotateListener() {
final Button rotatebutton=(Button) findViewById(R.id.btnRotateImage);
rotatebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currImage++;
//forwardInc indicating that click button occurred
forwardInc=true;
...
setCurrentImage();
...
目前还不清楚您要做什么。但据我所知,请尝试以下操作:
首先,在 class.
中使Runnable
和 Handler
成为全局变量
public class MainActivity extends AppCompatActivity{
private Handler handler = new Handler();
private Runnable runnable = new Runnable(){
public void run(){/**/}
}
//other declarations.
//methods
}
内部 运行 方法 运行nable:
public void run(){
setInitialImage(); //probably set next image
currImage++;
ThreadCnt++;
handler.postDelayed( this, 5000 )
}
现在在 onClick 中:
public void onClick(View view){
handler.removeCallbacks(this);
handler.postDelayed(this, 5000);
//no idea why you are using other code here.
}
并在 onCreate 内部:
//other lines
setImageRotateListener();
handler.post(runnable);