Allows you to dynamically switch the background of a widget in Android.
This is also used in yesterday's study. Let's summarize the ideas, because this knowledge point will definitely be used again in the future.
Purpose: To dynamically select the component background, system skin, and custom toast background in the software.
Implementation idea: to use the SharedPrefence function in Android, write a control in the settings, set a click listener, and display an Alert pop-up window for you to select, in this pop-up window, set a click listener (onItemListener). When you click a specific listener, save the corresponding click id to sahredprefence. In this way, in other cases, you can obtain the selected value from the settings for dynamic personalization.
Code:
Set the selected operation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
Scv_setAddressBg.setOnClickListener (new OnClickListener (){ @ Override Public void onClick (View v ){ Int which = sp. getInt ("which", 0 ); Final String [] items = {"Translucent", "active orange", "Wei Shi blue", "metallic gray", "apple green "}; AlertDialog. Builder builder = new Builder (SettingActivity. this ); Builder. setTitle ("set the "); Builder. setSingleChoiceItems (items, which, new DialogInterface. OnClickListener (){ @ Override Public void onClick (DialogInterface dialog, int which ){ Editor edit = sp. edit (); Edit. putInt ("which", which ); Edit. commit (); Scv_setAddressBg.setDesc (items [which]); Dialog. dismiss (); } }); Builder. setNegativeButton ("cancel", null ); Builder. show (); } }); |
Display custom toast operations:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
Public void showMyToast (String address ){ <Span style = "color: # ff6600;"> int [] ids = {R. drawable. call_locate_white, R. drawable. call_locate_orange, R. drawable. call_locate_blue , R. drawable. call_locate_gray, R. drawable. call_locate_green}; </span> <Span style = "color: # ff6600;"> SharedPreferences sp = getSharedPreferences ("config", MODE_PRIVATE ); Int which = sp. getInt ("which", 1); </span> View = View. inflate (this, R. layout. address_show, null ); TextView textView = (TextView) view. findViewById (R. id. TV _address ); TextView. setText (address ); <Span style = "color: # ff6600;"> view. setBackgroundResource (ids [which]); </span> WindowManager. LayoutParams params = new WindowManager. LayoutParams (); Params. height = WindowManager. LayoutParams. WRAP_CONTENT; Params. width = WindowManager. LayoutParams. WRAP_CONTENT; Params. flags = WindowManager. LayoutParams. FLAG_NOT_FOCUSABLE | WindowManager. LayoutParams. FLAG_NOT_TOUCHABLE | WindowManager. LayoutParams. FLAG_KEEP_SCREEN_ON; Params. format = PixelFormat. TRANSLUCENT; Params. type = WindowManager. LayoutParams. TYPE_TOAST; Wm. addView (view, params ); } |
Summary:
1. Pay attention to the Array application. ids [value] is a good method and idea to write image resource files in an ids array.
2. Be careful. When you get the sp, the name is wrong. config is written as congig.
3. Understanding debugging skills depends on experience and logical reasoning.