记录常用代码片,以备不时之需..wkakak,开始:

   1、 精确获取屏幕尺寸(例如:3.5、4.0、5.0寸屏幕) 

  

1  public static double getScreenPhysicalSize(Activity ctx) {
2         DisplayMetrics dm = new DisplayMetrics();
3         ctx.getWindowManager().getDefaultDisplay().getMetrics(dm);
4         double diagonalPixels = Math.sqrt(Math.pow(dm.widthPixels, 2) + Math.pow(dm.heightPixels, 2));
5         return diagonalPixels / (160 * dm.density);
6     }

 一般小于7寸的,都是非平板,属于正常智能机系列

  2、 判断是否是平板(官方用法)

  

public static boolean isTablet(Context context) {return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_L         ARGE;}

3、 文字根据状态更改颜色 android:textColor 

  

<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:color="#53c1bd" android:state_selected="true"/><item android:color="#53c1bd" android:state_focused="true"/><item android:color="#53c1bd" android:state_pressed="true"/><item android:color="#777777"/>
</selector>

放在工程 res/color/目录下

4、背景色根据状态更改颜色 android:backgroup

     

 1 <selector xmlns:android="http://schemas.android.com/apk/res/android">
 2
 3     <item android:state_selected="true"><shape>
 4             <gradient android:angle="0" android:centerColor="#00a59f" android:endColor="#00a59f" android:startColor="#00a59f" />
 5         </shape></item>
 6     <item android:state_focused="true"><shape>
 7             <gradient android:angle="0" android:centerColor="#00a59f" android:endColor="#00a59f" android:startColor="#00a59f" />
 8         </shape></item>
 9     <item android:state_pressed="true"><shape>
10             <gradient android:angle="0" android:centerColor="#00a59f" android:endColor="#00a59f" android:startColor="#00a59f" />
11         </shape></item>
12     <item><shape>
13             <gradient android:angle="0" android:centerColor="#00ff00" android:endColor="00ff00" android:startColor="00ff00" />
14         </shape></item>
15
16 </selector>

  如果直接给背景色color会报错。

5、 启动APK的默认Activity

  

 1 public static void startApkActivity(final Context ctx, String packageName) {
 2         PackageManager pm = ctx.getPackageManager();
 3         PackageInfo pi;
 4         try {
 5             pi = pm.getPackageInfo(packageName, 0);
 6             Intent intent = new Intent(Intent.ACTION_MAIN, null);
 7             intent.addCategory(Intent.CATEGORY_LAUNCHER);
 8             intent.setPackage(pi.packageName);
 9
10             List<ResolveInfo> apps = pm.queryIntentActivities(intent, 0);
11
12             ResolveInfo ri = apps.iterator().next();
13             if (ri != null) {
14                 String className = ri.activityInfo.name;
15                 intent.setComponent(new ComponentName(packageName, className));
16                 ctx.startActivity(intent);
17             }
18         } catch (NameNotFoundException e) {
19             Log.e("startActivity", e);
20         }
21     }

7、计算字宽

  

1  public static float GetTextWidth(String text, float Size) {
2         TextPaint FontPaint = new TextPaint();
3         FontPaint.setTextSize(Size);
4         return FontPaint.measureText(text);
5     }

8、获取应用程序下所有Activity 

  

1  public static ArrayList<String> getActivities(Context ctx) {
2       ArrayList<String> result = new ArrayList<String>();
3       Intent intent = new Intent(Intent.ACTION_MAIN, null);
4       intent.setPackage(ctx.getPackageName());
5       for (ResolveInfo info : ctx.getPackageManager().queryIntentActivities(intent, 0)) {
6           result.add(info.activityInfo.name);
7       }
8       return result;
9   }

9、检测字符串中是否包含汉字

  

public static boolean checkChinese(String sequence) {final String format = "[\\u4E00-\\u9FA5\\uF900-\\uFA2D]";boolean result = false;Pattern pattern = Pattern.compile(format);Matcher matcher = pattern.matcher(sequence);result = matcher.find();return result;}

检测字符串中只能包含:中文、数字、下划线(_)、横线(-)

 public static boolean checkNickname(String sequence) {        final String format = "[^\\u4E00-\\u9FA5\\uF900-\\uFA2D\\w-_]";        Pattern pattern = Pattern.compile(format);        Matcher matcher = pattern.matcher(sequence);        return !matcher.find();    }

10、检查有没有应用程序来接受处理你发出的intent

  

1  public static boolean isIntentAvailable(Context context, String action) {
2         final PackageManager packageManager = context.getPackageManager();
3         final Intent intent = new Intent(action);
4         List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
5         return list.size() > 0;
6     }

参考:http://www.cnblogs.com/over140/archive/2013/03/05/2706068.html

Android_CodeWiki_01相关推荐

最新文章

  1. 云服务器重装系统后卡顿,低延迟云服务器发生卡顿丢包检测方法
  2. jQuery 事件
  3. Swift学习Day01(Object_c 与Swift的相互调用 )
  4. 数据结构之图:图的搜索,Python代码实现——23
  5. c语言边序列构造邻接表,结构C语言版期末考试考试(有答案).doc
  6. mysql简单部署_安装部署Mysql实例(最简单快速噢)
  7. Spring 异常处理三种方式
  8. python 多线程和协程结合_如何让 python 处理速度翻倍?内含代码
  9. 机器学习速成课程 | 练习 | Google Development——编程练习:稀疏性和 L1 正则化
  10. maven配置sqlServer的依赖
  11. request 和require区别_JAVA WEB开发中涉及到的get和post请求,他们的区别
  12. PWM级联方案。UART接口的单总线控制多个PWM输出。数字舵机,舵机级联方案
  13. SQL Server-聚焦使用索引和查询执行计划(五)
  14. 这些年,这些ACM大佬-施韩原访谈
  15. 服务器fs改变文件内容,SeaweedFS文件系统
  16. 华为最强科普:什么是DSP?
  17. 大数据之足球盘口赔率凯利必发数据采集爬虫
  18. Handler之消息屏障你应该知道的
  19. adb+python实现自动领喵币
  20. Emby识别都是英文海报

热门文章

  1. ROS中使用摄像头的问题
  2. liunx查看python的site-packages路径
  3. php base64解码图片 base64加密图片还原
  4. php字符串比较函数
  5. 不畏浮云遮望眼--离散数学和组合数学
  6. 一键安装python3环境
  7. 微信跳一跳高分辅助踩坑
  8. Tesseract-OCR 3.04在Windows7 vs2013上编译过程
  9. Windows7 64bit VS2013 Caffe test MNIST操作步骤
  10. 送给那些还在迷茫的人