当button 的属性被设置focusableInTouchMode 为true的时候 鼠标的点击事件就不执行.去掉这个属性即可
原因:在View的OnTouchEvent 中的action_up事件中有如下逻辑
boolean focusTaken = false;
if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
focusTaken = requestFocus();
}
if (prepressed) {
// The button is being released before we actually
// showed it as pressed. Make it show the pressed
// state now (before scheduling the click) to ensure
// the user sees it.
setPressed(true, x, y);
}
if (!mHasPerformedLongPress && !mIgnoreNextUpEvent) {
// This is a tap, so remove the longpress check
removeLongPressCallback();
// Only perform take click actions if we were in the pressed state
if (!focusTaken) {
// Use a Runnable and post this rather than calling
// performClick directly. This lets other visual state
// of the view update before click actions start.
if (mPerformClick == null) {
mPerformClick = new PerformClick();
}
if (!post(mPerformClick)) {
performClickInternal();
}
}
}
private boolean performClickInternal() {
// Must notify autofill manager before performing the click actions to avoid
//scenarios where
// the app has a click listener that changes the state of views the autofill
//service might
// be interested on.
notifyAutofillManagerOnClick();
return performClick();
}
public boolean performClick() {
// We still need to call this method to handle the cases where performClick() was
//called
// externally, instead of through performClickInternal()
notifyAutofillManagerOnClick();
final boolean result;
final ListenerInfo li = mListenerInfo;
if (li != null && li.mOnClickListener != null) {
playSoundEffect(SoundEffectConstants.CLICK);
li.mOnClickListener.onClick(this);
result = true;
} else {
result = false;
}
sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
notifyEnterOrExitForAutoFillIfNeeded(true);
return result;
}
performClickInternal会执行 performClick()方法,
performClick()会执行 li.mOnClickListener.onClick(this); 这个onclick就是我们定义的onclick事件
由于isFoucusableInTouchmode=true 所以导致focusTaken=true
这样performClickInternal()方法 就不会执行,
最终导致Button的onclick事件不执行