How to change an ImageButton image after a while?
本问题已经有最佳答案,请猛点这里访问。
在android studio中,如何在指定的时间后更改ImageButton图像而不触摸它?
谢谢!
嘿,将这个例子与线程一起使用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | public class MainActivity extends AppCompatActivity { private ImageButton button; private boolean flagChange = false; private Activity mActivity; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); mActivity = this; button = findViewById(R.id.button1); //thread new Thread(new Runnable() { @Override public void run() { //always run background while(true){ SystemClock.sleep(2000);//2 seconds sleep //update Android UI on Main Thread mActivity.runOnUiThread(new Runnable() { @Override public void run() { if (!flagChange) { button.setImageResource(R.drawable.ic_launcher_background); flagChange = true; }else{ button.setImageResource(R.drawable.ic_drop); flagChange = false; } } }); } } }).start(); //start thread } |
你写过逻辑吗?如果您写了什么,可以帮助您更正代码。编写一个定时器,并在时间过去后改变图像。