android - 自定义 View (按钮)背景选择器不起作用

标签 android android-layout android-button android-custom-view android-selector

我有简单的自定义按钮:

public class myButton extends Button {
    public myButton (Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        commonConstructorCode();
    }


    public myButton (Context context, AttributeSet attrs) {
        super(context, attrs);
        commonConstructorCode();
    }

  public myButton (Context context) {
    super(context);
    commonConstructorCode();
  }

      private void commonConstructorCode() {
                this.setOnTouchListener(new OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction(); 

                    if(action == MotionEvent.ACTION_DOWN) 
                    {
                      //Just to be sure that we removed all callbacks, 
                      // which should have occurred in the ACTION_UP
                      removeCallbacks(repeatClickWhileButtonHeldRunnable);

                      //Perform the default click action.
                      performClick();
                      //if(v.isEnabled()){
                      //Schedule the start of repetitions after a one half second delay.
                      postDelayed(repeatClickWhileButtonHeldRunnable, initialRepeatDelay);
                      //} else {
                        //  removeCallbacks(repeatClickWhileButtonHeldRunnable);
                      //}
                    }
                    else if(action == MotionEvent.ACTION_UP) {
                      //Cancel any repetition in progress.
                      removeCallbacks(repeatClickWhileButtonHeldRunnable);
                    }
                //Returning true here prevents performClick() from getting called 
                // in the usual manner, which would be redundant, given that we are 
                // already calling it above.
                return true;
      }
    });
      }
}

带选择器

<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_pressed="true"
           android:drawable="@drawable/button_pressed" /> <!-- pressed -->
     <item android:state_focused="true"
           android:drawable="@android:color/white" /> <!-- focused -->
     <item android:drawable="@drawable/button_standard" /> <!-- default -->
 </selector>

设置

<com.blabla.myButton
    ...
    background="@drawable/button_selector" />

“正常”状态下的背景很好。但其他州根本不起作用。有什么奇怪的 - 当我改变时

<com.blabla.myButton

<Button

选择器工作得很好。

有什么想法吗?

编辑: 我添加了 commonConstructorCode()。

最佳答案

我复制了您的代码并让自定义按钮选择器正常工作,但设置:

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // Blah blah blah....
            return false; // this was return true
        }
    });

根据android文档,public boolean onTouch(),

Returns: True if the listener has consumed the event, false otherwise.

因此,返回 true 表示您的方法已消耗该事件,并且似乎阻止它向上渗透层次结构并触发选择器状态。

关于android - 自定义 View (按钮)背景选择器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16430019/

相关文章:

Java android 文件输入流文件输出流问题

android - 如果布局为 mdpi/ldpi,则停止执行函数?

android:创建在滚动时折叠的布局

Android UrbanAirship 在起飞时崩溃

java - 访问扩展 ArrayList 的对象的元素

使用 Viewpageindicator 和 UniversalImageLoader 的 Android Image Slider

android - 使用约束布局垂直居中多个 View

Android - 状态选择器动画 - 仅动画按下,而不是按下

android - 为什么在android中按下按钮不激活 toast ?

Android 3D按钮文字位置