gpt4 book ai didi

android - 点击按钮后如何保持按下状态?

转载 作者:IT老高 更新时间:2023-10-28 21:47:48 31 4
gpt4 key购买 nike

Possible Duplicate:
Android. How do I keep a button displayed as PRESSED until the action created by that button is finished?

我有一个按钮,当我按下它时,我希望它保持按下状态(Froyo 上为绿色)。

有什么帮助吗?

mycodes_Button = (Button) findViewById(R.id.mycodes);
...
if (saved_Button.isPressed())
{
saved_Button.setFocusable(true);
}

这样的?

最佳答案

我在使用自定义背景的按钮时遇到了这个问题,最终使用了 selected state为了这。该状态适用于所有 View 。

要使用它,您必须将自定义按钮背景定义为 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>

然后要使用该背景,假设它在您的布局文件中的 /res/drawable/button_bg.xml 中,您可以使用:

...
<Button android:background="@drawable/button_bg" ... />
...

在您的代码中,您可以在 onClick 监听器中切换到(取消)选中状态:

myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setSelected(true);
// normal click action here
}
});

activated state更好地匹配预期含义,但仅适用于 Android 3.x 及更高版本。

关于android - 点击按钮后如何保持按下状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4747311/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com