【Android 应用开发】Android开发技巧--Application, ListView排列,格式化浮点数,string.xml占位符,动态引用图片
来源:互联网 发布:php网址跳转代码 编辑:程序博客网 时间:2024/05/16 16:07
一. 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™ 2.1 android.R.drawable Icon Resources
- Android™ 1.5 android.R.drawable Icon Resources
- Android™ 1.6 android.R.drawable Icon Resources
- Android™ 2.1 android.R.drawable Icon Resources
- Originated from: http://www.darshancomputing.com/android/1.5-drawables.html
This is a list of resources in Android 2.1's android.R.drawable
that might be useful as icons in your Android applications. You can use them like this:
myMenuItem.setIcon(android.R.drawable.ic_menu_save);
android:icon="@android:drawable/ic_menu_save"
alert_dark_frame
alert_light_frame
arrow_down_float
arrow_up_float
bottom_bar
btn_default
btn_default_small
btn_dialog
btn_dropdown
btn_minus
btn_plus
btn_radio
btn_star
btn_star_big_off
btn_star_big_on
button_onoff_indicator_off
button_onoff_indicator_on
checkbox_off_background
checkbox_on_background
dark_header
dialog_frame
divider_horizontal_bright
divider_horizontal_dark
divider_horizontal_dim_dark
divider_horizontal_textfield
edit_text
editbox_background
editbox_background_normal
editbox_dropdown_dark_frame
editbox_dropdown_light_frame
gallery_thumb
ic_btn_speak_now
ic_delete
ic_dialog_alert
ic_dialog_dialer
ic_dialog_email
ic_dialog_info
ic_dialog_map
ic_input_add
ic_input_delete
ic_input_get
ic_lock_idle_alarm
ic_lock_idle_charging
ic_lock_idle_lock
ic_lock_idle_low_battery
ic_lock_lock
ic_lock_power_off
ic_lock_silent_mode
ic_lock_silent_mode_off
ic_media_ff
ic_media_next
ic_media_pause
ic_media_play
ic_media_previous
ic_media_rew
ic_menu_add
ic_menu_agenda
ic_menu_always_landscape_portrait
ic_menu_call
ic_menu_camera
ic_menu_close_clear_cancel
ic_menu_compass
ic_menu_crop
ic_menu_day
ic_menu_delete
ic_menu_directions
ic_menu_edit
ic_menu_gallery
ic_menu_help
ic_menu_info_details
ic_menu_manage
ic_menu_mapmode
ic_menu_month
ic_menu_more
ic_menu_my_calendar
ic_menu_mylocation
ic_menu_myplaces
ic_menu_preferences
ic_menu_recent_history
ic_menu_report_image
ic_menu_revert
ic_menu_rotate
ic_menu_save
ic_menu_search
ic_menu_send
ic_menu_set_as
ic_menu_share
ic_menu_slideshow
ic_menu_sort_alphabetically
ic_menu_sort_by_size
ic_menu_today
ic_menu_upload
ic_menu_upload_you_tube
ic_menu_view
ic_menu_week
ic_menu_zoom
ic_notification_clear_all
ic_notification_overlay
ic_partial_secure
ic_popup_disk_full
ic_popup_reminder
ic_popup_sync
ic_search_category_default
ic_secure
menu_frame
menu_full_frame
picture_frame
presence_away
presence_busy
presence_invisible
presence_offline
presence_online
progress_indeterminate_horizontal
radiobutton_off_background
radiobutton_on_background
spinner_background
spinner_dropdown_background
star_big_off
star_big_on
star_off
star_on
stat_notify_call_mute
stat_notify_chat
stat_notify_error
stat_notify_missed_call
stat_notify_more
stat_notify_sdcard
stat_notify_sdcard_prepare
stat_notify_sdcard_usb
stat_notify_sync
stat_notify_sync_noanim
stat_notify_voicemail
stat_sys_data_bluetooth
stat_sys_download
stat_sys_download_done
stat_sys_headset
stat_sys_phone_call
stat_sys_phone_call_forward
stat_sys_phone_call_on_hold
stat_sys_speakerphone
stat_sys_upload
stat_sys_upload_done
stat_sys_vp_phone_call
stat_sys_vp_phone_call_on_hold
stat_sys_warning
status_bar_item_app_background
status_bar_item_background
sym_action_call
sym_action_chat
sym_action_email
sym_call_incoming
sym_call_missed
sym_call_outgoing
sym_contact_card
sym_def_app_icon
title_bar
title_bar_tall
toast_frame
zoom_plate
转载
http://docs.since2006.com/android/2.1-drawables.php
七 Android 不显示标题栏和全屏的设置方法
1.在Manifest.xml中设置
不显示标题栏
android:theme="@android:style/Theme.NoTitleBar"
全屏
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
2.在代码中实现
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
- 【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应用开发之(图片处理技巧一)
- javacore note
- 微信自定义菜单json字符串解析
- client mac addr: DHCP FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
- 2013年北邮网研院上机真题(D)
- C++11: 新手易学,老兵易用
- 【Android 应用开发】Android开发技巧--Application, ListView排列,格式化浮点数,string.xml占位符,动态引用图片
- 数据库连接池简介
- salesforce pagereference 和接收url上传递的参数
- 15分钟学会使用Git和远程代码库
- 矩阵分析5.1节
- 关于alfa无线设备
- new String("abc")与String a="abc"的区别
- servlet远程操作内存
- vitualbox虚拟机下Ubuntu网络设置