Andrroid数据各类持久化工具类

程序猿是最懒的生物,开发中从不重复造轮子,实际开发中数据吃就化是必然要处理的一个问题,先总结了几个除处理sqlite外的几个工具类,因为sqlite可以直接用orm,持久化数据有I/O,SharedPreference等等方式。

外置储存卡

[java]  view plain copy
  1. package cn.edu.zafu.utils;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8. import java.io.UnsupportedEncodingException;  
  9.   
  10. import android.os.Environment;  
  11.   
  12. /** 
  13.  * 外部存儲卡工具类 
  14.  * 需要添加权限  
  15.  * android.permission.WRITE_EXTERNAL_STORAGE 
  16.  * android.permission.MOUNT_UNMOUNT_FILESYSTEMS 
  17.  *  
  18.  * @author lizhangqu 
  19.  * @version 1.0 
  20.  *  
  21.  */  
  22. public class ExternalStorageUtil {  
  23.   
  24.     /** 
  25.      * 是否可写 
  26.      *  
  27.      * @return 可写性 
  28.      */  
  29.     public static boolean isExternalStorageWritable() {  
  30.         String state = Environment.getExternalStorageState();  
  31.         if (Environment.MEDIA_MOUNTED.equals(state)) {  
  32.             return true;  
  33.         }  
  34.         return false;  
  35.     }  
  36.   
  37.     /** 
  38.      * 是否可读 
  39.      *  
  40.      * @return 可读性 
  41.      */  
  42.     public static boolean isExternalStorageReadable() {  
  43.         String state = Environment.getExternalStorageState();  
  44.         if (Environment.MEDIA_MOUNTED.equals(state)  
  45.                 || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {  
  46.             return true;  
  47.         }  
  48.         return false;  
  49.     }  
  50.   
  51.     /** 
  52.      * 获得根路径 
  53.      *  
  54.      * @return 外置内存卡根路径 
  55.      */  
  56.     public static String getExternalStoragePath() {  
  57.         if (isExternalStorageWritable())  
  58.             return Environment.getExternalStorageDirectory().getAbsolutePath();  
  59.         else  
  60.             return null;  
  61.     }  
  62.   
  63.     /** 
  64.      * 获得下载目录路径 
  65.      *  
  66.      * @return 外置内存卡下载路径 
  67.      */  
  68.     public static String getExternalDownloadPath() {  
  69.         return Environment.getExternalStoragePublicDirectory(  
  70.                 Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();  
  71.     }  
  72.   
  73.     /** 
  74.      * 向根路径写文件 
  75.      *  
  76.      * @param fileName 文件名 
  77.      * @param content 上下文 
  78.      * @return 是否写入成功 
  79.      */  
  80.     public static boolean write(String fileName, String content) {  
  81.         return write("/", fileName, content);  
  82.     }  
  83.   
  84.     /** 
  85.      * 向根目录写字节 
  86.      *  
  87.      * @param fileName 文件名 
  88.      * @param bytes 文件字节数组 
  89.      * @return 是否写入成功 
  90.      */  
  91.     public static boolean writeBytes(String fileName, byte[] bytes) {  
  92.         return writeBytes("/", fileName, bytes);  
  93.     }  
  94.   
  95.     /** 
  96.      * 向指定目录的文件中写入字符串,路径以/开始/结尾 
  97.      *  
  98.      * @param path 相对于根路径的路径,路径以/开始,以/结尾 
  99.      * @param fileName 文件名 
  100.      * @param content 文件内容 
  101.      * @return 是否写入成功 
  102.      */  
  103.     public static boolean write(String path, String fileName, String content) {  
  104.         return writeBytes(path, fileName, content.getBytes());  
  105.     }  
  106.   
  107.     /** 
  108.      * 向指定目录的文件写入字节数组,路径以/开始/结尾 
  109.      *  
  110.      * @param path 相对于根路径的路径,路径以/开始,以/结尾 
  111.      * @param fileName 文件名 
  112.      * @param bytes 字节数组 
  113.      * @return 
  114.      */  
  115.     public static boolean writeBytes(String path, String fileName, byte bytes[]) {  
  116.         boolean flag = false;  
  117.         if (!path.equals("/")) {  
  118.             File dir = new File(getExternalStoragePath() + path);  
  119.             if (!dir.exists()) {  
  120.                 if (!(dir.mkdir() || dir.isDirectory())) {  
  121.                     // 文件目录创建失败或者不是一个目录  
  122.                     return false;  
  123.                 }  
  124.             }  
  125.         }  
  126.         File file = new File(getExternalStoragePath() + path + fileName);  
  127.         FileOutputStream fos = null;  
  128.         try {  
  129.             fos = new FileOutputStream(file, false);  
  130.             fos.write(bytes);  
  131.             flag = true;  
  132.         } catch (FileNotFoundException e) {  
  133.             e.printStackTrace();  
  134.         } catch (IOException e) {  
  135.             e.printStackTrace();  
  136.         } finally {  
  137.             if (fos != null) {  
  138.                 try {  
  139.                     fos.close();  
  140.                 } catch (IOException e) {  
  141.                     e.printStackTrace();  
  142.                 }  
  143.             }  
  144.   
  145.         }  
  146.         return flag;  
  147.     }  
  148.   
  149.     /** 
  150.      * 从根路径读字节 
  151.      *  
  152.      * @param fileName 文件名 
  153.      * @return 字节数组 
  154.      */  
  155.     public static byte[] readBytes(String fileName) {  
  156.         return readBytes("/", fileName);  
  157.     }  
  158.   
  159.     /** 
  160.      * 从指定目录读字节,路径以/开始/结尾 
  161.      *  
  162.      * @param path 相对于根路径的路径,路径以/开始,以/结尾 
  163.      * @param fileName 文件名 
  164.      * @return 字节数组 
  165.      */  
  166.     public static byte[] readBytes(String path, String fileName) {  
  167.         File file = new File(getExternalStoragePath() + path + fileName);  
  168.         if (!file.isFile()) {  
  169.             return null;  
  170.         } else {  
  171.             FileInputStream fis = null;  
  172.             try {  
  173.                 fis = new FileInputStream(file);  
  174.                 int length = fis.available();  
  175.                 byte[] buffer = new byte[length];  
  176.                 fis.read(buffer);  
  177.                 return buffer;  
  178.             } catch (FileNotFoundException e) {  
  179.                 e.printStackTrace();  
  180.             } catch (IOException e) {  
  181.                 e.printStackTrace();  
  182.             } finally {  
  183.                 if (fis != null) {  
  184.                     try {  
  185.                         fis.close();  
  186.                     } catch (IOException e) {  
  187.                         e.printStackTrace();  
  188.                     }  
  189.                 }  
  190.             }  
  191.             return null;  
  192.         }  
  193.   
  194.     }  
  195.   
  196.     /** 
  197.      * 从根目录读文本 
  198.      *  
  199.      * @param fileName 文件名 
  200.      * @return 字符串 
  201.      */  
  202.     public static String read(String fileName) {  
  203.         return read("/", fileName);  
  204.     }  
  205.   
  206.     /** 
  207.      * 从指定目录读文本,路径以/开始/结尾 
  208.      *  
  209.      * @param path 相对于根路径的路径,路径以/开始,以/结尾 
  210.      * @param fileName 文件名 
  211.      * @return 字符串 
  212.      */  
  213.     public static String read(String path, String fileName) {  
  214.         try {  
  215.             byte[] readBytes = readBytes(path, fileName);  
  216.             if (readBytes == null) {  
  217.                 return null;  
  218.             }  
  219.             return new String(readBytes, "UTF-8");  
  220.         } catch (UnsupportedEncodingException e) {  
  221.             e.printStackTrace();  
  222.         }  
  223.         return null;  
  224.     }  
  225.   
  226.     /** 
  227.      * 从根目录删除 
  228.      *  
  229.      * @param fileName 文件名 
  230.      * @return 是否删除成功 
  231.      */  
  232.     public static boolean delete(String fileName) {  
  233.         return delete("/", fileName);  
  234.     }  
  235.   
  236.     /** 
  237.      * 从指定目录删除,路径以/开始/结尾 
  238.      *  
  239.      * @param path 相对于根路径的路径,路径以/开始,以/结尾 
  240.      * @param fileName 文件名 
  241.      * @return 是否删除成功 
  242.      */  
  243.     public static boolean delete(String path, String fileName) {  
  244.         File file = new File(getExternalStoragePath() + path + fileName);  
  245.         if (file.exists())  
  246.             return file.delete();  
  247.         else  
  248.             return true;  
  249.     }  
  250. }  

内置储存卡

[java]  view plain copy
  1. package cn.edu.zafu.utils;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8. import java.io.UnsupportedEncodingException;  
  9.   
  10. import android.content.Context;  
  11.   
  12. /** 
  13.  * 內部存儲卡工具类 
  14.  *  
  15.  * @author lizhangqu 
  16.  * @version 1.0 
  17.  */  
  18. public class InternalStorageUtil {  
  19.   
  20.     /** 
  21.      * 在原文件后追加内容 
  22.      *  
  23.      * @param context 上下文 
  24.      * @param fileName 文件名 
  25.      * @param content 追加的文本 
  26.      * @return 是否追加成功 
  27.      */  
  28.     public static boolean append(Context context, String fileName,  
  29.             String content) {  
  30.         return writeBytes(context, fileName, content.getBytes(), true);  
  31.     }  
  32.   
  33.     /** 
  34.      * 写入文件,文件存在则覆盖 
  35.      *  
  36.      * @param context 上下文 
  37.      * @param fileName 文件名 
  38.      * @param content 写入的文本 
  39.      * @return 是否写入成功 
  40.      */  
  41.     public static boolean write(Context context, String fileName, String content) {  
  42.         return writeBytes(context, fileName, content.getBytes(), false);  
  43.     }  
  44.   
  45.     /** 
  46.      * 写入字节 
  47.      *  
  48.      * @param context 上下文 
  49.      * @param fileName 文件名 
  50.      * @param content 写入的字节 
  51.      * @return 是否写入成功 
  52.      */  
  53.     public static boolean writeBytes(Context context, String fileName,  
  54.             byte[] content) {  
  55.         return writeBytes(context, fileName, content, false);  
  56.     }  
  57.   
  58.     /** 
  59.      * 写入文件,文件存在时根据参数isAppend判断是否覆盖 
  60.      *  
  61.      * @param context 上下文 
  62.      * @param fileName 文件名 
  63.      * @param content 写入的字节 
  64.      * @param isAppend 是否追加 
  65.      * @return 是否写入成功 
  66.      */  
  67.     public static boolean writeBytes(Context context, String fileName,  
  68.             byte[] content, boolean isAppend) {  
  69.         FileOutputStream fout = null;  
  70.         boolean flag = false;  
  71.         try {  
  72.             if (isAppend) {  
  73.                 fout = context.openFileOutput(fileName, Context.MODE_APPEND);  
  74.             } else {  
  75.   
  76.                 fout = context.openFileOutput(fileName, Context.MODE_PRIVATE);  
  77.             }  
  78.             fout.write(content);  
  79.             flag = true;  
  80.         } catch (FileNotFoundException e) {  
  81.             e.printStackTrace();  
  82.         } catch (IOException e) {  
  83.             e.printStackTrace();  
  84.         } finally {  
  85.             try {  
  86.                 if (fout != null) {  
  87.                     fout.close();  
  88.                     fout = null;  
  89.                 }  
  90.             } catch (IOException e) {  
  91.                 e.printStackTrace();  
  92.             }  
  93.         }  
  94.         return flag;  
  95.     }  
  96.   
  97.     /** 
  98.      * 读取文件 
  99.      *  
  100.      * @param context 上下文 
  101.      * @param fileName 文件名 
  102.      * @return 文件内容的字符串 
  103.      */  
  104.     public static String read(Context context, String fileName) {  
  105.         byte[] buffer = readBytes(context, fileName);  
  106.         String result=null;  
  107.         try {  
  108.             result = new String(buffer, "UTF-8");  
  109.         } catch (UnsupportedEncodingException e) {  
  110.             e.printStackTrace();  
  111.         }  
  112.         return result;  
  113.     }  
  114.     /** 
  115.      * @param context 上下文 
  116.      * @param fileName 文件名 
  117.      * @return 字节数组 
  118.      */  
  119.     public static byte[] readBytes(Context context, String fileName) {  
  120.         FileInputStream fin = null;  
  121.         byte[] buffer = null;  
  122.         try {  
  123.             fin = context.openFileInput(fileName);  
  124.             int length = fin.available();  
  125.             buffer = new byte[length];  
  126.             fin.read(buffer);  
  127.         } catch (FileNotFoundException e) {  
  128.             e.printStackTrace();  
  129.         } catch (IOException e) {  
  130.             e.printStackTrace();  
  131.         } finally {  
  132.             try {  
  133.                 if (fin != null) {  
  134.                     fin.close();  
  135.                     fin = null;  
  136.                 }  
  137.             } catch (IOException e) {  
  138.                 e.printStackTrace();  
  139.             }  
  140.         }  
  141.         return buffer;  
  142.     }  
  143.   
  144.     /** 
  145.      * 清除所有文件,当有一个文件未清除时返回false 
  146.      *  
  147.      * @param context 上下文 
  148.      * @return 是否清楚成功 
  149.      */  
  150.     public static boolean clear(Context context) {  
  151.         boolean flag = true;  
  152.         String[] files = context.fileList();  
  153.         for (String fileName : files) {  
  154.             boolean result = context.deleteFile(fileName);  
  155.             if (result == false) {  
  156.                 flag = false;  
  157.             }  
  158.         }  
  159.         return flag;  
  160.     }  
  161.   
  162.     /** 
  163.      * 根据文件名清除文件 
  164.      *  
  165.      * @param context 上下文 
  166.      * @param fileName 文件名 
  167.      * @return 是否删除成功 
  168.      */  
  169.     public static boolean delete(Context context, String fileName) {  
  170.         return context.deleteFile(fileName);  
  171.     }  
  172.   
  173.     /** 
  174.      * 返回内部存储的绝对路径 
  175.      *  
  176.      * @param context 上下文 
  177.      * @return app内置文件夹路径 
  178.      */  
  179.     public static String getFileDir(Context context) {  
  180.         File filesDir = context.getFilesDir();  
  181.         return filesDir.getAbsolutePath();  
  182.     }  
  183. }  
资源文件的读取

[java]  view plain copy
  1. package cn.edu.zafu.utils;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.io.UnsupportedEncodingException;  
  6.   
  7. import android.content.Context;  
  8.   
  9. /** 
  10.  * assert资源的读取 
  11.  *  
  12.  * @author lizhangqu 
  13.  * @version 1.0 
  14.  */  
  15. public class ResouceFileUtil {  
  16.     /** 
  17.      * 从assert文件夹下读取文本资源 
  18.      *  
  19.      * @param context 上下文 
  20.      * @param fileName 文件名 
  21.      * @return 文件内容字符串 
  22.      */  
  23.     public static String readStringFromAssert(Context context, String fileName) {  
  24.         String result = null;  
  25.         byte[] buffer = readBytesFromAssert(context, fileName);  
  26.         try {  
  27.             result = new String(buffer, "UTF-8");  
  28.         } catch (UnsupportedEncodingException e) {  
  29.             e.printStackTrace();  
  30.         }  
  31.         return result;  
  32.     }  
  33.   
  34.     /** 
  35.      * 从raw文件夹下读取文本资源 
  36.      *  
  37.      * @param context 上下文 
  38.      * @param rawId raw资源id 
  39.      * @return 文件内容字符串 
  40.      */  
  41.     public static String readStringFromRaw(Context context, int rawId) {  
  42.         String result = null;  
  43.         byte[] buffer = readBytesFromRaw(context, rawId);  
  44.         try {  
  45.             result = new String(buffer, "UTF-8");  
  46.         } catch (UnsupportedEncodingException e) {  
  47.             e.printStackTrace();  
  48.         }  
  49.         return result;  
  50.     }  
  51.   
  52.     /** 
  53.      * 从assert文件夹下读取文件到字节数组 
  54.      *  
  55.      * @param context 上下文 
  56.      * @param fileName 文件名 
  57.      * @return 文件字节数组 
  58.      */  
  59.     public static byte[] readBytesFromAssert(Context context, String fileName) {  
  60.         InputStream is = null;  
  61.         byte[] buffer = null;  
  62.         try {  
  63.             is = context.getAssets().open(fileName);  
  64.             int size = is.available();  
  65.             buffer = new byte[size];  
  66.             is.read(buffer);  
  67.         } catch (IOException e) {  
  68.             e.printStackTrace();  
  69.         } finally {  
  70.             if (is != null) {  
  71.                 try {  
  72.                     is.close();  
  73.                     is = null;  
  74.                 } catch (IOException e) {  
  75.                     e.printStackTrace();  
  76.                 }  
  77.             }  
  78.         }  
  79.   
  80.         return buffer;  
  81.     }  
  82.   
  83.     /** 
  84.      * 从raw文件夹下读取文件到字节数组 
  85.      *  
  86.      * @param context 上下文 
  87.      * @param rawId raw资源id 
  88.      * @return 文件字节数组 
  89.      */  
  90.     public static byte[] readBytesFromRaw(Context context, int rawId) {  
  91.         InputStream is = null;  
  92.         byte[] buffer = null;  
  93.         try {  
  94.             is = context.getResources().openRawResource(rawId);  
  95.             int size = is.available();  
  96.             buffer = new byte[size];  
  97.             is.read(buffer);  
  98.         } catch (IOException e) {  
  99.             e.printStackTrace();  
  100.         } finally {  
  101.             if (is != null) {  
  102.                 try {  
  103.                     is.close();  
  104.                     is = null;  
  105.                 } catch (IOException e) {  
  106.                     e.printStackTrace();  
  107.                 }  
  108.             }  
  109.         }  
  110.         return buffer;  
  111.     }  
  112. }  


SharedPreference的操作

[java]  view plain copy
  1. package cn.edu.zafu.utils;  
  2.   
  3. import android.content.Context;  
  4. import android.content.SharedPreferences;  
  5.   
  6. /** 
  7.  * SharedPreference方式持久化数据的工具类 
  8.  *  
  9.  * @author lizhangqu 
  10.  * @version 1.0 
  11.  */  
  12. public class SharedPreferenceUtil {  
  13.   
  14.     /** 
  15.      * 保存键值对 
  16.      *  
  17.      * @param context 上下文 
  18.      * @param fileName 文件名 
  19.      * @param key 键 
  20.      * @param value 值 
  21.      * @return 是否保存成功 
  22.      */  
  23.     public static boolean set(Context context, String fileName, String key,  
  24.             String value) {  
  25.         SharedPreferences sharedPreferences = context.getSharedPreferences(  
  26.                 fileName, Context.MODE_PRIVATE);  
  27.         SharedPreferences.Editor editor = sharedPreferences.edit();  
  28.         editor.putString(key, value);  
  29.         return editor.commit();  
  30.     }  
  31.   
  32.     /** 
  33.      * 获得键对应的值,如果没有则返回"" 
  34.      *  
  35.      * @param context 上下文 
  36.      * @param fileName 文件名 
  37.      * @param key 键 
  38.      * @return 值,没有则返回"" 
  39.      */  
  40.     public static String get(Context context, String fileName, String key) {  
  41.         return get(context, fileName, key, "");  
  42.     }  
  43.   
  44.     /** 
  45.      * 获得键对应的值,如果没有则返回defaultValue 
  46.      *  
  47.      * @param context 上下文 
  48.      * @param fileName 文件名 
  49.      * @param key 键 
  50.      * @param defaultValue 默认值 
  51.      * @return 值,没有则返回defaultValue 
  52.      */  
  53.     public static String get(Context context, String fileName, String key,  
  54.             String defaultValue) {  
  55.         SharedPreferences sharedPreferences = context.getSharedPreferences(  
  56.                 fileName, Context.MODE_PRIVATE);  
  57.         String value = sharedPreferences.getString(key, defaultValue);// 第二个参数为默认值  
  58.         return value;  
  59.     }  
  60.   
  61.     /** 
  62.      * 移除一项 
  63.      * @param context 上下文 
  64.      * @param fileName 文件名 
  65.      * @param key 键 
  66.      * @return 是否移除成功 
  67.      */  
  68.     public static boolean remove(Context context, String fileName, String key) {  
  69.         SharedPreferences sharedPreferences = context.getSharedPreferences(  
  70.                 fileName, Context.MODE_PRIVATE);  
  71.         SharedPreferences.Editor editor = sharedPreferences.edit();  
  72.         editor.remove(key);  
  73.         return editor.commit();  
  74.   
  75.     }  
  76.       
  77.     /** 
  78.      * 清除文件内容 
  79.      * @param context 上下文 
  80.      * @param fileName 文件名 
  81.      * @return 是否清除成功 
  82.      */  
  83.     public static boolean clear(Context context, String fileName) {  
  84.         SharedPreferences sharedPreferences = context.getSharedPreferences(  
  85.                 fileName, Context.MODE_PRIVATE);  
  86.         SharedPreferences.Editor editor = sharedPreferences.edit();  
  87.         editor.clear();  
  88.         return editor.commit();  
  89.   
  90.     }  
  91.       
  92.     /** 
  93.      * 某一项是否存在 
  94.      * @param context 上下文 
  95.      * @param fileName 文件名 
  96.      * @param key 键 
  97.      * @return 该键对应的值是否存在 
  98.      */  
  99.     public static boolean contatins(Context context, String fileName,String key) {  
  100.         SharedPreferences sharedPreferences = context.getSharedPreferences(  
  101.                 fileName, Context.MODE_PRIVATE);  
  102.         return sharedPreferences.contains(key);  
  103.           
  104.   
  105.     }  
  106.   
  107. }  


对烦人的findViewById说再见

下面的这个函数参考自一条微博

[java]  view plain copy
  1. public <T extends View> T $(int id) {  
  2.     return (T) findViewById(id);  
  3. }  

<T extends View>说明这是一个泛型方法,并且这个泛型是View的子类,返回值是泛型T,函数名借鉴JQuery,使用美元符$

实例调用代码

[html]  view plain copy
  1. TextView tv=$(R.id.tv);  

为了避免在每个Activity中重复写这个函数,可以写一个自己的MyActivity类继承Activity,实现该方法,以后的所有Activity继承自己的MyActivity类即可。在一定程度上减少了findViewById函数的书写,不得不说程序猿是世界上最懒的生物,总是想尽千方百计偷懒!

LayoutInflate的三种用法

在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById()。不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例化;而findViewById()是找xml布局文件下的具体widget控件(如Button、TextView等)。 具体作用:

1、对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;

2、对于一个已经载入的界面,就可以使用Activiyt.findViewById()方法来获得其中的界面元素。

 

LayoutInflater 是一个抽象类,在文档中如下声明:

 

  public abstract class LayoutInflater extends Object

 

获得 LayoutInflater 实例的三种方式:

[java]  view plain copy
  1. 1.LayoutInflater inflater = getLayoutInflater();  //调用Activity的getLayoutInflater()  
  2.   
  3. 2.LayoutInflater localinflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  4.   
  5. 3. LayoutInflater inflater = LayoutInflater.from(context);    


 

其实,这三种方式本质是相同的,从源码中可以看出:

getLayoutInflater():

Activity 的 getLayoutInflater() 方法是调用 PhoneWindow 的getLayoutInflater()方法,看一下该源代码:

[java]  view plain copy
  1. public PhoneWindow(Context context) {           
  2.   
  3.    super(context);           
  4.   
  5.    mLayoutInflater = LayoutInflater.from(context);   
  6.   
  7.  }  


可以看出它其实是调用 LayoutInflater.from(context)。

 

LayoutInflater.from(context):

[java]  view plain copy
  1. public static LayoutInflater from(Context context) {        
  2.   
  3.    LayoutInflater LayoutInflater =  (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);         
  4.   
  5.    if (LayoutInflater ==null) {            
  6.   
  7.        throw new AssertionError("LayoutInflater not found.");        
  8.   
  9.    }        
  10.   
  11.    return LayoutInflater;    
  12.   
  13.  }  


可以看出它其实调用 context.getSystemService()。

 

结论:所以这三种方式最终本质是都是调用的Context.getSystemService()。

 

inflate 方法 通过 sdk 的 api 文档,可以知道该方法有以下几种过载形式,返回值均是 View 对象,如下:

[java]  view plain copy
  1. public View inflate (int resource, ViewGroup root);    
  2.     public View inflate (XmlPullParser parser, ViewGroup root);  
  3.     public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot);     
  4.     public View inflate (int resource, ViewGroup root, boolean attachToRoot);   
  5.   
  6.     LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);      
  7.     View view = inflater.inflate(R.layout.custom, (ViewGroup)findViewById(R.id.test));      
  8.     //EditText editText = (EditText)findViewById(R.id.content);  
  9.     // error   
  10.     EditText editText = (EditText)view.findViewById(R.id.content);  


对于上面代码,指定了第二个参数 ViewGroup root,当然你也可以设置为 null 值。

注意:

·inflate方法与 findViewById 方法不同;

·inflater 是用来找 res/layout下的 xml 布局文件,并且实例化;

·findViewById() 是找具体 xml 布局文件中的具体 widget 控件(如:Button、TextView 等)。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值