public static FileViewFragment getInstance(A a){
FileViewFragment fileViewFragment = new FileViewFragment();
fileViewFragment.a = a;
return fileViewFragment;
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
onBackPressed();
return true;
} else {
return super.onKeyDown(keyCode, event);
}
}
int indentify= getResources().getIdentifier
(“icon”, “drawable”, “org.anddev.android.testproject”);
第一个参数为ID名,第二个为资源属性是ID或者是Drawable,第三个为包名。
project–>properties->Run Setting->
选择错误类->删除Bootstrap Entries
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
exit();
return true;
} else {
return super.onKeyDown(keyCode, event);
}
}
//两秒后isExit=flase;
private Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 0:
isExit = false;
}
}
};
public void exit() {
if (!isExit) {
isExit = true;
Toast.makeText(getApplicationContext(),
R.string.msg_exit_next,Toast.LENGTH_SHORT).show();
mHandler.sendEmptyMessageDelayed(0, 2000);
} else {
//完全退出程序
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
// System.exit(0);
finish();
}
}
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0==arg1;}
false无动画,true有动画
viewPager.setCurrentItem(index,false);
ImageView view1=new ImageView(ctx);
WindowManager windowManager = (WindowManager)
getSystemService(Context.WINDOW_SERVICE);
int xPos = windowManager.getDefaultDisplay().getWidth() / 2
- hisRecordPopWin.getWidth() / 2;
//确定布局
View contentView=LayoutInflater.from(DoctorListActivity.this).
inflate(R.layout.consult_depart_list,null);
//定义布局的长宽
mPopWindow=new PopupWindow(contentView,
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
lv_data_depart=
(ListView)contentView.findViewById(R.id.lv_data_depart);
lv_data_depart.setAdapter(mAdapter_depart);
//popWindow显示位置
mPopWindow.showAsDropDown(v);
//使PopupWindow能响应按键事件或点击背景Activity以关闭弹出对话框
mPopWindow.setBackgroundDrawable(new BitmapDrawable());
doInBackground(Params…)
将在onPreExecute 方法执行后马上执行,该方法运行在后台线程中。
这里将主要负责执行那些很耗时的后台处理工作。可以调用publishProgress方法来更新实时的任务进度。该方法是抽象方法,子类必须实现。
onProgressUpdate(Progress…)
在publishProgress方法被调用后,UI 线程将调用这个方法从而在界面上展示任务的进展情况,例如通过一个进度条进行展示。
在doInBackground 执行完成后,onPostExecute 方法将被UI 线程调用,后台的计算结果将通过该方法传递到UI 线程,并且在界面上展示给用户.
sv_scroll.fullScroll(ScrollView.FOCUS_UP);
tv_quick_question_content.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (v.getId()) {
case R.id.tv_quick_question_content:
v.getParent().requestDisallowInterceptTouchEvent(true);
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_UP:
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
}
return false;
}
});
android: scrollbars=”none”
event.getAction() & MotionEvent.ACTION_MASK
holder.iv_dash.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
//只接受整数输入
KeyListener l = new DigitsKeyListener(fasle,false);
//接受有符号整数输入
KeyListener l = new DigitsKeyListener(true,false);
//接受小数,整数输入
KeyListener l = new DigitsKeyListener(false,true);
//接受有符号整数/小数输入
KeyListener l = new DigitsKeyListener(true,true);
et_idNo.setKeyListener(new NumberKeyListener() {
@Override
public int getInputType() {
return InputType.TYPE_CLASS_TEXT;
}
/*
1.InputType.TYPE_CLASS_TEXT
2.InputType.TYPE_CLASS_NUMBER
3.InputType.TYPE_CLASS_PHONE
4.InputType.TYPE_CLASS_DATETIME
*/
//控制输入的字符getAcceptedChars
@Override
protected char[] getAcceptedChars() {
char[] numberChars = { '1', '2', '3', '4', '5', '6', '7', '8',
'9', '0', 'x', 'X' };
return numberChars;
}
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(true);
SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintEnabled(true);
//通知栏所需颜色
tintManager.setStatusBarTintResource(R.color.top_bar);
}
}
@TargetApi(19)
private void setTranslucentStatus(boolean on) {
Window win = getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
if (on) {
winParams.flags |= bits;
} else {
winParams.flags &= ~bits;
}
win.setAttributes(winParams);
}
布局文件设定
android:clipToPadding=”true”
android:fitsSystemWindows=”true”
判断sd是否存在并挂载正确
if (android.os.Environment.getExternalStorageState()
.equals(android.os.Environment.MEDIA_MOUNTED))
return true;
获取sd卡路径(/mnt/sdcard)
Environment.getExternalStorageDirectory().toString()
Environment.getDownloadCacheDirectory().getPath()
新建文件夹
File file = new File(AppConfig.DB_PATH);
if (!file.exists()) {
file.mkdir();
}
新建文件
//指定文件名和文件路径
mCurrentPhotoFile = new File(mPhoto_camera_path,mPhoto_name_temp);
if (mCurrentPhotoFile.exists()) {
mCurrentPhotoFile.delete();
}
//设置图片网页自适应
setJavaScriptEnabled(true); //支持js
setPluginsEnabled(true); //支持插件
setUseWideViewPort(false); //将图片调整到适合webview的大小
setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN); //支持内容重新布局
setSupportZoom(true); //支持缩放
setLoadWithOverviewMode(true); // 缩放至屏幕的大小
setAllowFileAccess(true); //设置可以访问文件
//不常用
setBuiltInZoomControls(true); //设置支持缩放
setNeedInitialFocus(true); //当webview调用requestFocus时为webview设置节点
setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口
setLoadsImagesAutomatically(true); //支持自动加载图片
setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //关闭webview中缓存
supportMultipleWindows(); //多窗口
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
int expandSpec = MeasureSpec.makeMeasureSpec
(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
/*
MeasureSpec表示属性的大小和模式
前两位表示模式,后30位表示大小
00<-->未指定模式<-->MeasureSpec.UNSPECIFIED
01<-->精确模式<-->MeasureSpec.EXACTLY
10<-->最大模式<-->MeasureSpec.AT_MOST
*/
super.onMeasure(widthMeasureSpec, expandSpec);
}
lv_data_1.setFocusable(false);
lv_data_2.setFocusable(false);
//直接拨打
Intent intentPhone = new Intent(Intent.ACTION_CALL,
Uri.parse("tel:" + phoneNumber));
//跳到拨号界面
Intent intent =
newIntent(Intent.ACTION_DIAL,Uri.parse("tel:" + phoneNumber));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri myBlogUri = Uri.parse("http://www.baidu.com");
Intent intent = new Intent(Intent.ACTION_VIEW, myBlogUri);
Uri mapUri = Uri.parse("geo:38.899533,-77.036476");
Intent intent = new Intent(Intent.ACTION_VIEW, mapUri);
Uri uninstallUri = Uri.fromParts("package", "packagename", null);
returnIt = new Intent(Intent.ACTION_DELETE, uninstallUri);
Uri installUri = Uri.fromParts("package", "packagename", null);
returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);
Intent intentPick = new Intent(Intent.ACTION_PICK,null);
//这个Uri代表要查询的数据库名称加上表的名称
intentPick.setDataAndType(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*");
startActivityForResult(intentPick,REQUEST_CODE_IMAGE);
/*其他类型
music--audio/*
movie--video/*
pdf--application/*
*/
1.跳转前activity的name
callbackActivity=this.getClass().getName();
2.跳转时传递
i.putExtra(“target”, callbackActivity);
3.获取跳转前activity
targetAct = Class.forName(target);
//获取包名
String packageName=this.getPackageName();
PackageInfo pi=context.getPackageManager().getPackageInfo(packageName, 0);
//获取版本名
String versionName = pi.versionName;
//获取版本号
String versionCode = pi.versionCode;
/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*/
public int dip2px(Context context, float dpValue) {
final float scale =
context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* 根据手机的分辨率从 px(像素) 的单位 转成为 dp
*/
public int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
String html="字体一;
SpannableString spanString = new SpannableString("welcome to blog");
ForegroundColorSpan span = new ForegroundColorSpan(Color.BLUE);
//setSpan (Object what, int start, int end, int flags)
spanString.setSpan(span, 1, 3, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
/*flag取值如下
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE 前后都不包括
即在指定范围的前面和后面插入新字符都不会应用新样式
Spannable.SPAN_EXCLUSIVE_INCLUSIVE 前面不包括,后面包括
Spannable.SPAN_INCLUSIVE_EXCLUSIVE 前面包括,后面不包括
Spannable.SPAN_INCLUSIVE_INCLUSIVE 前后都包括
*/
//常用span
msp = new SpannableString("字体测试字体大小一半两倍前景色背景
色正常粗体斜体粗斜体下划线删除线x1x2电话邮件网站短信彩信地图X轴综合/bot");
//设置字体(default,default-bold,monospace,serif,sans-serif)
msp.setSpan(new TypefaceSpan("monospace"),
0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
msp.setSpan(new TypefaceSpan("serif"),
2, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//设置字体大小(绝对值,单位:像素)
msp.setSpan(new AbsoluteSizeSpan(20),
4, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//第二个参数boolean dip
//如果为true,表示前面的字体大小单位为dip,否则为像素,同上。
msp.setSpan(new AbsoluteSizeSpan(20,true),
6, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//设置字体大小(相对值,单位:像素) 参数表示为默认字体大小的多少倍
msp.setSpan(new RelativeSizeSpan(0.5f),
8, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
msp.setSpan(new RelativeSizeSpan(2.0f),
10, 12, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//设置字体前景色
msp.setSpan(new ForegroundColorSpan(Color.MAGENTA),
12, 15, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//设置字体背景色
msp.setSpan(new BackgroundColorSpan(Color.CYAN),
15, 18, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//设置字体样式正常,粗体,斜体,粗斜体
msp.setSpan(new StyleSpan(android.graphics.Typeface.NORMAL),
18, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //正常
msp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD),
20, 22, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //粗体
msp.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC),
22, 24, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //斜体
msp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC),
24, 27, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //粗斜体
//设置下划线
msp.setSpan(new UnderlineSpan(),
27, 30, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//设置删除线
msp.setSpan(new StrikethroughSpan(),
30, 33, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//设置上下标
msp.setSpan(new SubscriptSpan(),
34, 35, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //下标
msp.setSpan(new SuperscriptSpan(),
36, 37, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //上标
//超级链接(需要添加setMovementMethod方法附加响应)
//setMovementMethod(LinkMovementMethod.getInstance());
//电话
msp.setSpan(new URLSpan("tel:4155551212"),
37, 39, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//邮件
msp.setSpan(new URLSpan("mailto:[email protected]"),
39, 41, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//网络
msp.setSpan(new URLSpan("http://www.baidu.com"),
41, 43, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//短信
msp.setSpan(new URLSpan("sms:4155551212"),
43, 45, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 使用sms:或者smsto:
//彩信
msp.setSpan(new URLSpan("mms:4155551212"),
45, 47, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 使用mms:或者mmsto:
//地图
msp.setSpan(new URLSpan("geo:38.899533,-77.036476"),
47, 49, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//图片
Drawable drawable = getResources().getDrawable(R.drawable.arrow_down);
drawable.setBounds(0, 0,
drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
spanText8.setSpan(new ImageSpan(drawable), 0, 8,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//自定义字体
spanText9.setSpan(new TextAppearanceSpan(getApplicationContext(),
R.style.style1), 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
SpannableStringBuilder spanString = new SpannableStringBuilder();
spanString.append(spanString1);
spanString.append(spanString2);
spanString.append(spanString3);
UUID是由一个十六位的数字组成,表现出来的形式例如
550E8400-E29B-11D4-A716-446655440000
public static String getUUID(){
String s = UUID.randomUUID().toString();
//去掉“-”符号
return s.substring(0,8)+s.substring(9,13)+
s.substring(14,18)+s.substring(19,23)+s.substring(24);
}
SharedPreferences shareTest=
MainActivity.this.getSharedPreferences("shareMy", Context.MODE_PRIVATE);
//mode指定为MODE_PRIVATE==0,则该配置文件只能被自己的应用程序访问
//mode指定为MODE_WORLD_READABLE,则该配置文件除了自己访问外还可以被其它应该程序读取
//mode指定为MODE_WORLD_WRITEABLE,则该配置文件除了自己访问外还可以被其它应该程序读取和写入
SharedPreferences.Editor editTest=shareTest.edit();
editTest.putString("key","value");
editTest.commit();
//取值
SharedPreferences shareTest=
MainActivity.this.getSharedPreferences("shareMy", Context.MODE_PRIVATE);
String temp=shareTest.getString("key", "test");//test 为默认的值
//清空
edit.clear();
//删除键值对
editor.remove("key");
/data/data/packageName/shared_prefs/shareName.xml
<map>
<string name="-6">144string>
<string name="3">144string>
<string name="-5">1443333string>
<string name="2">1254string>
<string name="1">1243string>
<string name="-7">144string>
map>
/**
存储对象
*/
//新建输出字节流,my为存储的对象
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
// 创建对象输出流,并封装字节流
ObjectOutputStream oos = new ObjectOutputStream(baos);
// 将对象写入字节流
oos.writeObject(my);
// 将字节流编码成base64的字符串
String oAuth_Base64 =
Base64.encodeToString(baos.toByteArray(),Base64.DEFAULT );
editTest.putString(count+"", oAuth_Base64);
editTest.commit();
}catch(IOException e) {
e.printStackTrace();}
}
/**
取对象
CRLF 就是Win风格的换行符,意思就是使用CRLF这一对作为一行的结尾而不是Unix风格的LF
DEFAULT 这个参数是默认,使用默认的方法来加密
NO_PADDING 这个参数是略去加密字符串最后的”=”
NO_WRAP 这个参数意思是略去所有的换行符(设置后CRLF就没用了)
URL_SAFE 这个参数意思是加密时不使用对URL和文件名
有特殊意义的字符来作为加密字符,具体就是以-和_取代+和/
*/
byte[] base64 = Base64.decode(temp.getBytes(), Base64.DEFAULT);
Name myout = null;
//封装到字节流
ByteArrayInputStream bais = new ByteArrayInputStream(base64);
try {
//再次封装
ObjectInputStream bis = new ObjectInputStream(bais);
try {
//读取对象
myout = (Name) bis.readObject();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (StreamCorruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
public static void showMyToast(Context ctx) {
View toastRoot = ((Activity) ctx).getLayoutInflater().inflate(
R.layout.toast_my, null);
Toast toastStart = new Toast(ctx);
toastStart.setGravity(Gravity.CENTER, 0, 0);
toastStart.setDuration(Toast.LENGTH_SHORT);
toastStart.setView(toastRoot);
toastStart.show();
}
由项目的构建顺序决定
public class NetUtil {
public static Boolean isNetWork(Context context) {
ConnectivityManager cm =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if(cm != null){
NetworkInfo[] info = cm.getAllNetworkInfo();
for(NetworkInfo n:info){
if(n.getState() == NetworkInfo.State.CONNECTED){
return true;
}
}
}
return false;
}
}
//public class ActivityPagerAdapter extends FragmentPagerAdapter {
public Object instantiateItem(ViewGroup container, int position) {
IntroductionFragment f =
(IntroductionFragment) super.instantiateItem(container, position);
f.setSimpleDesc(doctor.simpleDesc);
f.setGoodDisease(doctor.goodDisease);}
return f;
}
@Override
public int getItemPosition(Object object) {
return FragmentPagerAdapter.POSITION_NONE;
}
1.使用attach
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.ctx=activity;
}
2.新建构造方法
public PageFrag(Activity ctx) {
this.mActivity = ctx;
}
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
{code}
}
}, int delaytime);
hsv_title.smoothScrollTo(x,y);
mRotateUpAnim = new RotateAnimation(0.0f, -180.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);
mRotateUpAnim.setDuration(200);
//设为true之后,界面会停留在动画播放完时的界面
mRotateUpAnim.setFillAfter(true);
view.startAnimation(mRotateUpAnim);
退出虚拟机
arg | meaning |
---|---|
0 | normal |
1 | abnormal |
public static void loadImage(ImageView imageView,String url,int loadingImage,int emptyImage,int failImage){
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(loadingImage)
.showImageForEmptyUri(emptyImage)
.showImageOnFail(failImage)
.cacheInMemory(true)
.cacheOnDisc(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
ImageLoader.getInstance().displayImage(url, imageView, options);
}