如何在android中更改按钮onClick的视图

发布于 2024-11-02 14:47:16 字数 185 浏览 0 评论 0原文

在我的应用程序中,我尝试使用计时器计算操作。为了控制这些操作,我使用四个按钮:开始、停止、暂停和恢复。

但我只想显示 2 个按钮。一开始我只有两个按钮“开始”和“暂停”。 单击“开始”按钮时,计时器启动,并立即在“开始”按钮的位置显示“停止”按钮。

我想对其他停止和暂停按钮执行相同的操作。如何做到这一点请帮助我......

In my app I am trying to calculate an operation using timer. For controlling those operations I am using four buttons as Start, Stop, Pause and resume.

But I want to show only 2 buttons. At the beginning I have only two buttons Start and Pause.
When the start button is clicked timer gets started and immediately in Start button's place I want to show the Stop button.

I want to do the same for the other stop and pause buttons. How to do this please help me......

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

滥情稳全场 2024-11-09 14:47:16

使用 ToggleButton 对您来说是一个很好的解决方案。执行以下操作:

ToggleButton first = new ToggleButton(getContext());
    ToggleButton second = new ToggleButton(getContext());
    first.setTextOff("start");
    first.setTextOn("stop");
    second.setTextOff("pause");
    second.setTextOn("resume");

并使用 setOnCheckedChangeListener() 来实施您的操作。

Using ToggleButton is a good solution for you. Do something like:

ToggleButton first = new ToggleButton(getContext());
    ToggleButton second = new ToggleButton(getContext());
    first.setTextOff("start");
    first.setTextOn("stop");
    second.setTextOff("pause");
    second.setTextOn("resume");

and use setOnCheckedChangeListener() to implement your actions.

赠意 2024-11-09 14:47:16

onClick(View v) 中,v 是被点击的按钮。您可以像这样转换它:

Button b = (Button) v;

这样您就可以使用 setText() 更改其文本,并设置另一个侦听器。您可以将备用侦听器声明为活动的成员一次,然后设置它们,而无需每次都重新声明它们。

In your onClick(View v), v is the button that gets clicked. You can cast it like:

Button b = (Button) v;

so you can change its text with setText(), and set another listener. You can declare the alternate listeners once as members of the activity, and set them without re-declaring them each time.

傻比既视感 2024-11-09 14:47:16

您的应用程序需要维护状态,例如“Idle/Stopped”、“In Progress”、“Paused”等。如果您想隐藏按钮,可以使用 View.setVisibility,并在状态更改时(按下其他按钮时)动态显示和隐藏按钮。您需要适当地设置布局,以便按钮在动态显示/隐藏时显示良好

,或者,您可以动态更改按钮的文本及其关联的单击侦听器。此方法不是很理想,因为您可能会遇到这样的情况:您需要不同数量的按钮来表示所有不同的状态,而且您将可变行为与单个控件相关联。此外,您还必须管理点击侦听器,动态添加和删除它们。

Your application needs to maintain states, such as "Idle/Stopped", "In Progress", "Paused", etc. If you want to hide buttons, you can use View.setVisibility, and dynamically show and hide the buttons when your state changes (when other buttons are pressed). You would need to set your layout appropriately so that the buttons display nicely as they are shown/hidden dynamically

Or, you can change the text of the buttons, and their associated click listeners dynamically. This method is not very ideal becuase you may run in to cases where you want different amount of buttons for all your different states, and also, you're associating variable behavior with a single control. Also, you must manage your click listeners, adding and removing them dynamically.

哎呦我呸! 2024-11-09 14:47:16

这是一个简单的实现

public class Demo extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final Button button=(Button)findViewById(R.id.button);

    button.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            button.setText("stop");
        }
    });
    }
}

在 main.xml 中有一个像这样的 Button 小部件,

<Button android:id="@+id/button"
android:text="start"
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
 />

here is a simple implementation

public class Demo extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final Button button=(Button)findViewById(R.id.button);

    button.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            button.setText("stop");
        }
    });
    }
}

In the main.xml have a Button widget like this,

<Button android:id="@+id/button"
android:text="start"
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
 />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文