我有一個ImageButton在我的佈局xml中聲明。我已經告訴它,當它被點擊時,調用某種方法,playSound。延遲onClick事件
在playSound的內部,手機會播放持續一定時間的聲音,準確地說是5277 ms。我希望playSound能夠將ImageButton的ImageResource更改一段時間(5400毫秒),然後將其更改回來。
這裏是我的代碼:
ImageButton dBellButt;
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_main);
dBellButt.setImageResource(R.drawable.doorbell2);
public void playSound(View view) {
dBellButt.setImageResource(R.drawable.doorbell);
dBell.start();
if (vibeOn) {
Vibrator vibe = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibe.vibrate(500);
} else {
// No vibration
}
try {
Thread.sleep(5400);
} catch (InterruptedException e) {
}
dBellButt.setImageResource(R.drawable.doorbell2);
}
doorbell2
是點燃門鈴的圖像。 doorbell
是一個無門鈴的圖像。
我可以切換兩個圖像的位置,它會工作得很好,即:如果我已經創建了門鈴亮起,當按下按鈕時,它將保持亮起直到5400毫秒,之後的圖像變化。如果我在門鈴「亮起」的情況下創建應用程序,那麼我根本無法改變它。
希望這是有道理的。
總之,我無法讓ImageButton更改ImageResource。 Thread.sleep();
後setImageResource
工作正常,但它之前沒有。
這是怎麼回事?
謝謝!
彌敦道
編輯:
想我會附上我的XML,以防萬一。
<ImageButton
android:id="@+id/dBellButton"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:scaleType="fitCenter"
android:padding="0dp"
android:background="@null"
android:onClick="playSound"
android:contentDescription="@string/ImgDesc" />
乾杯