- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我定义了以下按钮:
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/
抱歉,我对 Java 和 LibGDX 很陌生,但是我遇到了问题。 我的 Pong 游戏中有两个按钮。如果score1或score2等于5,则游戏结束。这是通过使球和桨移出屏幕,然后初始化名为draw
我有一个由文本框和自定义小部件组成的复合小部件,然后将其添加到 VerticalPanel 中。我使用 Raphael JS 创建了这个自定义小部件。 现在我希望这个复合小部件能够识别单击事件和 ct
我想给一个ListView添加一个ListView的ClickListener,但是我不知道怎么办?这是我创建适配器的代码的结尾: //Get all the values from Name, an
我在名为 profile_sliding_menu.xml 的 .xml 文件中定义了一个布局 View : .......... 我正在通过代码创建 Sliding
好的 friend 们。所以我让我的应用程序执行一些基本功能并且对自己非常满意。我的下一个目标是设置一个按钮和备用 Activity/布局。我打算继续进行该 Activity ,并且它在按钮代码之外工
我有一个关于使用自定义适配器为 ListView 创建点击监听器的最佳实践的问题。所以,我的问题是:在哪里更好地实现点击监听器?在自定义适配器中还是在 Activity 中实现 setOnItemCl
我想显示一个 Seekbar,当我选中一个复选框时,当我取消选中该复选框时,Seekbar 将消失。 但它只对我有用,当我取消选中复选框时,Seekbar 通过单击复选框出现但不会消失。 我用 Cli
我定义了以下按钮: public class PressedButton extends Button { private static final int[] STATE_PRESSED = {R.
我有一个带有 ClickListener 事件的按钮和一个内部方法。该方法负责在单击按钮后在 Vaadin GirdLayout 中添加新行。 private GridLayout buildDedu
在处理监听器类并实现多个触摸手势的处理时,我遇到了一个可能的错误。 代码实现 public class MyListener extends ClickListener { private L
我设置了几个 ClickListener 事件(使用 setOnClickListener 函数)。现在我想暂时禁用(或停用、删除、删除)其中一些,但我找不到方法。 提前致谢。 最佳答案 调用setO
我开发了一个 2D 游戏,并使用 OrthographicCamera 和 Viewport 将虚拟板调整为实际显示尺寸。我将图像添加到舞台并使用 ClickListener 来检测点击。它工作正常,
我想在单击此按钮时更改按钮样式。这是我的解决方案: show(){ ... Gdx.input.setInputProcessor(stage); stage.add(myButto
我想让一条线可点击。 我在 Canvas 中使用 onDraw 编写了代码。 我尝试过,但是,drawline(line) 不是一个对象,所以 onclicklistener 无法连接到 line。
我需要创建一种算法。我有它的创建布局的方法,在这个布局中我需要在这个布局中指定 clickListener 。我想不出任何办法,请帮我做这个。 此方法创建布局 public LinearLayout
我已经创建了一个RecyclerView,并且在适配器中我希望创建一个ClickListener。我希望 RecyclerView 中的每个项目在单击后发送唯一的响应。换句话说,一次点击会为每个特定的
该应用程序具有初始消息屏幕,该屏幕在首次运行时显示,其中使用日期选择器从用户处获取日期,然后从下一次开始直接转到 main.xml。这是我试过的逻辑在 onCreate() 中 if() {
我想了解微调器是否已被单击,我对选择的项目不感兴趣,仅当用户已单击微调器时。当我尝试它时,会抛出一个编译器错误,显示“不要在 AdapterView 上调用 setOnClickListener ..
我在编写一个简单的 Android 应用程序时遇到了一个小问题。在我的主要 Activity 中,有一个按钮和与之关联的回调在 AsynchTask 中获取用户的 json 文件。获取文件后,我在 t
我有一个带有 SlidingDrawer 的 RelativeLayout。我正在使用抽屉作为菜单。当您单击抽屉中的某个项目时,它会将一个 ImageView 添加到 RelativeLayout。我
我是一名优秀的程序员,十分优秀!