【Android 应用开发】Android开发技巧--Application, ListView排列,格式化浮点数,string.xml占位符,动态引用图片
来源:互联网 发布:brite网络拓扑生成器 编辑:程序博客网 时间:2024/06/05 23:01
一. Application用途
1. Application用途
创建Application时机 : Application在启动的时候会调用Application无参的构造方法创建实例;
Application构造方法 : Application的无参的构造方法必须是public的, 否则运行的时候会出现错误.
Application单例 : 在一个应用中, Application是单例的;
Application用途 : 所有的组件共享一个Application, 可以使用Application共享, 传递, 缓存数据;
Application用法 :
a. 组件间数据传递 : 组件之间传递数据的数据量比较大的时候, 尽量不要使用Intent附加域来传递, 可以将数据放在Application中, 以Application作为中转站;
b. 下载数据 : 从网络上下载的数据, 也可以暂时缓存到Application中, 如果下载的数据过多, 可以缓存到SD卡中;
c. 注意内存泄露 : Application是静态的, 存放数据的时候注意声明周期, 不要造成内存泄露;
2. 自定义Application技巧
在应用中经常用到自定义的Application, 自定义一个MyApplication, 将Application设置成单例, 并且在AndroidManifest.xml中注册这个MyApplication;
(1)设置MyApplication单例
a. 定义私有 静态 的MyApplication;
b. 设置一个公有的静态方法, 将this 赋值给自定义的MyApplication对象;
c. 设置一个获取MyApplication对象的方法, 该方法公共 静态;
public class MyApplication extends Application {/* * 单例模式 : 私有 静态 本类的对象 */private static MyApplication mApplication;/* * 单例模式 : 构造方法 , 注意 : Application的构造方法必须是public的 */public ReceiverApplication(){mApplication = this;}/* * 单例模式 : 公共静态获取方法 */public static MyApplication getInstance() {return mApplication;}}
这样调用getInstance()方法, 就可以在任何类中调用Application了, 并能取到该类中的数据;
(2)在AndroidManifest.xml中注册自定义的Application
<manifest xmlns:android="http://schemas.android.com/apk/res/android" > <application android:name=".MyApplication" > </application></manifest>
(3)获取Application方法
调用系统方法 : 在Activity中可以调用getApplicationContext()方法获取Application;
通过自定义的方法 : 如我们上面自定义的Application那样, 可以调用自定义的getInstance()方法获取Application实例对象;
二. ListView中元素的排序
ListView中的元素排序, 即将数据源排序即可;
给集合排序的方法 : 调用Collections的sort(list, Comparator)方法, 该方法需要2个参数, 第一个参数就是需要排序的集合, 第二个参数是比较器;
这里的比较器需要创建, 并且重写其中的compare()方法, compare()方法返回1或者-1, 用此来控制排序的升序还是降序;
Collections.sort(mList, new Comparator<Integer>() { @Override public int compare(Integer a, Integer b) { if (a>b) { return 1; } return -1; } });
这样就会将mList集合自动进行排序;
三. 格式化浮点数
如何在程序中保留一个float或者double浮点数的小数位数 , 这里可以使用以下几种方法 :
1.使用DecimalFormat格式化浮点数
例如 : System.out.println(new java.text.DecimalFormat("0.00").format(3.135)); 5最近的偶数是4, 打印的结果就是3.14;
System.out.println(new java.text.DecimalFormat("0.00").format(3.125)); 5最近的偶数是2, 打印的结果就是3.12;
float pi = 3.1415926f;DecimalFormat decimalFormat = new DecimalFormat("#.00");String formatData = decimalFormat.format(pi);System.out.println(formatData);System.out.println(new DecimalFormat("#.00").format(pi));
2.利用BigDecimal实现
float pi = 3.1415926f;BigDecimal bigDecimal = new BigDecimal(pi);float result = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();System.out.println(result);
与浮点数有关的构造方法 : 可以向构造方法中传入浮点数 或者 字符串 , 这里需要注意的是 , 使用浮点数的构造方法不精确 , 这个值最后会有不该有的数据,尽量使用字符串的构造方法.
BigDecimal bd1=new BigDecimal(0.05); System.out.println(bd1.toString()); BigDecimal bd2=new BigDecimal("0.05"); System.out.println(bd2.toString());结果是 :
0.05000000000000000277555756156289135105907917022705078125 0.05因此使用字符串的构造函数,获得的数据更精确.
BigDecimal bd3=new BigDecimal(String.valueOf(0.05)); BigDecimal bd4=new BigDecimal(String.valueOf(0.01)); System.out.println((bd3.add(bd4)).doubleValue());
减法计算 :
BigDecimal bd5=new BigDecimal(String.valueOf(0.05)); BigDecimal bd6=new BigDecimal(String.valueOf(0.01)); System.out.println((bd5.subtract(bd6)).doubleValue());
乘法计算 :
BigDecimal bd7=new BigDecimal(String.valueOf(0.05)); BigDecimal bd8=new BigDecimal(String.valueOf(0.01)); System.out.println((bd7.multiply(bd8)).doubleValue());
除法计算 :
//这里没有考虑数据错误的可能情况 //定义了精确位数 int scale=10; BigDecimal bd9=new BigDecimal(String.valueOf(0.05)); BigDecimal bd10=new BigDecimal(String.valueOf(0.03)); System.out.println((bd9.divide(bd10,scale,BigDecimal.ROUND_HALF_EVEN)).doubleValue());
四舍五入:
//四舍五入 scale=4; BigDecimal bd11=new BigDecimal(String.valueOf(3.1415926)); System.out.println(bd11.setScale(scale,BigDecimal.ROUND_HALF_UP).toString());
四舍五入的精确模式 :
- ROUND_CEILING : 向正无穷方向舍入 .
- ROUND_DOWN : 向零方向舍入
- ROUND_FLOOR : 向负无穷方向射舍入
- ROUND_HALF_DOWN : 向距离近的一方舍入 , 如果两边相等 , 向下舍入 , 例如 2.155 , 保留2位小数的话 是 2.15;
- ROUND_HALF_UP : 向距离近的一方舍入 , 如果两边相等 , 向上舍入 , 例如 2.155,保留两位小数的话 是 2.16; 这个就是四舍五入
- ROUND_HALF_EVEN : 向距离近的一方舍入 , 如果两边距离相等 , 如果保留位是奇数位 使用ROUND_HALF_UP , 如果保留位是偶数位,使用ROUND_HALF_DOWN;
- ROUND_UNNECESSARY : 精确的计算 , 不需要舍入 .
- ROUND_UP : 向远离0的方向舍入.
四. string.xml占位符
开发中经常遇到这样的情况 , 在string.xml中用到以下占位符
<string name="delete_success">删除<xliff:g id="name">%1$s</xliff:g>成功</string>
<string name="upload_success">%1$s上传到%2$s成功</string>
1.xliff:g标签
五. 动态引用图片
在资源文件中存放有 image_1.png, image_2.png, image_3.png 三张图片 , 根据传入参数动态引用对应的图片 , 有三个解决方法
根据R.drawable.xx动态引用是错误的 , 因为每个这种id都对应着R文件中的一个id,如果没有相对应的id , 编译不会通过;
建立一个工程,包名为com.yun.demo
方案一 : 图片放在drawable目录下的情况
Resources resources = this.getResources();int imageIndentify = resources.getIdentifier(imageName, "drawable","chao.yun.demo");使用上面的代码可以通过字符串拼接图片名称 , 根据传入的参数 , 拼接imageName字符串 , 从而动态的获取图片对应的id;
resources.getIdentifier(imageName, "drawable","chao.yun.demo");这个方法返回的是图片对应的id ;
详细代码 :
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/ll_1" android:layout_width="fill_parent" android:layout_height="0px" android:layout_weight="1"></LinearLayout> <LinearLayout android:id="@+id/ll_2" android:layout_width="fill_parent" android:layout_height="0px" android:layout_weight="1"></LinearLayout> <LinearLayout android:id="@+id/ll_3" android:layout_width="fill_parent" android:layout_height="0px" android:layout_weight="1"></LinearLayout></LinearLayout>activity代码 :
public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); LinearLayout ll_1 = (LinearLayout) findViewById(R.id.ll_1); LinearLayout ll_2 = (LinearLayout) findViewById(R.id.ll_2); LinearLayout ll_3 = (LinearLayout) findViewById(R.id.ll_3); Resources resources = this.getResources();String imageName = "image_" + 1;int imageIndentify = resources.getIdentifier(imageName, "drawable","chao.yun.demo");ll_1.setBackgroundResource(imageIndentify);imageName = "image_" + 2;imageIndentify = resources.getIdentifier(imageName, "drawable","chao.yun.demo");ll_2.setBackgroundResource(imageIndentify);imageName = "image_" + 3;imageIndentify = resources.getIdentifier(imageName, "drawable","chao.yun.demo");ll_3.setBackgroundResource(imageIndentify); }}
- 【Android 应用开发】Android开发技巧--Application, ListView排列,格式化浮点数,string.xml占位符,动态引用图片
- 【Android 应用开发】Android开发技巧--Application, ListView排列,格式化浮点数,string.xml占位符,动态引用图片
- Android:格式化占位符 动态修改string.xml文件的值
- android string.xml占位符
- Android ListView开发技巧
- Android,string.xml中使用占位符
- Android项目中string.xml占位符
- android string.xml中的占位符
- android string.xml中使用占位符
- android string.xml中使用占位符
- Android ListView开发小技巧
- Android开发 Edittext限制输入浮点数
- android string.xml格式化
- Android开发技巧:Application和Instance
- Android开发技巧:Application和Instance
- Android开发技巧三--格式化TextView文本
- android开发图片小技巧
- Android应用开发之(图片处理技巧一)
- 【模拟】篝火晚会(noip2005)
- 通过DBCC PAGE查看页信息验证聚集索引和非聚集索引节点信息
- 通过wifi连接android设备的方法
- 近期计划
- C#知识点总结系列:1、C#中Hashtable、Dictionary详解以及写入和读取对比
- 【Android 应用开发】Android开发技巧--Application, ListView排列,格式化浮点数,string.xml占位符,动态引用图片
- Android修改AVD默认路径
- 女孩武汉丢iPhone5定位郑州 警方嫌麻烦不受理
- 04-EasyUI_登陆的实现
- 在SecureCRT下使用sz下载和rz上传文件
- 关于那些第三方在线编辑器ckeditor、KindEditor等在火狐或谷歌下无法进入输入框的一个问题
- HTTPS 要比 HTTP 多用多少服务器资源?
- 如何将App程序发布到App Store(图文+视频)
- javascript IN 的用法一览