这是我正在处理的代码:
if(place.equalsIgnoreCase("Department Store")){
Thread thread = new Thread()
{
@Override
public void run() {
t = Toast.makeText(Map.this, "Department Store", Toast.LENGTH_SHORT);
t.setGravity(Gravity.CENTER, 0, 0);
t.show();
}
};
thread.start();
}
基本上,我想做的是当用户点击按钮并且它满足条件(即“百货商店”)时,图像按钮将改变其图像资源5秒钟然后返回其默认图像资源。我怎样才能做到这一点?我正在考虑按照上面的帖子使用线程,但我似乎无法想出实现它的好方法。任何帮助深表感谢。感谢。
答案 0 :(得分:3)
使用Handler().postDelayed()
:
// Original image
// This could have been set in your layout file.
// In this case, you can skip this statement.
imageButton. setImageDrawable(getResources().getDrawable(
R.drawable.some_drawable_id));
someButton.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (someCondition) {
// Change image
imageButton.setImageDrawable(getResources().getDrawable(
R.drawable.some_drawable_that_will_stay_for_5_secs));
// Handler
new Handler().postDelayed(new Runnable() {
public void run() {
// Revert back to original image
imageButton.setImageDrawable(getResources().getDrawable(
R.drawable.some_drawable_id));
}
}, 5000L); // 5000 milliseconds(5 seconds) delay
}
}
});