枚举
2016-06-01 11:02
239 查看
枚举用法:
1.okhttp
2.oschina
枚举很像c里面的结构体,小巧。可以保存信息。保存tabhost的模板信息。
枚举可以替代以前在接口里面定义的常量字符串。不用public static final了。
枚举可以switch。
1.okhttp
package okhttp3; import java.io.IOException; public enum Protocol { HTTP_1_0, HTTP_1_1, SPDY_3, HTTP_2; private final String protocol; public static Protocol get(String protocol) throws IOException { if (protocol.equals(HTTP_1_0.protocol)) return HTTP_1_0; if (protocol.equals(HTTP_1_1.protocol)) return HTTP_1_1; if (protocol.equals(HTTP_2.protocol)) return HTTP_2; if (protocol.equals(SPDY_3.protocol)) return SPDY_3; throw new IOException("Unexpected protocol: " + protocol); } public String toString() { return this.protocol; } }
2.oschina
package net.oschina.app.ui; import net.oschina.app.R; import net.oschina.app.fragment.ExploreFragment; import net.oschina.app.fragment.MyInformationFragment; import net.oschina.app.viewpagerfragment.NewsViewPagerFragment; import net.oschina.app.viewpagerfragment.TweetsViewPagerFragment; public enum MainTab { NEWS(0, R.string.main_tab_name_news, R.drawable.tab_icon_new, NewsViewPagerFragment.class), TWEET(1, R.string.main_tab_name_tweet, R.drawable.tab_icon_tweet, TweetsViewPagerFragment.class), QUICK(2, R.string.main_tab_name_quick, R.drawable.tab_icon_new, null), EXPLORE(3, R.string.main_tab_name_explore, R.drawable.tab_icon_explore, ExploreFragment.class), ME(4, R.string.main_tab_name_my, R.drawable.tab_icon_me, MyInformationFragment.class); private int idx; private int resName; private int resIcon; private Class<?> clz; private MainTab(int idx, int resName, int resIcon, Class<?> clz) { this.idx = idx; this.resName = resName; this.resIcon = resIcon; this.clz = clz; } public int getIdx() { return idx; } public void setIdx(int idx) { this.idx = idx; } public int getResName() { return resName; } public void setResName(int resName) { this.resName = resName; } public int getResIcon() { return resIcon; } public void setResIcon(int resIcon) { this.resIcon = resIcon; } public Class<?> getClz() { return clz; } public void setClz(Class<?> clz) { this.clz = clz; } }
private void initTabs() { MainTab[] tabs = MainTab.values(); final int size = tabs.length; for (int i = 0; i < size; i++) { MainTab mainTab = tabs[i]; TabSpec tab = mTabHost.newTabSpec(getString(mainTab.getResName())); View indicator = LayoutInflater.from(getApplicationContext()) .inflate(R.layout.tab_indicator, null); TextView title = (TextView) indicator.findViewById(R.id.tab_title); Drawable drawable = this.getResources().getDrawable( mainTab.getResIcon()); title.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null); if (i == 2) { indicator.setVisibility(View.INVISIBLE); mTabHost.setNoTabChangedTag(getString(mainTab.getResName())); } title.setText(getString(mainTab.getResName())); tab.setIndicator(indicator); tab.setContent(new TabContentFactory() { @Override public View createTabContent(String tag) { return new View(MainActivity.this); } }); mTabHost.addTab(tab, mainTab.getClz(), null); if (mainTab.equals(MainTab.ME)) { View cn = indicator.findViewById(R.id.tab_mes); mBvNotice = new BadgeView(MainActivity.this, cn); mBvNotice.setBadgePosition(BadgeView.POSITION_TOP_RIGHT); mBvNotice.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10); mBvNotice.setBackgroundResource(R.drawable.notification_bg); mBvNotice.setGravity(Gravity.CENTER); } mTabHost.getTabWidget().getChildAt(i).setOnTouchListener(this); } }
枚举很像c里面的结构体,小巧。可以保存信息。保存tabhost的模板信息。
枚举可以替代以前在接口里面定义的常量字符串。不用public static final了。
枚举可以switch。
相关文章推荐
- 如何规划自己的程序员人生
- 简析 JSON 中的{}与[]
- Android获取屏幕宽度的4种方法
- 9. MyEclipse中的SVN操作手册
- android music 源代码汇总
- Centos6.5搭建java开发环境
- Linux上使用openssl实现模拟CA
- 利用ndk-gdb调试时,检测到的app_abi为armeabi
- C# 设计模式系列教程-组合模式
- Spark学习——Job的提交和运行
- Static全局变量与普通的全局变量有什么区别?static函数与普通函数有什么区别?
- HTML标准事件(包含HTML5)
- maven ant tomcat配置(待续)
- Android开发之Intent
- SpringMVC-RequestMapping注解
- SQLiteDatabase中query、insert、update、delete方法参数说明
- 错误Error running app: Instant Run requires 'Tools | Android | Enable ADB integration' to be enabled.
- 编写shell命令实现IOS的自动打包
- ios 小数点后两位
- 【问题处理】GIT删除主分支报错:[remote rejected] master (deletion of the current branch prohibited)