我有一个按钮在我的活动中显示搜索栏,十秒钟后,我隐藏了搜索栏,但是如果用户在10秒之前按下按钮以隐藏搜索栏,则后延迟必须重新启动或停在使用的代码下方。我如何停止后延迟?
barraBrilho.setVisibility(View.GONE);
brilho.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!gone) {
Animation anim = AnimationUtils.loadAnimation(getActivity(),R.anim.translate_layout_right );
barraBrilho.startAnimation(anim);
barraBrilho.setVisibility(View.GONE);
gone = true;
}
else{ barraBrilho.setVisibility(View.VISIBLE);
Animation anim = AnimationUtils.loadAnimation(getActivity(),R.anim.translate_layout_left);
barraBrilho.startAnimation(anim);
gone = false;
new Handler().postDelayed( new Runnable() {
@Override
public void run() {
Animation anim = AnimationUtils.loadAnimation( getActivity(), R.anim.translate_layout_right );
barraBrilho.startAnimation( anim );
barraBrilho.setVisibility( View.GONE );
gone = true;
}
}, 10000 );
}
}
} );
答案 0 :(得分:0)
当用户单击brilho按钮时,调用handler.removeCallbacks(可运行)。
class ansBtnListener implements ActionListener {
int count = 0;
public void actionPerformed(ActionEvent e) {
// checking answer is correct or wrong
double doubleOfInput = Double.parseDouble(input.getText()); // getting string to double
if (doubleOfInput == result && count==0) {
JOptionPane.showMessageDialog(null, "Correct");
count++;
}
else if (doubleOfInput == result && count==1) {
JOptionPane.showMessageDialog(null, "Great Job!");
count++;
}
else if (doubleOfInput == result && count==2) {
JOptionPane.showMessageDialog(null, "Excellent!");
count++;
}
else if (doubleOfInput == result && count>2) {
JOptionPane.showMessageDialog(null, "Please click on end!");
}
else {
JOptionPane.showMessageDialog(null, "Wrong, Try Again!");
input.setText(" ");
}
}
}
我建议您创建Runnable的子类,并通过WeakReference引用Activity实例
Handler handler = new Handler();
Runnable runnable = new Runnable();
handler.removeCallbacks(runnable);