Android_CodeWiki_01

11352 ワード

必要に応じて、共通のチップを記録します.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