如何在android中更改按钮onClick的视图
在我的应用程序中,我尝试使用计时器计算操作。为了控制这些操作,我使用四个按钮:开始、停止、暂停和恢复。
但我只想显示 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 ToggleButton 对您来说是一个很好的解决方案。执行以下操作:
并使用 setOnCheckedChangeListener() 来实施您的操作。
Using ToggleButton is a good solution for you. Do something like:
and use setOnCheckedChangeListener() to implement your actions.
在
onClick(View v)
中,v
是被点击的按钮。您可以像这样转换它:这样您就可以使用
setText()
更改其文本,并设置另一个侦听器。您可以将备用侦听器声明为活动的成员一次,然后设置它们,而无需每次都重新声明它们。In your
onClick(View v)
,v
is the button that gets clicked. You can cast it like: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.您的应用程序需要维护状态,例如“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.
这是一个简单的实现
在 main.xml 中有一个像这样的 Button 小部件,
here is a simple implementation
In the main.xml have a Button widget like this,