1

这是我正在处理的代码:

    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 秒,然后返回其默认图像资源。我怎样才能做到这一点?我正在考虑按照我上面的帖子使用线程,但我似乎想不出实现它的好方法。任何帮助深表感谢。谢谢。

4

1 回答 1

4

用于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
        }
    }
});
于 2013-07-27T17:02:15.407 回答