问题描述
我创建一个自定义视图,我通常附加一个onClickListener来。我想有这样行为的一些按钮:如果是pressed,应该改变它在的onDraw()方法来定义它的外观。然而,这code不工作:
I have created a custom View which I usually attach an onClickListener to. I'd like to have some button like behavior: if it is pressed, it should alter its appearance which is defined in the onDraw() method. However, this code does not work:
//In my custom View:
@Override
protected void onDraw(Canvas canvas)
{
boolean pressed = isPressed();
//draw depending on the value of pressed
}
//when creating the view:
MyView.setClickable(true);
pressed的值总是假的。怎么了?
pressed always has the value false. What's wrong?
非常感谢!
推荐答案
你有没有考虑只用一个按钮,你想要做什么?你可以使用切换按钮和写XML很短的选择,这将允许您指定图像时pressed或不使用。 this问题可能是对你有所帮助。
have you considered just using a button to do what you want? you could use ToggleButton and write a short selector in xml that will allow you to specify an image to use when pressed or not. this question may be of some help to you.
这篇关于像上的点击行为按键自定义视图/触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!