仿IOS Switch控件
来源:互联网 发布:淘宝订单险坑卖家 编辑:程序博客网 时间:2024/05/29 17:50
该篇文章从eoeAndroid搬迁过来的,原文地址:仿IOS Switch控件
前几天写了一个仿ios的segmentcontrol控件( 仿 ios segmentcontrol ),今天突然兴致来了,就写了一个仿ios 的switch控件,由于本人不是学习ps的,所以从ios上截取的图片可能不太完美,希望各位能够见谅。
废话不多少,上代码: 首先是得到自定义的属性,也就一个自定义的属性,就是控件的内容,默认值是on和off,可以在XML文件中引用,填写值的时候需要把打开写在前面,关闭写在后面,并用分号(:)隔开,如(开:关),注意分号是英文的分号。
public SwitchButton(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub this.context = context; TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SwitchButton); onOffString = typedArray.getString(R.styleable.SwitchButton_onOff); if (onOffString != null && (!"".equals(onOffString))) { String[] contentStr = onOffString.split(";"); if (contentStr.length >= 2) { onString = "".equals(contentStr[0]) ? "on" : contentStr[0]; offString = "".equals(contentStr[1]) ? "off" : contentStr[1]; } else if (contentStr.length == 1) { onString = "".equals(contentStr[0]) ? "on" : contentStr[0]; offString = "off"; } else { onString = "on"; offString = "off"; } } else { onString = "on"; offString = "off"; } LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater.inflate(R.layout.view_switchbutton, this); switchLayout = (RelativeLayout) findViewById(R.id.switch_layout); switchOnBgTextView = (TextView) findViewById(R.id.switch_on_bg_textview); switchOffBgTextView = (TextView) findViewById(R.id.switch_off_bg_textview); switchOnButtonTextView = (TextView) findViewById(R.id.switch_on_button_textview); switchOffButtonTextView = (TextView) findViewById(R.id.switch_off_button_textview); switchOnButtonTextView.setTextColor(Color.WHITE); switchOnButtonTextView.setText(onString); switchOffButtonTextView.setTextColor(Color.GRAY); switchOffButtonTextView.setText(offString); switchLayout.setOnClickListener(this); setView(); }
然后就是不同的点击事件展示不同的图片
private void setView() { if (switchStatues) { switchOnBgTextView.setVisibility(View.VISIBLE); switchOnButtonTextView.setVisibility(View.VISIBLE); switchOffBgTextView.setVisibility(View.GONE); switchOffButtonTextView.setVisibility(View.GONE); } else { switchOnBgTextView.setVisibility(View.GONE); switchOnButtonTextView.setVisibility(View.GONE); switchOffBgTextView.setVisibility(View.VISIBLE); switchOffButtonTextView.setVisibility(View.VISIBLE); } } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.switch_layout: switchStatues = !switchStatues; setView(); onCheckedChangeListener.onCheckedChange(switchStatues); break; default: break; } }
最后附上switch的布局
代码中的点击事件和得到当前的状态
button.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChange(boolean isChecked) { // TODO Auto-generated method stub if (isChecked) { text.setText("开"); } else { text.setText("关"); } } });
button.getSwitchStatues()
下载地址:项目代码
0 0
- 仿IOS Switch控件
- 仿IOS Switch 开关
- Android ui 控件之仿苹果SWITCH
- android仿IOS选择(switch)开关
- ios switch 控件基本操作
- ios 仿android gallery控件
- android 仿ios开关控件
- android仿ios时间控件
- android仿ios时间控件
- 仿ios日期选择控件
- 仿IOS分段控件SegmentConrtol
- 仿IOS搜索控件SearchView
- 仿ios开关选择控件
- Android Switch开关, 高仿iOS(一)
- android 仿IOS的滚轮选择控件
- 自定义控件之仿IOS的ToggleButton
- 自定义控件之仿IOS的ToggleButton
- 自定义View:仿ios开关按钮控件
- 安卓动画技术
- 使用Objective-C获取每日必应壁纸设置为桌面
- Html5添加图片分类过滤特效插件教程
- maven仓库找不到需要的jar包解决办法
- SVN创建新文件不能提交的处理
- 仿IOS Switch控件
- 数据库设计步骤--了解用户需求:与客户会面
- 安装Hadoop,启动hadoop(每次都用),设置免密码登陆
- 全站HTTPS来了!有何优势、与HTTP有何不同?
- Js闭包的用途
- DATABASE 12C 新功能rman RECOVER TABLE
- Linux网络套接字学习笔记(一)
- iOS开发之LLDB断点调试和常用命令
- Google Protocol Buffer 的使用和原理