The program described in this paper features a dynamic selection of component backgrounds, system skins, custom toast backgrounds, etc. in software.
In order to achieve this requirement, we need to use the Sharedprefence function of Android, first write a control in the setup, set up a click Listener, click on the display of an alert selection window, let you choose, To this window again set a click on the Listener (Onitemlistener), click to a specific time, the corresponding click ID to save the sahredprefence inside, so that other places can be obtained from the settings in the selected values, dynamic personalization processing.
The specific code is as follows:
1. Set the selected action:
Scv_setaddressbg.setonclicklistener (New Onclicklistener () {
@Override public
void OnClick (View v) {
int which = Sp.getint ("which", 0);
Final string[] items = {"Translucent", "Vigor orange", "Guardian Blue", "Metal Ash", "Apple Green"};
Alertdialog.builder Builder = new Builder (settingactivity.this);
Builder.settitle ("setting attribution to show Background");
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 ();
}
);
2. Display Custom toast Action:
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.drawabl e.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.L
Ayoutparams ();
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);
}
3. The summary concludes as follows:
(1) To pay attention to the application of the array, ids[value] this way to use it, it is a good way and idea to write a picture resource file in an IDs array.
(2) Careful, to get the SP when the name was wrong, Config wrote configs.
(3) It is important to understand that debugging skills are based on experience and logical reasoning.