Easy Code

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:
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:color="#ff2a3137" />
    <item android:state_enabled="true" android:state_pressed="true" android:color="#ff00ffff" />
    <item android:state_enabled="true" android:color="#ffbbbbbb" />
    <item android:state_focused="true" android:color="#ffffff00" />
    <item android:color="#ffbbbbbb" />
</selector>

 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
<?xml version="1.0" encoding="utf-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:drawable="@drawable/button_bg_focused" />
    <item android:state_enabled="true" android:state_pressed="true" android:drawable="@drawable/button_bg_pressed" />
    <item android:state_enabled="true" android:drawable="@drawable/button_bg_normal" />
</selector>

 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<HashMap<Integer, View>> mContentViews ;
mContentViews = new ArrayList<HashMap<Integer, View>>(); 

mSettingPrivacy= new SettingPrivacy(context);
HashMap<Integer, View> map_Privacy = new HashMap<Integer, View>(); 
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();

  

  

 

posted @ 2012-05-09 09:27  cornellbox  阅读(392)  评论(0编辑  收藏  举报