日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

【Android 应用开发】Android开发技巧--Application, ListView排列,格式化浮点数,string.xml占位符,动态引用图片

發(fā)布時(shí)間:2025/6/17 Android 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Android 应用开发】Android开发技巧--Application, ListView排列,格式化浮点数,string.xml占位符,动态引用图片 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一. Application用途


1. Application用途

創(chuàng)建Application時(shí)機(jī)?: Application在啟動(dòng)的時(shí)候會(huì)調(diào)用Application無參的構(gòu)造方法創(chuàng)建實(shí)例;?

Application構(gòu)造方法?: Application的無參的構(gòu)造方法必須是public的, 否則運(yùn)行的時(shí)候會(huì)出現(xiàn)錯(cuò)誤.

Application單例?: 在一個(gè)應(yīng)用中, Application是單例的;

Application用途?: 所有的組件共享一個(gè)Application, 可以使用Application共享, 傳遞, 緩存數(shù)據(jù);

Application用法?:?

a.?組件間數(shù)據(jù)傳遞?: 組件之間傳遞數(shù)據(jù)的數(shù)據(jù)量比較大的時(shí)候, 盡量不要使用Intent附加域來傳遞, 可以將數(shù)據(jù)放在Application中, 以Application作為中轉(zhuǎn)站;

b.?下載數(shù)據(jù)?: 從網(wǎng)絡(luò)上下載的數(shù)據(jù), 也可以暫時(shí)緩存到Application中, 如果下載的數(shù)據(jù)過多, 可以緩存到SD卡中;

c.?注意內(nèi)存泄露?: Application是靜態(tài)的, 存放數(shù)據(jù)的時(shí)候注意聲明周期, 不要造成內(nèi)存泄露;

2. 自定義Application技巧


在應(yīng)用中經(jīng)常用到自定義的Application, 自定義一個(gè)MyApplication, 將Application設(shè)置成單例, 并且在AndroidManifest.xml中注冊(cè)這個(gè)MyApplication;


(1)設(shè)置MyApplication單例

a. 定義私有 靜態(tài) 的MyApplication;

b. 設(shè)置一個(gè)公有的靜態(tài)方法, 將this 賦值給自定義的MyApplication對(duì)象;

c. 設(shè)置一個(gè)獲取MyApplication對(duì)象的方法, 該方法公共 靜態(tài);

public class MyApplication extends Application {/** 單例模式 : 私有 靜態(tài) 本類的對(duì)象*/private static MyApplication mApplication;/** 單例模式 : 構(gòu)造方法 , 注意 : Application的構(gòu)造方法必須是public的*/public ReceiverApplication(){mApplication = this;}/** 單例模式 : 公共靜態(tài)獲取方法*/public static MyApplication getInstance() {return mApplication;} }

這樣調(diào)用getInstance()方法, 就可以在任何類中調(diào)用Application了, 并能取到該類中的數(shù)據(jù);


(2)在AndroidManifest.xml中注冊(cè)自定義的Application


<manifest xmlns:android="http://schemas.android.com/apk/res/android" ><applicationandroid:name=".MyApplication" ></application></manifest>


(3)獲取Application方法

調(diào)用系統(tǒng)方法?: 在Activity中可以調(diào)用getApplicationContext()方法獲取Application;

通過自定義的方法?: 如我們上面自定義的Application那樣, 可以調(diào)用自定義的getInstance()方法獲取Application實(shí)例對(duì)象;



二. ListView中元素的排序

ListView中的元素排序, 即將數(shù)據(jù)源排序即可;

給集合排序的方法 : 調(diào)用Collections的sort(list, Comparator)方法, 該方法需要2個(gè)參數(shù), 第一個(gè)參數(shù)就是需要排序的集合, 第二個(gè)參數(shù)是比較器;

這里的比較器需要?jiǎng)?chuàng)建, 并且重寫其中的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; } });

這樣就會(huì)將mList集合自動(dòng)進(jìn)行排序;



三. 格式化浮點(diǎn)數(shù)


