How to use dialog boxes in Android and how to clear resources on bitmaps

Source: Internet
Author: User

We know that one of the things that we often need to use when writing Java code is the dialog box (Dialog), for example, we write the program, others in use, we need to give users some hints, and these tips if always in the form of the window to play out inevitably more trouble, but also increased the burden on the programmer, But a lot of people don't know much about the pop-up and use of Android dialogs, here's an example:

Alertdialog.builder dialog=new Alertdialog.builder (GetContext ());

Dialog.settitle ("White won");

Dialog.setmessage ("Whether to start a new bureau");

Dialog.setpositivebutton ("OK", surelistener);

Dialog.setnegativebutton ("cancellation", Cancellistener);

Dialog.show ();

This is a way to pop up a dialog when writing an Android Gobang, first use the Alertdialog.builder to create a dialog object, followed by new Alertdialog.builder ();

What should be written in parentheses is the object (context) of the current class, which means that if we want to pop a dialog box in the current activity, we can write this here, getcontext because it's written in a MyView class that I wrote, MyView inherits the method of GetContext in ImageView.

Then in dialog also provides a number of methods, such as the set title, information (message), there are two buttons, one is Positivebutton, one is Netgativebutton, which needs to enter two values, the front one is the button above the text , followed by a button listener, note that the button listener here is included in the Android.content.DialogInterface package.

Flexible use of the dialog box not only makes the code more concise, but also enhance the user's experience, and then there is a knowledge point, is how to clear the bitmap resources, there are two methods, one is the resources above our bitmap to Transparent, Map.erasecolor ( Color.transparent);//map is the bitmap object we created,

Of course, in this case, the resources on the bitmap must still be, but we can not see, if you want to completely clear the resources on the bitmap, we should use the following method, p is the brush object we created Paint,c is the canvas object we created, as follows:

P.setxfermode (New Porterduffxfermode (PorterDuff.Mode.CLEAR));

Rect Rect = new Rect (0, 0, 320, 320);

The following two values are the same size as the bitmap object we created

P.setstyle (Paint.Style.FILL);

C.drawrect (rect, p);

P.setstyle (Paint.Style.FILL_AND_STROKE);

P.setxfermode (NULL);

However, there are some advantages to the above method of setting transparency, one is that the code is more concise, the second is that in some cases, we can not complete the second method, for example, we are to the other activity in the method of clearing out the bitmap resources here, the second method will certainly have some need to consider the place, For example, the brush (Paint) and canvas (Canvas) must be public, and this method obviously only needs bitmap object is public. This avoids having both the brush and the canvas object in several different classes, but sometimes it needs to be shared and sometimes separated.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/

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.