【Android】一些会用到的简单但使用的小方法总结

来源:互联网 发布:数据的本质 电子书 编辑:程序博客网 时间:2024/06/09 19:10
package com.cjl.utils;  import java.util.List;  import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.ResolveInfo; import android.content.res.Configuration; import android.text.TextPaint; import android.util.DisplayMetrics; import android.util.Log; import android.view.WindowManager; import android.widget.PopupWindow;  public class Utils { public void utils(){ //-------------文字根据状态更改颜色 android:textColor,放在res/color/目录下-------------     /*      *       * <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>         */  //------------- 背景色根据状态更改颜色 android:backgroup, 如果直接给背景色color会报错。------------- /*     <selector xmlns:android="http://schemas.android.com/apk/res/android">      <item android:state_selected="true">     <shape>     <gradient android:angle="0" android:centerColor="#00a59f" android:endColor="#00a59f" android:startColor="#00a59f" />             </shape>             </item>             <item android:state_focused="true"><shape>                 <gradient android:angle="0" android:centerColor="#00a59f" android:endColor="#00a59f" android:startColor="#00a59f" />             </shape></item>         <item android:state_pressed="true"><shape>                 <gradient android:angle="0" android:centerColor="#00a59f" android:endColor="#00a59f" android:startColor="#00a59f" />             </shape></item>         <item><shape>                 <gradient android:angle="0" android:centerColor="#00ff00" android:endColor="00ff00" android:startColor="00ff00" />             </shape></item>              </selector>*/   // -------------【Andorid X 项目笔记】禁用ListView的Fling功能-------------------------- /*http://www.cnblogs.com/over140/archive/2012/09/18/2692377.html 手势识别类     private class TouchGesture extends SimpleOnGestureListener {          *//** 快速滚动 *//*         @Override         public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {             return true;         }      }      private OnTouchListener mOnListViewTouchListener = new OnTouchListener() {          @Override         public boolean onTouch(View v, MotionEvent event) {             if (mTouchGesture.onTouchEvent(event))                 return true;             return false;         }     };     代码说明:直接调用ListView的setOnTouchListener绑定mOnListViewTouchListener即可。     关键点还是在onFling方法返回true,意思是消耗掉Fling事件,不再继续往下传事件链。     */   // -------------Android控制ScrollView滑动速度-------------------------- /*  * http://www.cnblogs.com/over140/archive/2011/10/06/2199891.html      * 快/慢滑动ScrollView      *     public class SlowScrollView extends ScrollView {              public SlowScrollView(Context context, AttributeSet attrs, int defStyle) {             super(context, attrs, defStyle);         }              public SlowScrollView(Context context, AttributeSet attrs) {             super(context, attrs);         }              public SlowScrollView(Context context) {             super(context);         }              /**          * 滑动事件         @Override         public void fling(int velocityY) {             super.fling(velocityY / 4);         }     }     */  // ----------------------[Android疑难杂症]动态改变Background后Padding无效的问题----------- /*int bottom = theView.getPaddingBottom();     int top = theView.getPaddingTop();     int right = theView.getPaddingRight();     int left = theView.getPaddingLeft();     theView.setBackgroundResource(R.drawable.entry_bg_with_image);     theView.setPadding(left, top, right, bottom);*/     }          // 1、精确获取屏幕尺寸(例如:3.5、4.0、5.0寸屏幕),一般是7寸以上是平板     public static double getScreenPhysicalSize(Activity ctx) {         DisplayMetrics dm = new DisplayMetrics();         ctx.getWindowManager().getDefaultDisplay().getMetrics(dm);         double diagonalPixels = Math.sqrt(Math.pow(dm.widthPixels, 2) + Math.pow(dm.heightPixels, 2));         return diagonalPixels / (160 * dm.density);     }          // 2、判断是否是平板(官方用法)     public static boolean isTablet(Context context) {         return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;     }          // 5、启动APK的默认Activity     public static void startApkActivity(final Context ctx, String packageName) {         PackageManager pm = ctx.getPackageManager();         PackageInfo pi;         try {             pi = pm.getPackageInfo(packageName, 0);             Intent intent = new Intent(Intent.ACTION_MAIN, null);             intent.addCategory(Intent.CATEGORY_LAUNCHER);             intent.setPackage(pi.packageName);              List<ResolveInfo> apps = pm.queryIntentActivities(intent, 0);              ResolveInfo ri = apps.iterator().next();             if (ri != null) {                 String className = ri.activityInfo.name;                 intent.setComponent(new ComponentName(packageName, className));                 ctx.startActivity(intent);             }         } catch (NameNotFoundException e) {             Log.e("startActivity", e.toString());         }     }          // 7、计算字宽,注意如果设置了textStyle,还需要进一步设置TextPaint。      public static float GetTextWidth(String text, float Size) {         TextPaint FontPaint = new TextPaint();         FontPaint.setTextSize(Size);         return FontPaint.measureText(text);     }          // 设置键盘弹出时PopupWindow的表现方式为重新规划PopupWindow的位置给键盘腾出地     public static void setPopupWindowAndInputMode(PopupWindow popup){     // 设置弹出窗体需要软键盘,     popup.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);     // 再设置模式,和Activity的一样,覆盖,调整大小。     popup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);     } } 


// 1. 关于EditText的焦点问题(默认进入不弹出键盘)http://blog.csdn.net/ldci3gandroid/article/details/8477937

 

原创粉丝点击