如何在程序中保留一個(gè)float或者double浮點(diǎn)數(shù)的小數(shù)位數(shù) , 這里可以使用以下幾種方法 :?

1.使用DecimalFormat格式化浮點(diǎn)數(shù)

首先創(chuàng)建一個(gè)DecimalFormat類 , ?這個(gè)類可以按照一定的格式化數(shù)字來格式化浮點(diǎn)數(shù). 常見的格式化字符是"#" , "0" . 創(chuàng)建該類的時(shí)候 , 將格式化的格式傳入 , 例如如果要保存兩位小數(shù) , 就傳入 "#.00" , 以此類推 .? 創(chuàng)建了DecimalFormat對(duì)象之后 , 調(diào)用該對(duì)象的format對(duì)象 , 將需要格式化的浮點(diǎn)數(shù)傳入這個(gè)方法 , 返回的結(jié)果就是格式化之后的固定位數(shù)的浮點(diǎn)數(shù)的字符串形式 .? 注意 : 這種方法格式化之后的浮點(diǎn)數(shù)對(duì)象是字符串形式 , 如果之后需要使用這個(gè)浮點(diǎn)數(shù)進(jìn)行計(jì)算 , 就需要使用BigDecimal進(jìn)行實(shí)現(xiàn)的這種方法 ;? 精確度問題?: DecimalFormat使用的是half-even舍入法, 這個(gè)不是四舍五入方法, 當(dāng)出現(xiàn)5的時(shí)候,就會(huì)向最近的偶數(shù)靠近.

例如 : System.out.println(new java.text.DecimalFormat("0.00").format(3.135)); 5最近的偶數(shù)是4, 打印的結(jié)果就是3.14;

System.out.println(new java.text.DecimalFormat("0.00").format(3.125));?5最近的偶數(shù)是2, 打印的結(jié)果就是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實(shí)現(xiàn)

創(chuàng)建一個(gè)BigDecimal對(duì)象 , 創(chuàng)建的時(shí)候 , 傳入需要格式化的浮點(diǎn)數(shù) , new BigDecimal(float) ;? 調(diào)用這個(gè)BigDecimal的setScale方法 , 這個(gè)方法傳入的參數(shù) : 需要保留的小數(shù)位數(shù) , BigDecimal.ROUND_HALF_UP常量 , 之后調(diào)用這個(gè)常量對(duì)應(yīng)的將BigDecimal轉(zhuǎn)為浮點(diǎn)數(shù)的方法 , 得到的結(jié)果為轉(zhuǎn)化好的浮點(diǎn)數(shù) .? float pi = 3.1415926f;BigDecimal bigDecimal = new BigDecimal(pi);float result = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();System.out.println(result);
與浮點(diǎn)數(shù)有關(guān)的構(gòu)造方法?: 可以向構(gòu)造方法中傳入浮點(diǎn)數(shù) 或者 字符串 , 這里需要注意的是 , 使用浮點(diǎn)數(shù)的構(gòu)造方法不精確 , 這個(gè)值最后會(huì)有不該有的數(shù)據(jù),盡量使用字符串的構(gòu)造方法. 例如 :? BigDecimal bd1=new BigDecimal(0.05);System.out.println(bd1.toString());BigDecimal bd2=new BigDecimal("0.05");System.out.println(bd2.toString());結(jié)果是 :  0.050000000000000002775557561562891351059079170227050781250.05因此使用字符串的構(gòu)造函數(shù),獲得的數(shù)據(jù)更精確.
BigDecimal的基本方法 : 加法 add , 減法 subtract, 乘法 multiply, divide 除法, setScale四舍五入.
加法計(jì)算 :? BigDecimal bd3=new BigDecimal(String.valueOf(0.05));BigDecimal bd4=new BigDecimal(String.valueOf(0.01));System.out.println((bd3.add(bd4)).doubleValue());
減法計(jì)算 :? BigDecimal bd5=new BigDecimal(String.valueOf(0.05));BigDecimal bd6=new BigDecimal(String.valueOf(0.01));System.out.println((bd5.subtract(bd6)).doubleValue());
乘法計(jì)算 :? BigDecimal bd7=new BigDecimal(String.valueOf(0.05));BigDecimal bd8=new BigDecimal(String.valueOf(0.01));System.out.println((bd7.multiply(bd8)).doubleValue());
除法計(jì)算 :? //這里沒有考慮數(shù)據(jù)錯(cuò)誤的可能情況//定義了精確位數(shù)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?: 向負(fù)無窮方向射舍入
  • ROUND_HALF_DOWN?: 向距離近的一方舍入 , 如果兩邊相等 , 向下舍入 , 例如 2.155 , 保留2位小數(shù)的話 是 2.15;
  • ROUND_HALF_UP??: 向距離近的一方舍入 , 如果兩邊相等 , 向上舍入 , 例如 2.155,保留兩位小數(shù)的話 是 2.16; ?這個(gè)就是四舍五入
  • ?ROUND_HALF_EVEN?: 向距離近的一方舍入 , 如果兩邊距離相等 , 如果保留位是奇數(shù)位 使用ROUND_HALF_UP?, 如果保留位是偶數(shù)位,使用ROUND_HALF_DOWN;
  • ???ROUND_UNNECESSARY?: 精確的計(jì)算 , 不需要舍入 .?
  • ROUND_UP??: 向遠(yuǎn)離0的方向舍入.

