我有一个带有按钮的活动,其图像不断变化。现在单击带有特定图像的按钮,此时应该给出适当的祝酒词。我正在使用 timerTask 来更改按钮图像。单击按钮中出现的不同图像如何获得不同的响应(这里我的按钮是图像视图)。
我的代码如下
活动
public class MainActivity extends Activity {
public ImageView ib;
int[] images= {R.drawable.alarm,R.drawable.ic_launcher,R.drawable.music_player,R.drawable.onetouchpanel,R.drawable.recorder};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ib=(ImageView) findViewById(R.id.imageButton1);
int delay = 0; // delay for 0 sec.
int period = 2000; // repeat every 2 seconds.
Timer timer = new Timer();
MyTimerTask myTask = new MyTimerTask();
timer.schedule(myTask, delay,period);
ib.setDrawingCacheEnabled(true);
//ib.measure(Me, heightMeasureSpec)
ib.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.d("ayaz","button clicked");
}
});
}
class MyTimerTask extends TimerTask {
int i=0;
public void run() {
runOnUiThread( new Runnable() {
public void run() {
// ERROR
ib.setImageResource(images[i]);
i++;
if(i>images.length-1)
{
i=0;
}
}
});
}
}