我有一个连接到 onTouch 侦听器的 toggleButton,当我按下按钮时,图像会发生变化,文本也会发生变化。当我放手时,图像应该变回来,文本也应该变回来。除了我放开按钮之外,一切都很好,文本只改变了几分之一秒,然后返回到“打开”文本。图像工作正常,这是为什么呢?
xml:
<ToggleButton
android:id="@+id/PTT_button5"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:text="@string/ptt5"
android:textOn="Push To Talk On"
android:textOff="Push To Talk Off"
android:background="@drawable/btn_lightblue_glossy"
android:textColor="@android:color/white"
android:textSize="15sp"
/>
爪哇:
public boolean onTouch(View v, MotionEvent event) {
if(!serviceConnected) {
return true;
}
int action = event.getAction();
try {
if(action == MotionEvent.ACTION_DOWN) {
service.sendDtmf(callId, KeyEvent.KEYCODE_STAR);
service.sendDtmf(callId, KeyEvent.KEYCODE_9);
((ToggleButton) v).setBackgroundResource(R.drawable.btn_blue_glossy);
((ToggleButton) v).setChecked(true);
} else if (action == MotionEvent.ACTION_UP) {
service.sendDtmf(callId, KeyEvent.KEYCODE_POUND);
service.sendDtmf(callId, KeyEvent.KEYCODE_POUND);
((ToggleButton) v).setBackgroundResource(R.drawable.btn_lightblue_glossy);
((ToggleButton) v).setChecked(false);
}
} catch (RemoteException e) {
Log.e(THIS_FILE, "Cannot ask sip service to send dtmf", e);
}
return false;
}
}