单击按钮后如何保持按钮按下状态?

发布于 2024-10-13 04:06:54 字数 480 浏览 1 评论 0原文

可能的重复:
Android。如何让按钮保持显示为“已按下”,直到该按钮创建的操作完成为止?

我有一个按钮,我希望当我按下它时,它保持按下状态(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你是暖光i 2024-10-20 04:06:54

我遇到了带有自定义背景的按钮的问题,最终使用了 为此选择的状态。该状态可用于所有视图。

要使用此功能,您必须将自定义按钮背景定义为 状态列表

<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" ... />
...

在代码中,您可以切换到(de- )onClick 侦听器中的选定状态:

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

激活状态 更符合预期含义,但仅适用于 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:

<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:

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

In your code you can switch to the (de-)selected state in your onClick listener:

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.

躲猫猫 2024-10-20 04:06:54

使用以下代码。这很有用。

mycodes_Button.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        mycodes_Button.setPressed(true);
        return true;
    }
});

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;
    }
});
篱下浅笙歌 2024-10-20 04:06:54

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文