Android:如何创建幻灯片(开/关)按钮
我想创建一个具有两种状态的滑动按钮(= switch ):打开和关闭,因此用户必须按下按钮并滑动它来更改状态(类似于解锁滑块,但不能跨整个屏幕)。你知道该怎么做吗?我真的很努力地寻找答案,但没有成功。
多谢!
I'd like to create a slide button (= something as switch ) with two states: on and off so user will have to press the button and slide it to change the state (something similar as unlock slider but not cross whole screen). Do you have any idea how to do it? I really tried to find the answer but I haven't been successful.
Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
//在您的布局设计中,下面一行
// 在您的活动中调用此
// 这将像 iphone 样式一样切换开关按钮
//in your layout design the below line
// in your activity call this
// this will switch like iphone style on off toggle button
好吧,如果您的目标 SDK 高于 4.0(Ice奶油三明治)。因此,对于其他将面临同样问题的人来说,请看一下。 :)
Well it seems that Switch component is the best solution if your target SDK is higher than 4.0 (Ice Cream Sandwich). So for the others who will face the same problem look at it. :)
您可以通过使用复选框或切换按钮来实现此目的。下面是一个示例
xml 文件,
可绘制 chec_box_on_off 文件,
您将获得像打开关闭按钮一样的效果,并且您还可以检查复选框是否打开或关闭。
java代码
类似,您也可以使用ToggleButton来实现这一点。
You can achieve this by use check box or ToggleButton. Below is an example
xml file
drawable chec_box_on_off file is
you will get like on off button and also you can check whether checkbox is on or off.
the java code is
Similarly, you can also achieve this using ToggleButton.