博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Easy Code
阅读量:5037 次
发布时间:2019-06-12

本文共 6595 字,大约阅读时间需要 21 分钟。

1创建文件 

File Directory=mContext.getDir("mydir", Context.MODE_PRIVATE);File dest = new File(Directory, filename); //FileUtils.copyURLToFile( src, dest );

 2 读取文件 

public static Drawable loadImageFromFilesystem(Context context,			String filename, int originalDensity) {				File Directory = context.getDir("mydir", Context.MODE_PRIVATE);		File f = new File( Directory , filename );		InputStream is = null;		try {			is = new FileInputStream(f);		} catch (FileNotFoundException e) {			e.printStackTrace();		}				Bitmap bmp = BitmapFactory.decodeStream(is );				Drawable drawable = new BitmapDrawable(bmp);  ;		return drawable;	}

 3删除文件(目录) 

public static void delFolder(String folderPath) {		try {			delAllFile(folderPath); // 删除完里面所有内容			String filePath = folderPath;			filePath = filePath.toString();			java.io.File myFilePath = new java.io.File(filePath);			myFilePath.delete(); // 删除空文件夹		} catch (Exception e) {			System.out.println("删除文件夹操作出错");			e.printStackTrace();		}	}		public static void delAllFile(String path) {		File file = new File(path);		if (!file.exists()) {			return;		}		if (!file.isDirectory()) {			return;		}		String[] tempList = file.list();		File temp = null;		for (int i = 0; i < tempList.length; i++) {			if (path.endsWith(File.separator)) {				temp = new File(path + tempList[i]);			} else {				temp = new File(path + File.separator + tempList[i]);			}			if (temp.isFile()) {				temp.delete();			}			if (temp.isDirectory()) {				delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件				delFolder(path + "/" + tempList[i]);// 再删除空文件夹			}		}	}

  4 按键

public boolean onKeyDown(int keyCode, KeyEvent event)      {          switch(keyCode)          {          case KeyEvent.KEYCODE_BACK:                          return true;        }          return super.onKeyDown(keyCode, event);      }

5 资源

 button.setTextColor(mContext.getResources().getColor(R.color.word_color));

text.setTextColor(Color.parseColor("#FFFFFF")); 

try {			XmlResourceParser parser = mContext.getResources().getXml(					R.color.word_color);			ColorStateList colors = ColorStateList.createFromXml(mContext.getResources(),parser);			button.setTextColor(colors);		} catch (XmlPullParserException e) {			// TODO Auto-generated catch block			e.printStackTrace();		} catch (IOException e) {			// TODO Auto-generated catch block			e.printStackTrace();		}R.color.word_color:

 6.自定义对话框

AlertDialog myDialog = new AlertDialog.Builder(UpdateActivity.this).create();          myDialog.show();          myDialog.getWindow().setContentView(R.layout.update);                 TextView tv = (TextView)(myDialog.getWindow()).findViewById(R.id.content);        tv.setText("text");

7.button —— bg

View Code

 8. background selector 代码实现

private StateListDrawable setBackground(int normalResId, int pressedResId,			int focusedResId) {		StateListDrawable bg = new StateListDrawable();		Drawable normal = getResources().getDrawable(normalResId);		Drawable selected = getResources().getDrawable(focusedResId);		Drawable pressed = getResources().getDrawable(pressedResId);		bg.addState(View.PRESSED_ENABLED_STATE_SET, pressed);		bg.addState(View.ENABLED_FOCUSED_STATE_SET, selected);		bg.addState(View.ENABLED_STATE_SET, normal);		bg.addState(View.FOCUSED_STATE_SET, selected);		bg.addState(View.EMPTY_STATE_SET, normal);		return bg;	}private void setButtonBackGroundDrawable(Drawable d) {		this.setBackgroundDrawable(d);	}

  9.map

private ArrayList
> mContentViews ;mContentViews = new ArrayList
>(); mSettingPrivacy= new SettingPrivacy(context);HashMap
map_Privacy = new HashMap
(); map_Privacy.put(new Integer(SETTING_TYPE_PRIVACY), mSettingPrivacy); mContentViews.add(map_Privacy);private View getView(int position){ for(HashMap map : mContentViews){ Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); if((Integer)(entry.getKey()) == mSettingType[position]){ return (View)entry.getValue(); } } } return null; }

  10焦点

setFocusableInTouchMode(true);

11.copy asserts to data

File startPageCacheDirectory = getDir("images", Context.MODE_PRIVATE);            		try {			String[] dir = getAssets().list("image");			for(int i = 0; i < dir.length; i++){				Log.d("iamge", dir[i]);				File dest = new File(startPageCacheDirectory, dir[i]);				copyToDataDir(getAssets().open("image/"+ dir[i]), dest);			}		} catch (IOException e) {			// TODO Auto-generated catch block			e.printStackTrace();		}        public void copyToDataDir(InputStream srcFile, File destFile)throws IOException{    	 if (srcFile == null) {             throw new NullPointerException("Source must not be null");         }         if (destFile == null) {             throw new NullPointerException("Destination must not be null");         }                       if (destFile.getParentFile() != null && destFile.getParentFile().exists() == false) {             if (destFile.getParentFile().mkdirs() == false) {                 throw new IOException("Destination '" + destFile + "' directory cannot be created");             }         }         if (destFile.exists() && destFile.canWrite() == false) {             throw new IOException("Destination '" + destFile + "' exists but is read-only");         }                      try {             FileOutputStream output = new FileOutputStream(destFile);             try {                 IOUtils.copy(srcFile, output);             } finally {                 IOUtils.closeQuietly(output);             }         } finally {             IOUtils.closeQuietly(srcFile);         }            }

  11.file name

String filename = url.substring(url.lastIndexOf("/") + 1);

12.判断联网

public class NetWorkUtils {	public static boolean isInternetConnected(Context ctx) {		ConnectivityManager manager = (ConnectivityManager) ctx.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);		NetworkInfo info = manager.getActiveNetworkInfo();		if (info == null || !info.isConnected())			return false;		else			return true;	}}

 13旋转

LinearInterpolator lin = new LinearInterpolator();		Animation am = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF,				0.5f, Animation.RELATIVE_TO_SELF, 0.5f);		am.setDuration(500);		// 动画重复次数(-1 表示一直重复)		am.setRepeatCount(-1);		am.setRepeatCount(Animation.INFINITE);		am.setInterpolator(lin);		// 图片配置动画		mImage.setAnimation(am);		am.startNow();

  

  

 

转载于:https://www.cnblogs.com/cornellbox/archive/2012/05/09/2491205.html

你可能感兴趣的文章
时间模块 && time datetime
查看>>
jquery自动生成二维码
查看>>
spring回滚数据
查看>>
新浪分享API应用的开发
查看>>
美国专利
查看>>
【JavaScript】Write和Writeln的区别
查看>>
百度编辑器图片在线流量返回url改动
查看>>
我对你的期望有点过了
查看>>
微信小程序wx:key以及wx:key=" *this"详解:
查看>>
下拉框比较符
查看>>
2.2.5 因子的使用
查看>>
css选择器
查看>>
photoplus
查看>>
Python 拓展之推导式
查看>>
[Leetcode] DP-- 474. Ones and Zeroes
查看>>
80X86寄存器详解<转载>
查看>>
c# aop讲解
查看>>
iterable与iterator
查看>>
返回顶部(动画)
查看>>
webpack+react+antd 单页面应用实例
查看>>