Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 具有多个按钮和不同id的CustomView侦听器(回调)_Java_Android - Fatal编程技术网

Java 具有多个按钮和不同id的CustomView侦听器(回调)

Java 具有多个按钮和不同id的CustomView侦听器(回调),java,android,Java,Android,我正在尝试制作一个幻灯片按钮库几乎所有内容都已完成,但在onClickListener 问题是当我在xml中使用两次或两次以上不同id的自定义视图(即SlideButton)时,以及在MainActivity中实现该侦听器时 所以问题是,当我点击任何按钮时,只有一个SlideButton(即slideButton1)被调用,即使我将按钮与id进行比较,比如if(slideButton1.getId()==R.id.btnUp),但什么也没有发生 简而言之,我在xml中多次使用不同id的Custo

我正在尝试制作一个幻灯片按钮库几乎所有内容都已完成,但在onClickListener
问题是当我在xml中使用两次或两次以上不同id的自定义视图(即SlideButton)时,以及在MainActivity中实现该侦听器时

所以问题是,当我点击任何按钮时,只有一个SlideButton(即slideButton1)被调用,即使我将按钮与id进行比较,比如if(slideButton1.getId()==R.id.btnUp),但什么也没有发生

简而言之,我在xml中多次使用不同id的CustomView,并在MainActivity中使用,然后始终只调用一个按钮

public class SlideButton extends LinearLayout implements View.OnClickListener
{
    LinearLayout linearLayout;
    TextView centerText;
    ImageButton slidingButton;

    TypedArray typedArray;
    //shapes
    GradientDrawable buttonCollapse,buttonExpand,backgroundButton;
    String mAttrText;
    Drawable disabledDrawable,enabledDrawable;
    int mAttrId,mAttrButtonPadding,mAttrTextPadding,
            mAttrTextSize,mAttrTextColor,mAttrRadius,mAttrCollapse,
            mAttrStrokeWidth,mAttrStrokeColor,mAttrExpand,mAttrBackColor,
            mAttrBackStrokeColor,mAttrBackStrokeWidth;

    //it is the variable that says that the button is expand or not.
    private boolean active;
    //it is the initial width of the button. we need to save it so we can back to the initial position.
    private int initialButtonWidth;
    SlideListener listener;

    public SlideButton(Context context) {
        super(context);
        init(context,null,-1,-1);
    }

