1.打开系统图库
Intent it = new Intent(Intent.ACTION_PICK, null);
it.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(it, 100);
2.设置指定字体
mWord = (EditText) findViewById(R.id.word);
//替换组件默认的字体
mWord.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/test.ttf"));
3.截屏
private Bitmap screenView(){
View view = getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
return view.getDrawingCache();
}
4.ZXing精简版本
compile 'com.google.zxing:core:3.3.0'
5.Android**根据包名打开应用**
public void startAPP(String appPackageName){
try{
Intent intent = this.getPackageManager().getLaunchIntentForPackage(appPackageName);
startActivity(intent);
}catch(Exception e){
Toast.makeText(this, "没有安装", Toast.LENGTH_LONG).show();
}
}
6.Android根据包名判断是否安装某个应用
public static boolean checkApkExist(Context context, String packageName) {
if (packageName == null || "".equals(packageName))
return false;
try {
ApplicationInfo info = context.getPackageManager()
.getApplicationInfo(packageName,
PackageManager.GET_UNINSTALLED_PACKAGES);
return true;
} catch (NameNotFoundException e) {
return false;
}
}
7.Android检查是否有网络访问
public static boolean isNetworkAvaliable(Context context) {
// 获取所有连接管理对象
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (null != cm) {
// 获取网络连接管理的对象
NetworkInfo info = cm.getActiveNetworkInfo();
if (null != info && info.isConnected()) {
// 判断当前网络是否已经连接
if (NetworkInfo.State.CONNECTED == info.getState()) {
return true;
}
}
}
return false;
}
8.Android获取设置下面帐户信息
Account[] accounts = AccountManager.get(this).getAccounts();
for(Account account : accounts){
LogUtil.e("tag", account.name+","+account.type+","+account.describeContents());
}
记得添加权限:
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
9.监听ListView滑动到底部
listview.setOnScrollListener(new OnScrollListener(){
@Override
public void onScrollStateChanged(AbsListView view, int scrollState){
// 当不滚动时
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
// 判断是否滚动到底部
if (view.getLastVisiblePosition() == view.getCount() - 1) {
//加载更多功能的代码
}
}
}
});
10.android设置字体颜色
tv.setTextColor(this.getResources().getColor(R.color.textColor_black));
11.Drawable与Bitmap的相互转换
//Drawable--->Bitmap
public static Bitmap drawableToBitmap(Drawable drawable) {
return ((BitmapDrawable)drawable).getBitmap();
}
//Bitmap--->Drawable
public static Drawable bitmapToDrawable(Bitmap bitmap){
return new BitmapDrawable(bitmap);
}
12.设置ListView数据靠下方显示
android:stackFromBottom="true"
13.Android TextView(EditView)文字底部或者中间加横线
//中间加横线
tv.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG );
//底部加横线:
tv .getPaint().setFlags(Paint. UNDERLINE_TEXT_FLAG );
14取消GridView单击时的黄色背景
mGridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
15.解决Andriod**软键盘**出现把原来的布局给顶上去的方法
<activity
android:name=".activity.HomeActivity"
android:windowSoftInputMode="adjustPan|stateHidden" />
16.Android**回到桌面**
Intent i = new Intent(Intent.ACTION_MAIN);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addCategory(Intent.CATEGORY_HOME);
startActivity(i);
17.让scrollView滚动到顶部
在scrollView内部最上面定义一个控件获得焦点,滚动条自然就到顶部去了,如下:
txtBaseMsg.setFocusable(true);
txtBaseMsg.setFocusableInTouchMode(true);
txtBaseMsg.requestFocus();
18.获取手机拍摄照片的时间
String img_date = Utils.DATE_TIME_FORMATER.format(new Date());
try {
ExifInterface exif = new ExifInterface(mDirPath);
String str = exif.getAttribute(ExifInterface.TAG_DATETIME);
String temp[] = str.split(" ");
temp[0] = temp[0].replace(":","-");
img_date = temp[0] + " " + temp[1];
LogUtil.e("img_date:"+img_date);
} catch (Exception ee) {
ee.printStackTrace();
}
19.代码设置TextView的drawable以及padding
Drawable drawable = ContextCompat.getDrawable(getActivity(), R.mipmap.gps_btn);
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
tabLeftTv.setCompoundDrawablePadding(5);
tabLeftTv.setCompoundDrawables(drawable, null, null, null);
20.滚动条自动滚动并在末尾追加内容:
orderDetailEt.setMovementMethod(ScrollingMovementMethod.getInstance());
orderDetailEt.setSelection(orderDetailEt.getText().length(), orderDetailEt.getText().length());
orderDetailEt.getText().append(detailSb.toString());
21.scrollview自动滑到底部:
private Handler scroll_handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
myscrollview.fullScroll(ScrollView.FOCUS_DOWN);
break;
}
super.handleMessage(msg);
}
};
Message message = new Message();
message.what = 1;
scroll_handler.sendMessage(message);