这是我正在处理的代码:

    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();

    }


基本上,我想做的是当用户单击按钮并且满足条件(即“部门存储”)时,imagebutton将更改其图像资源5秒钟,然后返回其默认图像资源。我怎样才能做到这一点?我正在考虑按照我上面的帖子使用线程,但是我似乎无法想到实现它的好方法。任何帮助深表感谢。谢谢。

最佳答案

为此使用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
        }
    }
});

08-17 11:33