點擊Button后迅速抬起,Button上的文本不會還原
點擊Button后迅速抬起,Button上的文本不會還原,會保持為“松開結束”。這是為什么呢?
相關代碼片段:
????????@Override public?boolean?onTouchEvent(MotionEvent?event)?{ int?action?=?event.getAction(); int?x?=?(int)?event.getX(); int?y?=?(int)?event.getY(); switch?(action)?{ case?MotionEvent.ACTION_DOWN: setBackgroundResource(R.drawable.recorder_button_recording); setText(R.string.button_recording); dialogManager.initDialog(); break; case?MotionEvent.ACTION_MOVE: if?(currentState?==?STATE_RECORDING)?{ if?(wantToCancel(x,?y))?{ changeToState(STATE_CANCEL); } }?else?if?(currentState?==?STATE_CANCEL)?{ if?(!wantToCancel(x,?y))?{ changeToState(STATE_RECORDING); } } break; case?MotionEvent.ACTION_UP: if(!isLongClicked){ reset(); return?super.onTouchEvent(event); } if(timeLength?<?500){ handler.sendEmptyMessage(MSG_DIALOG_TOOSHORT); audioManager.cancel(); handler.sendEmptyMessageDelayed(MSG_DIALOG_DIMISS,?1500); } if?(currentState?==?STATE_RECORDING)?{ handler.sendEmptyMessage(MSG_DIALOG_DIMISS); if(listener?!=?null){ listener.onFinished(audioManager.getFilePath(),?timeLength); } audioManager.release(); }?else?if?(currentState?==?STATE_CANCEL)?{ handler.sendEmptyMessage(MSG_DIALOG_DIMISS); audioManager.cancel(); } reset(); break; } return?super.onTouchEvent(event); } private?void?reset()?{ changeToState(STATE_NORMAL); timeLength?=?0; isLongClicked?=?false; } private?void?changeToState(int?state)?{ if?(currentState?!=?state)?{ switch?(state)?{ case?STATE_NORMAL: currentState?=?STATE_NORMAL; setBackgroundResource(R.drawable.recorder_button_normal); setText(R.string.button_normal); break; case?STATE_RECORDING: currentState?=?STATE_RECORDING; setBackgroundResource(R.drawable.recorder_button_recording); setText(R.string.button_recording); dialogManager.showDialog(STATE_RECORDING); break; case?STATE_CANCEL: currentState?=?STATE_CANCEL; setBackgroundResource(R.drawable.recorder_button_recording); setText(R.string.button_cancel); dialogManager.showDialog(STATE_CANCEL); break; } } }
2016-02-02
ACTION_DOWN的時候就應該把狀態切換成STATE_RECORDING
不然的話等你ACTION_UP時,執行了reset(),changeToState(STATE_NORMAL);而此時的currentState還是STATE_NORMAL。
那你的這句if?(currentState?!=?state){} 就執行不進去了
我也是看代碼分析的,若不正確還請見諒