四. string.xml占位符




開發(fā)中經(jīng)常遇到這樣的情況 , 在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標(biāo)簽

http://blog.csdn.net/hustpzb/article/details/6870817

http://blog.csdn.net/ganggang1st/article/details/6804086

五. 動(dòng)態(tài)引用圖片



在資源文件中存放有 image_1.png,?image_2.png,?image_3.png 三張圖片 , ?根據(jù)傳入?yún)?shù)動(dòng)態(tài)引用對(duì)應(yīng)的圖片 , 有三個(gè)解決方法

根據(jù)R.drawable.xx動(dòng)態(tài)引用是錯(cuò)誤的 , 因?yàn)槊總€(gè)這種id都對(duì)應(yīng)著R文件中的一個(gè)id,如果沒有相對(duì)應(yīng)的id , 編譯不會(huì)通過;

建立一個(gè)工程,包名為com.yun.demo

方案一 : 圖片放在drawable目錄下的情況

Resources resources = this.getResources(); int imageIndentify = resources.getIdentifier(imageName, "drawable","chao.yun.demo");使用上面的代碼可以通過字符串拼接圖片名稱 , 根據(jù)傳入的參數(shù) , 拼接imageName字符串 , 從而動(dòng)態(tài)的獲取圖片對(duì)應(yīng)的id;resources.getIdentifier(imageName, "drawable","chao.yun.demo");這個(gè)方法返回的是圖片對(duì)應(yīng)的id ;第一個(gè)參數(shù)是圖片的名稱 , 如果沒有找到 , 返回0 ;第二個(gè)參數(shù)是默認(rèn)的資源類型 , 如果找的是圖片 , 就是 "drawable" , 這個(gè)不是具體的目錄 , 因此不用注明"drawable-hdpi"第三個(gè)參數(shù)是包名 , 這個(gè)包名是創(chuàng)建工程時(shí)候的包名 , 是總的包名 , 與manifest配置文件中的包名相同;
詳細(xì)代碼 :layout中的代碼 :
<?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 {@Overridepublic 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 自帶圖標(biāo)庫


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:

Java Usage example:myMenuItem.setIcon(android.R.drawable.ic_menu_save); Resource Usage example: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

轉(zhuǎn)載

http://docs.since2006.com/android/2.1-drawables.php


七 ?Android?不顯示標(biāo)題欄和全屏的設(shè)置方法


1.在Manifest.xml中設(shè)置

不顯示標(biāo)題欄
android:theme="@android:style/Theme.NoTitleBar"
全屏
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

2.在代碼中實(shí)現(xiàn)
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);



《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的【Android 应用开发】Android开发技巧--Application, ListView排列,格式化浮点数,string.xml占位符,动态引用图片的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。