    public SlideButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, -1, -1);
    }

    public SlideButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr, -1);
    }

    @TargetApi(21)
    public SlideButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs, defStyleAttr, defStyleRes);
    }

    public void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {

        attrValue(context,attrs);
        btnCollapse();
        btnExpand();
        backButton();
        dynamicLayout(context);
        dynamicButton(context);
        dynamicTextView(context);
        new ShineEffect(centerText);

        try {
            listener = (SlideListener) context;
        }catch (ClassCastException e){
            e.printStackTrace();
        }
    }

    /*
  -----------------------------------------getting attr values---------------------------------------------------------
     */
    private void attrValue(Context context,AttributeSet attrs){

        typedArray=context.getTheme().obtainStyledAttributes(attrs,R.styleable.SlideButton,0,0);
        try {

            //attribute for text
            mAttrText = typedArray.getString(R.styleable.SlideButton_text);
            mAttrTextPadding=typedArray.getDimensionPixelSize(R.styleable.SlideButton_textPadding,0);
            mAttrTextSize=typedArray.getDimensionPixelSize(R.styleable.SlideButton_textSize,0);
            mAttrTextColor=typedArray.getColor(R.styleable.SlideButton_textColor, Color.GRAY);

            //attribute for  button
            mAttrButtonPadding =typedArray.getDimensionPixelSize(R.styleable.SlideButton_buttonPadding,0);
            mAttrRadius=typedArray.getDimensionPixelSize(R.styleable.SlideButton_cornerRadius,45);
            mAttrCollapse=typedArray.getColor(R.styleable.SlideButton_collapseColor,Color.WHITE);
            mAttrExpand=typedArray.getColor(R.styleable.SlideButton_expandColor,Color.WHITE);
            disabledDrawable=typedArray.getDrawable(R.styleable.SlideButton_collapseIcon);
            enabledDrawable=typedArray.getDrawable(R.styleable.SlideButton_expandIcon);
            mAttrStrokeColor=typedArray.getColor(R.styleable.SlideButton_strokeColor,Color.parseColor("#ee071a32"));
            mAttrStrokeWidth=typedArray.getDimensionPixelSize(R.styleable.SlideButton_strokeWidth,3);

            //attribute for background
            mAttrBackColor=typedArray.getColor(R.styleable.SlideButton_backColor,Color.parseColor("#ee071a32"));
            mAttrBackStrokeColor=typedArray.getColor(R.styleable.SlideButton_backStrokeColor,Color.WHITE);
            mAttrBackStrokeWidth=typedArray.getDimensionPixelSize(R.styleable.SlideButton_backStrokeWidth,3);
        }
        finally {
            typedArray.recycle();
        }
    }

    /*
  -----------------------------------------btn collapse shape---------------------------------------------------------
     */

    private void btnCollapse(){
        buttonCollapse = new GradientDrawable();
        buttonCollapse.setShape(GradientDrawable.RECTANGLE);
        buttonCollapse.setCornerRadius(mAttrRadius);
        buttonCollapse.setColor(mAttrCollapse);
        buttonCollapse.setStroke(mAttrStrokeWidth,mAttrStrokeColor);
    }

     /*
  -----------------------------------------btn expand shape---------------------------------------------------------
     */

    private void btnExpand(){
        buttonExpand = new GradientDrawable();
        buttonExpand.setShape(GradientDrawable.RECTANGLE);
        buttonExpand.setCornerRadius(mAttrRadius);
        buttonExpand.setColor(mAttrExpand);
        buttonExpand.setStroke(mAttrStrokeWidth,mAttrStrokeColor);
    }

     /*
  -----------------------------------------btn background shape---------------------------------------------------------
     */

    private void backButton(){
        backgroundButton = new GradientDrawable();
        backgroundButton.setShape(GradientDrawable.RECTANGLE);
        backgroundButton.setCornerRadius(mAttrRadius);
        backgroundButton.setColor(mAttrBackColor);
        backgroundButton.setStroke(mAttrBackStrokeWidth,mAttrBackStrokeColor);
    }

     /*
  -----------------------------------------dynamic linear layout---------------------------------------------------------
     */

    private void dynamicLayout(Context context) {
        linearLayout=new LinearLayout(context);
        //LayoutParams are used by views to tell their parents how big the view wants to be for both in width and height
        linearLayout.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        linearLayout.setBackground(backgroundButton);
        linearLayout.setGravity(Gravity.START);
        //add the view to parent layout
        addView(linearLayout);
    }

     /*
  -----------------------------------------dynamic text view---------------------------------------------------------
     */

    private void dynamicTextView(Context context) {
        centerText=new TextView(context);
        centerText.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
        centerText.setGravity(Gravity.CENTER);
        centerText.setText(mAttrText);
        centerText.setPadding(mAttrTextPadding,0,0,0);
        centerText.setTextSize(mAttrTextSize);
        centerText.setTextColor(mAttrTextColor);
        linearLayout.addView(centerText);
    }

     /*
  -----------------------------------------dynamic image button--------------------------------------------------------
     */

    private void dynamicButton(Context context) {
       slidingButton=new ImageButton(context);
       LayoutParams params=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
       params.setMargins(3,3,3,3);
       slidingButton.setLayoutParams(params);
       slidingButton.setId(mAttrId);
       slidingButton.setImageDrawable(disabledDrawable);
       slidingButton.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
       slidingButton.setPadding(mAttrButtonPadding,mAttrButtonPadding,mAttrButtonPadding,mAttrButtonPadding);
       slidingButton.setBackground(buttonCollapse);
       linearLayout.addView(slidingButton);
       slidingButton.setOnClickListener(this);
    }

     /*
  -----------------------------------------listener---------------------------------------------------------
     */

    @Override
    public void onClick(View view) {

        if(active){
            collapseButton();
        }
        else {
            initialButtonWidth=slidingButton.getWidth();
            expandButton();
        }
    }

    public interface SlideListener{
        void onClick(boolean active);
    }

    public void setOnSlideListener(SlideListener listener){
        this.listener=listener;
    }

     /*
  -----------------------------------------expand animation---------------------------------------------------------
     */

    private void expandButton()
    {
        final ValueAnimator widthAnimator = ValueAnimator.ofInt(slidingButton.getWidth(),getWidth()-6);
        widthAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation)
            {
                ViewGroup.LayoutParams params = slidingButton.getLayoutParams();
                params.width = (Integer) widthAnimator.getAnimatedValue();
                slidingButton.setLayoutParams(params);
                slidingButton.setEnabled(false);
            }
        });

        widthAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                active = true;

                if (listener!=null) {
                    listener.onClick(true);
                }
                slidingButton.setEnabled(true);
                slidingButton.setImageDrawable(enabledDrawable);
                slidingButton.setBackground(buttonExpand);
            }
        });
        widthAnimator.start();
    }

     /*
  -----------------------------------------collapse animation---------------------------------------------------------
     */

    private void collapseButton() {

        final ValueAnimator widthAnimator = ValueAnimator.ofInt(slidingButton.getWidth(), initialButtonWidth);
        widthAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation)
            {
                ViewGroup.LayoutParams params =  slidingButton.getLayoutParams();
                params.width = (Integer) widthAnimator.getAnimatedValue();
                slidingButton.setLayoutParams(params);
                slidingButton.setEnabled(false);
            }
        });

        widthAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                active = false;

                if(listener!=null){
                    listener.onClick(false);
                }
                slidingButton.setEnabled(true);
                if(disabledDrawable!=null){
                    slidingButton.setImageDrawable(disabledDrawable);
                }
                slidingButton.setBackground(buttonCollapse);
            }
        });
        widthAnimator.start();
    }
}
这是我的CustomView课程

