java - 如何在android上制作一个按下和释放的自定义按钮?

标签 java android button

我知道这里有很多此类问题,但我真的不知道我的代码不起作用的原因。 我正在尝试制作一个具有三种状态的按钮:正常、按下和释放。当我说发布时,我的意思是我想让它像一个toogle按钮一样,状态为activenot active

当我释放按钮时,它会返回到默认状态。我想通过单击更改图像,因为 checkButton 有效。

我试过这个:

http://blog.androgames.net/40/custom-button-style-and-theme/

http://www.gersic.com/blog.php?id=56

Android: How to Create a Custom button widget

http://techdroid.kbeanie.com/2010/03/custom-buttons-on-android.html

custom_buttom.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/focussed" />
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/focussedandpressed" />
    <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/pressed" />
    <item android:drawable="@drawable/default" />
</selector>

布局.xml:

 <Button android:layout_width="wrap_content"
                    android:layout_height="wrap_content" android:id="@+id/menu"
                    android:layout_weight="1" android:background="@drawable/custom_button"></Button>

最佳答案

您正在使用 android:state_focusedandroid:state_pressed

怎么样android:state_checked

关于java - 如何在android上制作一个按下和释放的自定义按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6619070/

相关文章:

android - 如何在 Android 中正确填写来自 admob 的 SMART_BANNER 广告?

javascript - 如何在javascript中动态创建的按钮上调用onclick函数

Java 8 Stream从过滤结果中获取对象

java - 将数组中的字符串复制到字符串变量时遇到问题

java - Spring:使用@Qualifier和@Profile

python - Pygame 按钮单击

excel - 在Excel VBA中查找按钮对象的单元格

Java:运行 Spring 批处理

Android Junit 测试-Mockito 类未找到异常

android - Dagger 2 - 在实现类中注入(inject)接口(interface) - "Attempt to invoke interface method on a null object reference"