Android learning summary 3

Source: Internet
Author: User

1 is at 1. In Android 5, you can specify that only numbers are input in edittext. For example, a keyboard is displayed:
Android: inputtype = "numberdecimal", you can also enter a number containing the decimal point.

2. Read the content in the resource file
Result. settext (gettext (R. String. bmi_result ));
Use the gettext function to read the content in the resource file.

3. Create a dialog box like "About Us:
New alertdialog. Builder (BMI. This)
. Settitle (R. String. about_title)
. Setmessage (R. String. about_msg)
. Setpositivebutton ("OK ",
New dialoginterface. onclicklistener (){
Public void onclick (
Dialoginterface, int I ){
}
})
. Show ();

4 toast component: displays a short prompt message, which disappears after a few seconds:
Toast. maketext (BMI. This, "About Us", Toast. length_short). Show ();
5. Open the URL
New alertdialog. Builder (BMI. This)
. Setnegativebutton (R. String. homepage_label,
New dialoginterface. onclicklistener (){
Public void onclick (
Dialoginterface, int I ){
// Go to URL
Uri uri = URI. parse ("http://sites.google.com/site/gasodroid ");
Intent intent = new intent (intent. action_view, Uri );
Startactivity (intent );
}
})
. Show ();
You can also write the URI to the resource file,
Uri uri = URI. parase (getstring (R. String. homepage_uri ));

6 menu
Public Boolean oncreateoptionsmenu (menu ){
// Todo auto-generated method stub
Menu. Add (0, menu_about, 0, "about"). seticon (R. drawable. help_browser );
Menu. Add (0, menu_quit, 0, "end"). seticon (R. drawable. emblem_unreadable );
Return super. oncreateoptionsmenu (menu );
}
Process menu clicking
Public Boolean onoptionsitemselected (menuitem item ){
// Todo auto-generated method stub
Switch (item. getitemid ()){
Case menu_about:
Openoptionsdialog ();
Break;
Case menu_quit:
Finish ();
Break;
}
Return super. onoptionsitemselected (item );
}
7 <activity Android: Name = ". BMI"
Android: Label = "@ string/app_name">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. Main"/>
<Category Android: Name = "android. Intent. Category. launcher"/>
</Intent-filter>,
The starting class is BMI. <intent-filter> describes this Program The nature
<Action Android: Name = "android. Intent. Action. Main"/> it indicates that this BMI is the starting point of the program,
<Category Android: Name = "android. Intent. Category. launcher"/> indicates that this is displayed in the lanucher list of the program.

8 data transmission between intent
Intent intent = new intent ();
Intent. setclass (BMI. This, report. Class );
Bundle bundle = new bundle ();
Bundle. putstring ("key_height", field_height.gettext (). tostring ());
Bundle. putstring ("key_weight", field_weight.gettext (). tostring ());
Intent. putextras (bundle );
Startactivity (intent );
Receiving information:
Bundle bunde = This. getintent (). getextras ();
Double Height = double. parsedouble (bunde. getstring ("key_height")/100;
9 use status Blocking Information
Import Android. App. notification;
Import Android. App. icationicationmanager;
Import Android. App. pendingintent;

Icationicationmanager barmanager = (notificationmanager) getsystemservice (notification_service );

Notification barmsg = new notification (
R. drawable. icon_128,
"Hi ",
System. currenttimemillis ()
);

Barmsg. defaults | = notification. default_sound;
// Barmsg. defaults | = notification. default_all;

Pendingintent contentintent = pendingintent. getactivity (
This,
0,
New intent (this, BMI. Class ),
Pendingintent. flag_update_current );

Barmsg. setlatesteventinfo (
Report. This,
"Hi ",
"OK ",
Contentintent );

Barmanager. Y (0, barmsg );

icationicationmanager barmanager = (notificationmanager) getsystemservice (icationication_service);
declare the manager,
notification barmsg = new notification (
r. drawable. icon_128,
"hi",
system. currenttimemillis ()
);
declares the reminder information, system. currenttimemillis () indicates immediate display;
barmsg. setlatesteventinfo (
report. this,
"hi",
"OK",
contentintent);
here is the details of adding the status bar, here, "Hi" is the display topic and the third parameter. Here 'OK' is the description.
contentintent indicates the intent that is opened when the status bar is clicked.

10 save priority settings:
private void restoreprefs () {
sharedpreferences settings = getsharedpreferences (Pref, 0);
string pref_height = settings. getstring (pref_height, "");

}< br> check whether the system uses the "bmi_pref" string as the file name priority setting. If yes, use settings as the code.
when the user leaves the activity, save the value and rewrite the onpause () function:
super. onpause ();
// save user preferences. use editor object to make changes.
sharedpreferences settings = getsharedpreferences (Pref, 0);
Settings. edit ()
. putstring (pref_height, field_height.gettext (). tostring ()
. commit ();

11 multi-language interface coexistence:
for example, if the original file is in English, values is stored in the res directory. If it is in Chinese, put another values-ZH-RTW in the res directory.
ZH is the subject system, and-R is required, followed by branch
resources res = getresources ();
Configuration conf = res. getconfiguration ();
Conf. locale = locale. traditional_chinese;
displaymetrics dm = res. getdisplaymetrics ();
res. updateconfiguration (Conf, DM);
the mandatory Chinese interface is used here. In the program, different interfaces are provided for different languages:
If (Conf. locale = Local. traditional_chinese)
12 select an event from the spinner drop-down menu:

field_feet.setonitemselectedlistener (getfeet);
private spinner. onitemselectedlistener getfeet = new Spinner. onitemselectedlistener () {
Public void onitemselected (adapterview parent, view V, int position, long ID) {
......
}< br> Public void onnothingselected (adapterview parent) {
}< BR >};

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.