public class SlideButton extends LinearLayout implements View.OnClickListener
{
    LinearLayout linearLayout;
    TextView centerText;
    ImageButton slidingButton;

    TypedArray typedArray;
    //shapes
    GradientDrawable buttonCollapse,buttonExpand,backgroundButton;
    String mAttrText;
    Drawable disabledDrawable,enabledDrawable;
    int mAttrId,mAttrButtonPadding,mAttrTextPadding,
            mAttrTextSize,mAttrTextColor,mAttrRadius,mAttrCollapse,
            mAttrStrokeWidth,mAttrStrokeColor,mAttrExpand,mAttrBackColor,
            mAttrBackStrokeColor,mAttrBackStrokeWidth;

    //it is the variable that says that the button is expand or not.
    private boolean active;
    //it is the initial width of the button. we need to save it so we can back to the initial position.
    private int initialButtonWidth;
    SlideListener listener;

    public SlideButton(Context context) {
        super(context);
        init(context,null,-1,-1);
    }

    public SlideButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, -1, -1);
    }

    public SlideButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr, -1);
    }

    @TargetApi(21)
    public SlideButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs, defStyleAttr, defStyleRes);
    }

    public void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {

        attrValue(context,attrs);
        btnCollapse();
        btnExpand();
        backButton();
        dynamicLayout(context);
        dynamicButton(context);
        dynamicTextView(context);
        new ShineEffect(centerText);

        try {
            listener = (SlideListener) context;
        }catch (ClassCastException e){
            e.printStackTrace();
        }
    }

    /*
  -----------------------------------------getting attr values---------------------------------------------------------
     */
    private void attrValue(Context context,AttributeSet attrs){

        typedArray=context.getTheme().obtainStyledAttributes(attrs,R.styleable.SlideButton,0,0);
        try {

            //attribute for text
            mAttrText = typedArray.getString(R.styleable.SlideButton_text);
            mAttrTextPadding=typedArray.getDimensionPixelSize(R.styleable.SlideButton_textPadding,0);
            mAttrTextSize=typedArray.getDimensionPixelSize(R.styleable.SlideButton_textSize,0);
            mAttrTextColor=typedArray.getColor(R.styleable.SlideButton_textColor, Color.GRAY);

            //attribute for  button
            mAttrButtonPadding =typedArray.getDimensionPixelSize(R.styleable.SlideButton_buttonPadding,0);
            mAttrRadius=typedArray.getDimensionPixelSize(R.styleable.SlideButton_cornerRadius,45);
            mAttrCollapse=typedArray.getColor(R.styleable.SlideButton_collapseColor,Color.WHITE);
            mAttrExpand=typedArray.getColor(R.styleable.SlideButton_expandColor,Color.WHITE);
            disabledDrawable=typedArray.getDrawable(R.styleable.SlideButton_collapseIcon);
            enabledDrawable=typedArray.getDrawable(R.styleable.SlideButton_expandIcon);
            mAttrStrokeColor=typedArray.getColor(R.styleable.SlideButton_strokeColor,Color.parseColor("#ee071a32"));
            mAttrStrokeWidth=typedArray.getDimensionPixelSize(R.styleable.SlideButton_strokeWidth,3);

            //attribute for background
            mAttrBackColor=typedArray.getColor(R.styleable.SlideButton_backColor,Color.parseColor("#ee071a32"));
            mAttrBackStrokeColor=typedArray.getColor(R.styleable.SlideButton_backStrokeColor,Color.WHITE);
            mAttrBackStrokeWidth=typedArray.getDimensionPixelSize(R.styleable.SlideButton_backStrokeWidth,3);
        }
        finally {
            typedArray.recycle();
        }
    }

    /*
  -----------------------------------------btn collapse shape---------------------------------------------------------
     */

    private void btnCollapse(){
        buttonCollapse = new GradientDrawable();
        buttonCollapse.setShape(GradientDrawable.RECTANGLE);
        buttonCollapse.setCornerRadius(mAttrRadius);
        buttonCollapse.setColor(mAttrCollapse);
        buttonCollapse.setStroke(mAttrStrokeWidth,mAttrStrokeColor);
    }

     /*
  -----------------------------------------btn expand shape---------------------------------------------------------
     */

    private void btnExpand(){
        buttonExpand = new GradientDrawable();
        buttonExpand.setShape(GradientDrawable.RECTANGLE);
        buttonExpand.setCornerRadius(mAttrRadius);
        buttonExpand.setColor(mAttrExpand);
        buttonExpand.setStroke(mAttrStrokeWidth,mAttrStrokeColor);
    }

     /*
  -----------------------------------------btn background shape---------------------------------------------------------
     */

    private void backButton(){
        backgroundButton = new GradientDrawable();
        backgroundButton.setShape(GradientDrawable.RECTANGLE);
        backgroundButton.setCornerRadius(mAttrRadius);
        backgroundButton.setColor(mAttrBackColor);
        backgroundButton.setStroke(mAttrBackStrokeWidth,mAttrBackStrokeColor);
    }

     /*
  -----------------------------------------dynamic linear layout---------------------------------------------------------
     */

    private void dynamicLayout(Context context) {
        linearLayout=new LinearLayout(context);
        //LayoutParams are used by views to tell their parents how big the view wants to be for both in width and height
        linearLayout.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        linearLayout.setBackground(backgroundButton);
        linearLayout.setGravity(Gravity.START);
        //add the view to parent layout
        addView(linearLayout);
    }

     /*
  -----------------------------------------dynamic text view---------------------------------------------------------
     */

    private void dynamicTextView(Context context) {
        centerText=new TextView(context);
        centerText.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
        centerText.setGravity(Gravity.CENTER);
        centerText.setText(mAttrText);
        centerText.setPadding(mAttrTextPadding,0,0,0);
        centerText.setTextSize(mAttrTextSize);
        centerText.setTextColor(mAttrTextColor);
        linearLayout.addView(centerText);
    }

     /*
  -----------------------------------------dynamic image button--------------------------------------------------------
     */

    private void dynamicButton(Context context) {
       slidingButton=new ImageButton(context);
       LayoutParams params=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
       params.setMargins(3,3,3,3);
       slidingButton.setLayoutParams(params);
       slidingButton.setId(mAttrId);
       slidingButton.setImageDrawable(disabledDrawable);
       slidingButton.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
       slidingButton.setPadding(mAttrButtonPadding,mAttrButtonPadding,mAttrButtonPadding,mAttrButtonPadding);
       slidingButton.setBackground(buttonCollapse);
       linearLayout.addView(slidingButton);
       slidingButton.setOnClickListener(this);
    }

     /*
  -----------------------------------------listener---------------------------------------------------------
     */

    @Override
    public void onClick(View view) {

        if(active){
            collapseButton();
        }
        else {
            initialButtonWidth=slidingButton.getWidth();
            expandButton();
        }
    }

    public interface SlideListener{
        void onClick(boolean active);
    }

    public void setOnSlideListener(SlideListener listener){
        this.listener=listener;
    }

     /*
  -----------------------------------------expand animation---------------------------------------------------------
     */

    private void expandButton()
    {
        final ValueAnimator widthAnimator = ValueAnimator.ofInt(slidingButton.getWidth(),getWidth()-6);
        widthAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation)
            {
                ViewGroup.LayoutParams params = slidingButton.getLayoutParams();
                params.width = (Integer) widthAnimator.getAnimatedValue();
                slidingButton.setLayoutParams(params);
                slidingButton.setEnabled(false);
            }
        });

        widthAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                active = true;

                if (listener!=null) {
                    listener.onClick(true);
                }
                slidingButton.setEnabled(true);
                slidingButton.setImageDrawable(enabledDrawable);
                slidingButton.setBackground(buttonExpand);
            }
        });
        widthAnimator.start();
    }

     /*
  -----------------------------------------collapse animation---------------------------------------------------------
     */

    private void collapseButton() {

        final ValueAnimator widthAnimator = ValueAnimator.ofInt(slidingButton.getWidth(), initialButtonWidth);
        widthAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation)
            {
                ViewGroup.LayoutParams params =  slidingButton.getLayoutParams();
                params.width = (Integer) widthAnimator.getAnimatedValue();
                slidingButton.setLayoutParams(params);
                slidingButton.setEnabled(false);
            }
        });

        widthAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                active = false;

                if(listener!=null){
                    listener.onClick(false);
                }
                slidingButton.setEnabled(true);
                if(disabledDrawable!=null){
                    slidingButton.setImageDrawable(disabledDrawable);
                }
                slidingButton.setBackground(buttonCollapse);
            }
        });
        widthAnimator.start();
    }
}
这里是使用自定义视图的xml

