单击按钮后如何保持按钮按下状态?
我有一个按钮,我希望当我按下它时,它保持按下状态(Froyo 上为绿色)。
有什么帮助吗?
mycodes_Button = (Button) findViewById(R.id.mycodes);
...
if (saved_Button.isPressed())
{
saved_Button.setFocusable(true);
}
像这样的东西吗?
Possible Duplicate:
Android. How do I keep a button displayed as PRESSED until the action created by that button is finished?
I have a button, and I want that when I press it, it stays as pressed (with the green color on Froyo).
Any help?
mycodes_Button = (Button) findViewById(R.id.mycodes);
...
if (saved_Button.isPressed())
{
saved_Button.setFocusable(true);
}
Something like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了带有自定义背景的按钮的问题,最终使用了 为此选择的状态。该状态可用于所有视图。
要使用此功能,您必须将自定义按钮背景定义为 状态列表:
然后要使用该背景,假设它位于布局文件中的
/res/drawable/button_bg.xml
中,您可以使用:在代码中,您可以切换到(de- )onClick 侦听器中的选定状态:
激活状态 更符合预期含义,但仅适用于 Android 3.x 及更高版本。
I had this issue with a button with a custom background, and ended up using the selected state for this. That state is available for all views.
To use this you have to define a custom button background as a state list:
Then to use that background, let's say it is in
/res/drawable/button_bg.xml
in your layout file, you use:In your code you can switch to the (de-)selected state in your onClick listener:
The activated state matches the intended meaning better, but is only available from Android 3.x and higher.
使用以下代码。这很有用。
Use the following code. It's useful.
ToggleButton 能满足您的需求吗?
从您的评论来看,您似乎不知道 触摸模式< /a>。在这种模式下,这是大多数事情的默认模式,没有焦点(这就是您想要实现的目标),也没有选定的项目。
您可以尝试以编程方式退出触摸模式,但我不推荐这样做。经过一段时间的适应后,触摸模式会给用户带来更好的体验。
Would a ToggleButton suit your needs?
Judging from your comments it seems that you aren't aware of the Touch Mode.In this mode, which is the default for most of the things, there is no focus (which is what you are trying to achieve) and no selected items.
You could try to exit the touch mode programmatically, but I wouldn't recommend it. After a small period of getting used to it, the touch mode will give a much better experience to the users.