Android provides two common message [pop-up box and Toast Alert]

Android provides two common message pop-up box, Toast and Alert.

Toast

Toast is a brief message box does not require user interaction, nor will the focus shift over, it is possible for most of the scene, the information presented to the user. In the previous study, it has been used many times to the Toast.

Create a Toast very simple, use the static method makeText (Context context, CharSequence text | int resId, int DURATION) , the String (or a String ID), and the time display of the length (LENGTH_SHORT or LENGTH_LONG) you can get a Toast object .

The above is the most common way, but if you want to display other View, rather than text, the constructors of Toast Toast (Context context), view and then use the setView () settings displayed by setDuration () provided time can be displayed.

If we need to display Toast, just call the show () method can be. E.g:

private void insertEmployee()

{

   // Call dialog box is displayed Toast

    Toast.makeText(TestMsg.this, "haha,Insert Employee Successfully!", Toast.LENGTH_LONG).show();

};

Alert

The traditional way is dialog box of the form, requires the use of AlertDialog, when an alert box pops up, will receive focus, it must be closed by the user, for an important event or error is displayed, you must ensure that the user knows the scene or some validation Information.

The easiest alert box created by AlertDialog.Builder class, you can step setMessage () to set the display text content, setTitle () , setIcon () , and set the button below setPositiveButton () , setNeutralButton () , setNegativeButton () , which button and a few specific name has little to do, only that in turn positions with the left, right, and display up to three buttons, we need to set up to deal with these buttons display content and click the trigger. The last call to show () method to display it.

If we need to create the builder object, and then configure, and finally display, you can use the Create () , and then set the foregoing, the last call show (). Once the show () call, and so has the user's process.

Here is an example of an alert box is displayed, we turn to set the alarm for the contents of the box:

private void AlertDialog()
{
    //Alert Dialog
    new AlertDialog.Builder(TestMsg.this)
    .setTitle("Alerting Message")
    .setMessage("Ha Ha!")
    .setNegativeButton("Close", new DialogInterface.OnClickListener() {   
        @Override
        public void onClick(DialogInterface dialog, int which) {
            //do nothing - it will close on its own
        }
     })
   .show();
    
};







Reproduced in: https: //www.cnblogs.com/kevinGao/archive/2012/01/11/2323347.html

Guess you like

Origin blog.csdn.net/weixin_33674976/article/details/93052827