Basic Components of Android applications (1)

Source: Internet
Author: User

General page:

 


Note: to change the method between buttons, you must: android: onClick = "clearNoti" to add the corresponding method.

1. When you click the status bar button:

Public void Notification (View v ){

ShowNotification ("sent SMS", "5557", "I Like You", R. drawable. ic_launcher, R. drawable. ic_launcher );
}
Public void showNotification (String tickerText, String contentTitle, String contentText, int iconId, int notiId ){
Icationicationmanager = (notificationManager) getSystemService (NOTIFICATION_SERVICE );
// Create a Notification
Notification notification = new Notification ();
// Set the content of the sent message
Notification. icon = iconId;
// Set the sent message
Notification. tickerText = tickerText;
// Set the notification time
Notification. when = System. currentTimeMillis ();
// Set the default voice or vibration, Light effect, and
Notification. defaults = Notification. DEFAULT_VIBRATE; // vibration effect
// Step 3: The PendingIntent Android system is responsible for maintenance
PendingIntent pendingIntent = PendingIntent. getActivity (this, 0, getIntent (), 0 );
// Notification notification = new Notification (R. drawable. ic_launcher, "new Message available", System. currentTimeMillis ());
// Step 4; set more detailed information
Notification. setLatestEventInfo (this, contentTitle, contentText, pendingIntent );
// Step 5: Use the icationicationmanager object's notify method to display the Notification message. The Notification ID must be specified.
Icationicationmanager. notify (notiId, notification );


}:

2. When you click the clear status button:

// Clear the operation
Public void clearNoti (View v ){

 


Icationicationmanager. cancelAll (); // clear all
}

 

3. When you click Create a pair DiaLog box: public void DiaLog (){
AlertDialog alertDialog = new AlertDialog. Builder (this)
. SetIcon (R. drawable. mw)
. SetTitle ("DiaLog"). setMessage ("whether to create a file ")
. SetPositiveButton ("OK", new DialogInterface. OnClickListener (){

@ Override
Public void onClick (DialogInterface dialog, int which ){
 
New AlertDialog. Builder (MainActivity. this). setMessage ("You have clicked the confirm button and the file has been created"). show ();
}
})
. SetNegativeButton ("cancel", new DialogInterface. OnClickListener (){

@ Override
Public void onClick (DialogInterface dialog, int which ){
New AlertDialog. Builder (MainActivity. this)
. SetMessage ("you have selected the cancel button, this file will not be created"). create ()
. Show ();


}


}). Show ();
}

 



4. Click the simple List dialog box

Public void Simplelistdialog (View v ){
Final String items [] = {"java", "php", "3g", ". net "};
AlertDialog alertDialog = new AlertDialog. Builder (this)
. SetTitle ("simple List dialog box"). setItems (items, new DialogInterface. OnClickListener (){

@ Override
Public void onClick (DialogInterface dialog, int which ){
Toast. makeText (getApplicationContext (), "dear, your favorite subjects are:" + items [which], Toast. LENGTH_LONG). show ();

}
}). Show ();

}

5. Click the radio List dialog box.

Public void Radiolistdialog (View v ){
Final String items [] = {"java", "php", "3g", ". net "};
AlertDialog alertDialog = new AlertDialog. Builder (this). setTitle (" ")
//. SetSingleChoiceItems (items, checkedItem, listener)
//. SetSingleChoiceItems (itemsId, checkedItem, listener)
//. SetSingleChoiceItems (cursor, checkedItem, labelColumn, listener) labelColumn if the data source is a dataset
// A column in the dataset is loaded as a list box in the List dialog box. This parameter indicates the column name (field name)

. SetSingleChoiceItems (items, 1, new DialogInterface. OnClickListener (){

@ Override
Public void onClick (DialogInterface dialog, int which ){
Toast. makeText (getApplicationContext (), "dear, your favorite subjects are:" + items [which], Toast. LENGTH_LONG). show ();

}
}). Show ();
}

 
 

 

7. Click the multiple choice List dialog box.


Public void Multiselectlistdialog (View v ){
Final String items [] = {"Java", "PHP", "3G", ". NET "};

New AlertDialog. Builder (this). setTitle ("Multi-choice List dialog box ")
//. SetMultiChoiceItems (itemsId, checkedItems, listener)
//. SetMultiChoiceItems (cursor, isCheckedColumn, labelColumn, listener)
. SetMultiChoiceItems (items, new boolean [] {false, true, true, false}, new DialogInterface. OnMultiChoiceClickListener (){

@ Override
Public void onClick (DialogInterface dialog, int which, boolean isChecked ){

If (isChecked ){
Toast. makeText (getApplicationContext (), "dear, the subjects you selected are:" + items [which], Toast. LENGTH_LONG). show ();
}

}
}). SetPositiveButton ("OK", new DialogInterface. OnClickListener (){

@ Override
Public void onClick (DialogInterface dialog, int which ){
Toast. makeText (getApplicationContext (), "dear, your favorite subjects include:", Toast. LENGTH_LONG). show ();
}
}). Show ();
}



8. Click the progress bar


Public void ProgressDialog (View v ){
Handler = new Handler (){
@ Override
Public void handleMessage (Message msg ){

Super. handleMessage (msg );
Switch (msg. what ){
Case PRO:
If (PRO> = MAX_PROGRESS ){
// Reset
Progress = 0;
ProgressDialog. dismiss (); // destruction dialog box
 
} Else {
Progress ++;
ProgressDialog. incrementProgressBy (1 );
// Delayed message sending
Handler. sendEmptyMessageDelayed (PRO, 100 );
}
Break;
Default:
Break;
}
}
};
ProgressDialog = new ProgressDialog (this );
ProgressDialog. setIcon (R. drawable. mw );
ProgressDialog. setTitle ("processing data ......");
ProgressDialog. setMessage ("Please wait .....");
ProgressDialog. setProgressStyle (ProgressDialog. STYLE_SPINNER); // set the progress bar dialog box (horizontal, spin)
// Set the maximum value
ProgressDialog. setMax (MAX_PROGRESS );
ProgressDialog. setButton ("pause", new DialogInterface. OnClickListener (){

@ Override
Public void onClick (DialogInterface dialog, int which ){
Handler. removeMessages (PRO );

}
});
ProgressDialog. setButton2 ("cancel", new DialogInterface. OnClickListener (){


@ Override
Public void onClick (DialogInterface dialog, int which ){


// Delete A Message Queue
Handler. removeMessages (PRO );
// Initial recovery progress Value
Progress = 0;
ProgressDialog. setProgress (progress );
}
});
// Display
ProgressDialog. show ();
// It must be set after show. A bug may occur before show.
Progress = (progress> 0 )? Progress: 0;
ProgressDialog. setProgress (progress );

// Thread
Handler. sendEmptyMessage (PRO );
}


Another effect is:


 

9. Click the custom table dialog box.


Public void MyDialog (View v ){

LayoutInflater layoutInflater = getLayoutInflater ();
View view = layoutInflater. inflate (R. layout. activity_main, null); // R. layout. activty_main custom layout file can be customized by yourself.
New AlertDialog. Builder (this). setView (view). setTitle ("Custom dialog box"). setPositiveButton ("confirm button", new DialogInterface. OnClickListener (){

@ Override
Public void onClick (DialogInterface dialog, int which ){
// Process

}
}). Show ();



}

 

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.