我的Android应用程序中有一个动画,它会闪烁TextView不同的颜色.我使用了TimerTask,Timer和Runnable方法来实现它.我需要做的是在用户在onPause()期间离开应用程序时停止线程,并在用户返回onResume()中的应用程序时恢复线程.以下是我实现的代码,但它不起作用(onPause()和onResume()件),我不明白为什么.我已经阅读了其他类似问题的其他帖子,但他们没有帮我弄清楚在我的情况下该怎么做.我已经读过TimerTasks已经过时了,我应该使用ExecutorService方法; 我不清楚如何实现这个功能.
...timerStep5 = new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (b5) {
cashButton2SignalText.setBackgroundColor(Color.RED);
cashButton2SignalText.setTextColor(Color.WHITE);
b5=false;
} else {
cashButton2SignalText.setBackgroundColor(Color.WHITE);
cashButton2SignalText.setTextColor(Color.RED);
b5=true;
}
}
});
}
};
timer5.schedule(timerStep5,250,250);
}
public void onPause(){
super.onPause();
timerStep5.cancel();
}
public void onResume(){
super.onResume();
timerStep5.run();
}
Run Code Online (Sandbox Code Playgroud) 我正在处理的应用程序中有几个EditText对象,需要学习如何在用户输入文本时关闭键盘,以便屏幕上的键盘阻挡的按钮再次可见,并准备好执行操作.
在Xcode中,我使用ResignFirstResponder方法来执行此操作,例如,用户在键盘上单击"完成"按钮.我认为这在Android中也是可行的,但我不确定.我感谢任何帮助!