【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
- 【Android】一些会用到的简单但使用的小方法总结
- Android SHELL or MAKEFILE 一些会用到的小方法 (LTS)
- 一些编程会用到的小技巧
- css 一些会用到的小技巧
- 总结:Js中可能用到的一些小方法
- iOS 开发中会用到的一些小技巧
- 初学python一些会用到的零件1【小甲鱼】
- 微信开发经常会用到的一些方法,做一次总结,以后直接过来拿
- 总结一些你肯定会用到的东西(长期更新)
- servlet里经常会用到的一些GET方法
- 一些经常会用到的原生JS方法
- 一些 新函数 但 很少用到的
- 总结的一些简单实用的小方法
- java中一些经常用到的小方法
- ACM会用到的一些数学知识
- GDB 会用到的一些操作
- 会用到的一些按钮样式
- 一些平时会用到的资料
- JQuery UI的拖拽功能
- 多文档记事本开发随记
- 动态规划求解01背包问题初探(C版)
- CentOS 下安装单机版zookeeper
- Linux下批量更改文件名
- 【Android】一些会用到的简单但使用的小方法总结
- 软件开发项目管理的十大精华指南
- 【iOS-Cocos2d游戏开发之十九】游戏数据存储的四种常用方式NSKeyedArchiver/NSUserDefaults/Write写入/SQLite3
- 并口、串口、COM口区别
- lucene开发包下载推荐
- VS中bin,app_code,app_data,app_browser,app_GlobalResources等文件夹的作用
- 项目管理知识点聚合
- 《Lua程序设计[第二版]》第11,12章笔记
- [iOS]AES加密在iOS上面的实现