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 - 如何将数据搜索到 json ListView 中?

android - PopupWindow 如何获得触摸外部事件的通知?

c# - <asp :button> is not working in IE9

JavaScript/jQuery - 有什么方法可以检测点击类型吗?

Android:使用ActivityInstrumentationTestCase2进行测试时如何获取上下文?

android - 在android中通过推送通知发送音频文件

css - 使用 CSS 表 (GTK+3) 更改 GtkToggleToolButton 的文本颜色

button - youtube 视频阻止移动浏览器上的按钮

java - 如何添加 onClickListener 而不是替换监听器?

java - 如何让 ProgressBar 在 OnClickListener 运行时显示?