ClickListener用于自定义按钮

时间:2018-04-09 19:58:01

标签: 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);

当我尝试像这样设置Click Listener时:

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"/>

1 个答案:

答案 0 :(得分:0)

制作from concurrent import futures import time import win32api import os class SearchThreader(): def __init__(self): self.allfiles = [] self.harddisks = win32api.GetLogicalDriveStrings().split('\000')[:-1] #skip the folders that shouldn't have files with this extension self.exlude = { "$SysReset", "AMD", "inetpub", "NVIDIA", "PerfLogs", "Windows.old", "Windows", "ProgrammData", "Programm Files (x86)", "Programm Files", "Doc", "Fotos", "Lib", "lib", "libs" "Scripts", "Tools", "bin", "Config", "Logs", "log", "mods", "win" } self.fullThreadSearch() def SearchHarddisk(self, hd): files = [] for root, dirs, files in os.walk(hd, topdown=True): dirs[:] = [d for d in dirs if d not in self.exlude] for f_name in files: file_path = os.path.join(root, f_name) if file_path.endswith(".mp3"): files.append(file_path) print(file_path) return files def fullThreadSearch(self): with futures.ProcessPoolExecutor(max_workers=len(self.harddisks)) as thr: future_objects = [thr.submit(self.SearchHarddisk, harddisk) for harddisk in self.harddisks] self.allfiles = [future.result() for future in future_objects] if __name__ == "__main__": starttime = time.time() ST = SearchThreader() print(ST.allfiles) print(time.time() - starttime) 类工具PressedButton并实施它。

以下代码:

OnClickListener