Possible Duplicate:
Android. How do I keep a button displayed as PRESSED until the action created by that button is finished?可能重复:Android。在按钮创建的操作完成之前,如何将按钮显示为PRESSED?
I have a button, and I want that when I press it, it stays as pressed (with the green color on Froyo).
我有一个按钮,我希望当我按下它时,它会保持按下状态(Froyo上的绿色)。
Any help?
mycodes_Button = (Button) findViewById(R.id.mycodes);
...
if (saved_Button.isPressed())
{
saved_Button.setFocusable(true);
}
Something like this?
像这样的东西?
3 个解决方案
#1
33
Use the following code. It's useful.
使用以下代码。这很有用。
mycodes_Button.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mycodes_Button.setPressed(true);
return true;
}
});
#2
41
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:
要使用此功能,您必须将自定义按钮背景定义为状态列表:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="false" android:state_focused="false"
android:state_pressed="false"><bitmap ... /></item>
<item android:state_selected="true"><bitmap ... /></item>
<item android:state_focused="true"><bitmap ... /></item>
<item android:state_pressed="true"><bitmap ... /></item>
</selector>
Then to use that background, let's say it is in /res/drawable/button_bg.xml
in your layout file, you use:
然后使用该背景,假设它位于布局文件中的/res/drawable/button_bg.xml中,您使用:
...
<Button android:background="@drawable/button_bg" ... />
...
In your code you can switch to the (de-)selected state in your onClick listener:
在您的代码中,您可以切换到onClick侦听器中的(取消)选定状态:
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setSelected(true);
// normal click action here
}
});
The activated state matches the intended meaning better, but is only available from Android 3.x and higher.
激活状态更好地匹配预期含义,但仅适用于Android 3.x及更高版本。
#3
4
Would a ToggleButton suit your needs?
ToggleButton会满足您的需求吗?
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.
您可以尝试以编程方式退出触摸模式,但我不推荐它。经过一段时间的习惯后,触摸模式将为用户提供更好的体验。
#1
33
Use the following code. It's useful.
使用以下代码。这很有用。
mycodes_Button.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mycodes_Button.setPressed(true);
return true;
}
});
#2
41
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:
要使用此功能,您必须将自定义按钮背景定义为状态列表:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="false" android:state_focused="false"
android:state_pressed="false"><bitmap ... /></item>
<item android:state_selected="true"><bitmap ... /></item>
<item android:state_focused="true"><bitmap ... /></item>
<item android:state_pressed="true"><bitmap ... /></item>
</selector>
Then to use that background, let's say it is in /res/drawable/button_bg.xml
in your layout file, you use:
然后使用该背景,假设它位于布局文件中的/res/drawable/button_bg.xml中,您使用:
...
<Button android:background="@drawable/button_bg" ... />
...
In your code you can switch to the (de-)selected state in your onClick listener:
在您的代码中,您可以切换到onClick侦听器中的(取消)选定状态:
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setSelected(true);
// normal click action here
}
});
The activated state matches the intended meaning better, but is only available from Android 3.x and higher.
激活状态更好地匹配预期含义,但仅适用于Android 3.x及更高版本。
#3
4
Would a ToggleButton suit your needs?
ToggleButton会满足您的需求吗?
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.
您可以尝试以编程方式退出触摸模式,但我不推荐它。经过一段时间的习惯后,触摸模式将为用户提供更好的体验。