android - 自定义按钮的 ClickListener

标签 android button onclicklistener

我定义了以下按钮:

public class PressedButton extends Button {

private static final int[] STATE_PRESSED = {R.attr.state_pressed};
private static final int[] STATE_UNPRESSED = {R.attr.state_unpressed};
private boolean mIsPressed = false;
private boolean mIsUnpressed = false;

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

public PressedButton(Context context) {
    super(context);
}

public void setPressed(boolean isPressed) {mIsPressed = isPressed;}
public void setUnpressed(boolean isUnpressed) {mIsUnpressed = isUnpressed;}

@Override
protected int[] onCreateDrawableState(int extraSpace) {
    final int[] drawableState = super.onCreateDrawableState(extraSpace + 2);
    if (mIsPressed) {
        mergeDrawableStates(drawableState, STATE_PRESSED);
    }
    if (mIsUnpressed) {
        mergeDrawableStates(drawableState, STATE_UNPRESSED);
    }
    return drawableState;
}

然后,在主 Activity 中我这样调用它:

PressedButton btn01=(PressedButton)findViewById(R.id.buttonP1);

当我尝试像这样设置点击监听器时:

btn01.setOnClickListener(new View.OnClickListener() {
        //@Override
        public void onClick(View v) {
            typeLogin = 0;
            Log.e("FINGERPRINT: ", "TypeLogin set to "+ new Integer(typeLogin).toString());
        }
    });

当我按下它时没有任何反应,按钮显示正确但是当我点击它时它没有输入代码,值得注意的是当我使用(按钮)而不是(PressedButton)时它完美地工作,所以我想知道我的自定义类做错了什么。

一些更多的信息,我在这里使用选定的答案来创建一个带有状态的自定义按钮:How to add a custom button state ,这是我的 attrs.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="buttonPressed">
        <attr name="state_pressed" format="boolean" />
        <attr name="state_unpressed" format="boolean" />
    </declare-styleable>
</resources>

这是 pressed_buton.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.fgtit.fingermap">
    <item
        app:state_pressed="true"
        app:state_unpressed="false"
        android:drawable="@drawable/pressed" />
    <item
        app:state_pressed="false"
        app:state_unpressed="true"
        android:drawable="@drawable/unpressed" />
</selector>

最后,它在 MainActivity 中使用的按钮如下所示:

    <com.fgtit.utils.PressedButton
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res/com.fgtit.fingermap"
        android:id="@+id/buttonP1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableTop="@drawable/msign"
        android:height="100dp"
        android:paddingTop="20dp"
        android:text="@string/action_btn1"
        android:textColor="@color/darkgreen"
        android:width="180dp"
        app:state_pressed="false"
        app:state_unpressed="true"
        android:background="@drawable/pressed"/>

最佳答案

让您的PressedButton 类实现OnClickListener 并实现它。

以下代码:

public class YourButton extends Button implements OnClickListener {

     YourButton(stuff) { 
         //bla bla bla
         setOnClickListener(this);
      }

    // bla bla bla

    @Override
    public void onClick(View v) {
        // Do something
    }

}

关于android - 自定义按钮的 ClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49740744/

相关文章:

android - 更改 Spinner View 的外观和感觉

android - 在 Android 的指定选项卡中启动新 Activity

android - 如何实现 Material Design 规定的 "parent-to-child"导航转换

ios - 为什么我的按钮不让我的选择器 View 出现?

reactjs - 使用按钮更改另一个类的某些内容的状态

java - 从 onclicklistener android 访问变量

android - 将 Android 项目导入 Eclipse 时的项目名称问题

ios - 如何自定义 Google+ 登录按钮

android - 如何将 onClick 的 androidx 数据绑定(bind)到带参数的静态方法

android - 错误 : Cannot resolve symbol 'OnClickListener'