gpt4 book ai didi

android - 自定义按钮的 ClickListener

转载 作者:太空狗 更新时间:2023-10-29 14:40:01 24 4
gpt4 key购买 nike

我定义了以下按钮:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com