<com.greenlab.hackme.slidebutton.SlideButton
        android:id="@+id/btnUp"

        android:layout_width="260dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        custom:buttonPadding="20dp"
        custom:cornerRadius="45dp"
        custom:collapseColor="@android:color/white"
        custom:expandColor="@android:color/white"
        custom:collapseIcon="@drawable/collapsed"
        custom:expandIcon="@drawable/expanded"
        custom:strokeColor="#ee071a32"
        custom:strokeWidth="3dp"
        custom:backStrokeColor="@android:color/white"
        custom:backColor="#ee071a32"
        custom:backStrokeWidth="2dp"
        custom:text="Tap to Activate"
        custom:textPadding="20dp"
        custom:textSize="8dp"
        custom:textColor="#a39c9c">
    </com.greenlab.hackme.slidebutton.SlideButton>

    <com.greenlab.hackme.slidebutton.SlideButton
        android:id="@+id/btnDown"

        android:layout_width="260dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="100dp"
        custom:buttonPadding="15dp"
        custom:cornerRadius="25dp"
        custom:collapseColor="@android:color/white"
        custom:expandColor="@android:color/white"
        custom:collapseIcon="@drawable/collapsed"
        custom:expandIcon="@drawable/expanded"
        custom:strokeColor="#ee071a32"
        custom:strokeWidth="3dp"

        custom:backStrokeColor="@android:color/white"
        custom:backColor="#ee071a32"
        custom:backStrokeWidth="2dp"

        custom:text="Tap to Activate"
        custom:textPadding="20dp"
        custom:textSize="8dp"
        custom:textColor="#b3a9a9">
    </com.greenlab.hackme.slidebutton.SlideButton>
