android - 如何在 MyButton::OnDraw 中知道状态被按下(聚焦)

标签 android

我正在覆盖按钮类中的 onDraw。

我怎么知道在 onDraw 中按下了按钮?

最佳答案

在 Button 的 onDraw(..) 方法中,您可以简单地检查 this.isPressed()
当您按下按钮时,这应该返回 true

编辑:
您还可以在 Button 的子类中实现触摸事件:

示例:

public class tempButton extends Button {

public tempButton(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
     switch(event.getAction())
     {
        case MotionEvent.ACTION_DOWN:

            //Button Pressed, Change the color of your Button here.

        break;

        case MotionEvent.ACTION_UP:

        break;

        case MotionEvent.ACTION_MOVE:

        break;

     }

    return true;
}

}

希望对您有所帮助。

关于android - 如何在 MyButton::OnDraw 中知道状态被按下(聚焦),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21316214/

相关文章:

安卓异常 : Error inflating class fragment

android - 在 android 中自定义时 Swich 背景图像拉伸(stretch)

java - 如何向 Android 应用程序添加简单的代码

android - 中心标记图标 (Android)

java - @NotThreadSafe 未找到错误

android - 多个项目react-native android构建错误

android - 丹麦语字母无法在 Android TextView 上正确显示

Android 滑动和标签 ui - 什么是 "best practice"和最新的?

java - android套接字连接后卡住

java - 如何从whatsapp获取pdf文件数据到我的android应用程序?