61.得到当前应用的版本号,版本名字
private int getVersionCode() throws Exception {
// 获取packagemanager的实例
PackageManager packageManager = getPackageManager();
// getPackageName()是你当前类的包名,0代表是获取版本信息
PackageInfo packInfo = packageManager.getPackageInfo(getPackageName(),
0);
return packInfo.versionCode;
}
62.截取手机屏幕,包括状态栏(状态栏黑的)
/**
* 截取整个屏幕
*/
public Bitmap getAllBitmap() {
// 1.构建Bitmap
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
int w = display.getWidth();
int h = display.getHeight();
Bitmap Bmp = Bitmap.createBitmap(w, h, Config.ARGB_8888);
// 2.获取屏幕
View decorview = getWindow().getDecorView();
decorview.setDrawingCacheEnabled(true);
Bmp = decorview.getDrawingCache();
return Bmp;
}
63.像素px与dp之间的转换
/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* 根据手机的分辨率从 px(像素) 的单位 转成为 dp
*/
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
/**
* 将px值转换为sp值,保证文字大小不变
*
* @param pxValue
* @param fontScale
* (DisplayMetrics类中属性scaledDensity)
* @return
*/
public static int px2sp(Context context, float pxValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (pxValue / fontScale + 0.5f);
}
/**
* 将sp值转换为px值,保证文字大小不变
*
* @param spValue
* @param fontScale
* (DisplayMetrics类中属性scaledDensity)
* @return
*/
public static int sp2px(Context context, float spValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (spValue * fontScale + 0.5f);
}
64.回到桌面
/**
* 回到桌面
*/
private void goHome() {
Intent i = new Intent(Intent.ACTION_MAIN);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addCategory(Intent.CATEGORY_HOME);
startActivity(i);
}
65.验证手机号码是否正确
/**
* 验证手机号码是否正确
*
* @param phoneNumber
* @return
*/
public static boolean phoneNumberIfRight(String phoneNumber) {
if (phoneNumber != null) {
if (phoneNumber.length() == 11) {
for (int i = 0; i < phoneNumber.length(); i++) {
char c = phoneNumber.charAt(i);
if (!(c >= '0' && c <= '9')) {
return false;
}
}
return true;
}
}
return false;
}
66.WebView Settins常用方法
setJavaScriptEnabled(true) ;//支持js脚本
setPluginsEnabled(true) ;//支持插件
setUserWideViewPort(false) ;//将图片调整到适合webview的大小
setSupportZoom(true) ;//支持缩放
setLayoutAlgorithm(LayoutAlgrithm.SINGLE_COLUMN) ;//支持内容从新布局
supportMultipleWindows() ;//多窗口
setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK) ;//关闭webview中缓存
setAllowFileAccess(true) ;//设置可以访问文件
setNeedInitialFocus(true) ;//当webview调用requestFocus时为webview设置节点
setjavaScriptCanOpenWindowsAutomatically(true) ;//支持通过JS打开新窗口
setLoadsImagesAutomatically(true) ;//支持自动加载图片
setBuiltInZoomControls(true);
//支持缩放
webView.setInitialScale(35);
//设置缩放比例
webView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
//设置滚动条隐藏
webView.getSettings().setGeolocationEnabled(true);
//启用地理定位
webView.getSettings().setRenderPriority(RenderPriority.HIGH);
//设置渲染优先级
String dir = "/sdcard/temp";//设置定位的数据库路径
webView.getSettings().setGeolocationDatabasePath(dir);
67,TextView获得跑马灯效果
重写TextView的isFocused方法,返回true就ok
android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever"
68.锁定屏幕的横竖屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);// 横屏
//setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) 竖屏
69.设置屏幕长亮
常亮
view.setKeepScreenOn(true)
70.设置滚动条的样式
android:scrollbarTrackVertical="@drawable/aaa"
android:scrollbarThumbVertical="@drawable/bbb"
android:scrollbarTrackVertical设置滚动条短的style
android:scrollbarThumbVertical设置滚动条长的style
aaa和bbb放图片和xml都可以,可以在xml里结合shap设置样式
<style name="scroll">
<item name="android:scrollbarThumbVertical">@drawable/seek_bar_thumb</item>
<item name="android:scrollbarTrackVertical">@drawable/scroll</item>
<item name="android:scrollbarThumbHorizontal">@drawable/scrollbar_handle</item>
<item name="android:scrollbarTrackHorizontal">@drawable/scrollbar_track</item>
<item name="android:scrollbarFadeDuration">0</item>
<item name="android:scrollbarAlwaysDrawVerticalTrack">true</item>
</style>
71.获得状态栏高度
来自: http://www.cnblogs.com/LuoYer/archive/2011/11/06/2238167.html
闲暇写了个单本小说阅读的应用。中间碰到了需要获取状态栏高度的问题。
就像android后期版本,无法直接退出一样。找了一些方法来获取状态栏高度,结果都是为0.
还好,牛人是很多的,当时,找到一段代码,能够有效的获取状态栏的高度。特此记录,备忘,以及供大家参考。
Class<?> c = null;
Object obj = null;
Field field = null;
int x = 0, sbar = 0;
try {
c = Class.forName("com.android.internal.R$dimen");
obj = c.newInstance();
field = c.getField("status_bar_height");
x = Integer.parseInt(field.get(obj).toString());
sbar = getResources().getDimensionPixelSize(x);
} catch(Exception e1) {
loge("get status bar height fail");
e1.printStackTrace();
}
个人注:以下代码不能在onCreate里面使用,否则获取状态栏高度为0
Rect frame = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
72.AndroidMainFest.xml中Activity设置
- android:screenOrientation="landscape"是限制此页面横屏显示,
- android:screenOrientation="portrait"是限制此页面数竖屏显示。
如果没有设置,则横竖屏都可以,这个时候的Activity会被重新执行,需要添加这一行代码阻止执行
AndroidManifest.xml中设置android:configChanges="orientation|screenSize“
键盘输入问题,当我们不需要输入法的时候,希望它可以自动隐藏
android:windowSoftInputMode="stateHidden"
android:windowSoftInputMode="stateHidden|adjustPan"可以把输入框推到上面
73.得到控件的绝对位置
控件的绝对位置必须在控件绘制完毕后获得,可以使用ViewTreeHolder来监听
然后时候方法mOptions2.getGlobalVisibleRect(rect);来获得控件在屏幕了的绝对位置
74,根据控件大小动态改变字体大小
/**
* 方法名: changedSize
*
* 功能描述:动态改变字体大小
*
* @return void
*
* </br>throws
*/
private void changedSize(TextView textView) {
if (textView.getText().toString().length() >= 7) {// 如果字数大于7,则设置为单行
textView.setSingleLine();
return;
}
int textPx = PxDpSp.sp2px(MainActivity.this, textView.getTextSize());// 得到字体的大小
if (textPx > textView.getWidth()) {// 判断字体的大小是否超过控件的大小
textView.setTextSize(PxDpSp.px2sp(MainActivity.this,
textView.getWidth() - 10));// 如果超过,则根据控件的大小设置字体的大小
}
}
75.EditText把光标定位到末尾
mEditName.setSelection(mEditName.getEditableText().toString().length());// 将光标移至文字末尾
76.设置TextView的drawabletoleft
Drawable d = getResources().getDrawable(R.drawable.q_shaixuan2);// 找图片
d.setBounds(0, 0, d.getMinimumWidth(), d.getMinimumHeight());
pb_screening.setCompoundDrawables(d, null, null, null);
77.取消正在播放的动画
view.clearAnimation();
78.// 判断是否是应用界面
public static boolean isApplicationFirst(final Context context){
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).topActivity;
if (topActivity.getPackageName().equals(context.getPackageName())) {
return true;
}
}
return false;
}
79.//判断应用是否切换到了后台
/**
* 程序是否在前台运行
*
* @return
*/
public boolean isAppOnForeground() {
// Returns a list of application processes that are running on the
// device
ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
String packageName = getApplicationContext().getPackageName();
List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
if (appProcesses == null)
return false;
for (RunningAppProcessInfo appProcess : appProcesses) {
// The name of the process that this object is associated with.
if (appProcess.processName.equals(packageName) && appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
return true;
}
}
return false;
}
80.判断数据库是否存在某个表
79.//判断应用是否切换到了后台
[java] view plaincopy
/**
* 程序是否在前台运行
*
* @return
*/
public boolean isAppOnForeground() {
// Returns a list of application processes that are running on the
// device
ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
String packageName = getApplicationContext().getPackageName();
List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
if (appProcesses == null)
return false;
for (RunningAppProcessInfo appProcess : appProcesses) {
// The name of the process that this object is associated with.
if (appProcess.processName.equals(packageName) && appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
return true;
}
}
return false;
}