这里是attribute.xml文件

<resources>
    <declare-styleable name="SlideButton">

        <attr name="text" format="string"/>
        <attr name="textPadding" format="dimension"/>
        <attr name="textSize" format="dimension"/>
        <attr name="textColor" format="color"/>

        <attr name="buttonPadding" format="dimension"/>
        <attr name="cornerRadius" format="dimension"/>
        <attr name="collapseColor" format="color"/>
        <attr name="expandColor" format="color"/>
        <attr name="collapseIcon" format="reference"/>
        <attr name="expandIcon" format="reference"/>
        <attr name="strokeWidth" format="dimension"/>
        <attr name="strokeColor" format="color"/>

        <attr name="backStrokeWidth" format="dimension"/>
        <attr name="backStrokeColor" format="color"/>
        <attr name="backColor" format="color"/>
    </declare-styleable>
</resources>

尝试如下定义回调
接口

public interface SlideListener {
    void onClick(SlideButton button, boolean active);
}
SlideButton
类中的任何地方调用
onClick
,并传递当前引用:

if (listener != null) {
    listener.onClick(SlideButton.this, true/false);
}
然后在
MainActivity
中检查单击按钮的id,如下所示:

@Override
public void onClick(SlideButton button, boolean active) {

    if(button.getId() == R.id.btnUp) {
        if(active){
            Toast.makeText(this, "Active Up", Toast.LENGTH_SHORT).show();
        }
        else {
            Toast.makeText(this, "Deactivate Up", Toast.LENGTH_SHORT).show();
        }
    } else if(button.getId() == R.id.btnDown) {
        if(active){
            Toast.makeText(this, "Active Down", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "Deactivate Down", Toast.LENGTH_SHORT).show();
        }
    }
}

非常感谢你和Stackoverflow如果你不介意的话,请告诉我如何在android中像你一样出色,这样我也可以在Stackoverflow上再次提供帮助谢谢。不客气,伙计:)你刚才在android中很棒,你在SlideButton中的代码很好,错误总是会发生。如果你继续写代码,你会发现对方的问题很快就会被你解决。