Android development dialog box advanced application, android development dialog box

Source: Internet
Author: User
Tags float number

Android development dialog box advanced application, android development dialog box

Android development dialog box advanced application

 

It is easy to create and display a dialog box, but if you want to perform more advanced operations, you need some skills. The following are some of the advanced tips used in the dialog box.


1. Change the display position of the dialog box:

In most cases, the dialog box is displayed in the middle of the screen (may vary depending on the system version ). In fact, the dialog box can be displayed on the left, center, right, top, bottom, or even any position of the screen as needed.

To obtain the display position of the dialog box, you need to obtain the Window object of the dialog box and control the display position of the dialog box through some methods of the Window object.

For example:

AlertDialog alertDialog = new AlertDialog. builder (this ). setMessage ("display the dialog box at the top of the screen "). setPositiveButton ("OK", null ). create (); Window window = alertDialog. getWindow ();/* call the setGravity method to display the dialog box at the top of the screen. */Window. setGravity (Gravity. TOP); alertDialog. show ();

Tip: the parameters of the setGravity method also support the following values:

Gravity. LEFT (LEFT), Gravity. RIGHT (RIGHT), Gravity. BOTTOM (BOTTOM), Gravity. LEFT | Gravity. TOP (top left), etc.

Run:



2. display the dialog box at any location:

The Window object in the dialog box has been obtained above. We can use the getAttributes () method of this object to obtain WindowManager. layoutParams object and use WindowManager. windowManager of the LayoutParams object. layoutParams. x and WindowManager. layoutParams. y attribute to set the position of the dialog box on the screen.

AlertDialog alertDialog = new AlertDialog. builder (this ). setMessage ("display the dialog box at any location "). setPositiveButton ("OK", null ). create (); Window window = alertDialog. getWindow (); WindowManager. layoutParams wl = window. getAttributes (); // x, y is the offset of the dialog box relative to the center of the screen // sets the horizontal offset wl. x =-50; // sets the vertical offset wl. y = 100; window. setAttributes (wl); alertDialog. show ();

Tip: WindowManager. LayoutParams. x and WindowManager. LayoutParams. y are used to set the offset of the dialog box relative to the center position of the screen, rather than the absolute position of the screen.

Run:



3. Insert the image degree in the text and buttons of the dialog box:

I have introduced image insertion in TextView IN THE TextView advanced application developed by Android. This technology also applies in the dialog box. The code for inserting an image in the dialog box is as follows:

/*** Insert the image degree in the text and button of the dialog box. **/protected void insertImg () {// TODO Auto-generated method stub AlertDialog alertDialog = new AlertDialog. builder (this ). setMessage (getHtml ()). setPositiveButton (getHtml (), null ). create (); alertDialog. show () ;}// return a Spanned object private Spanned getHtml () {// TODO Auto-generated method stub Spanned spanned = Html. fromHtml ("Haha,  hello. ", new ImageGetter () {@ Override public Drawable getDrawable (String source) {// TODO Auto-generated method stub Drawable drawable = getResources (). getDrawable (R. drawable. ic_launcher); // sets the image size to drawable. setBounds (, 30, 30); return drawable ;}}, null); return spanned ;}

Run:



4. Change the transparency of the dialog box.

You can use WindowManager. LayoutParams. alpha to set the transparency of the dialog box. The value of Alpha ranges from 0.0f to 1.0f (f indicates a float number ). 0.0f indicates that it is completely transparent (the dialog box will be invisible at this time), and 1.0f indicates that it is completely opaque. 1.0f is also the default alpha value. The following code shows a dialog box with an Alpha value of 0.6f.

/*** Change the transparency of the dialog box **/protected void setAlpha () {// TODO Auto-generated method stub AlertDialog alertDialog = new AlertDialog. builder (this ). setMessage ("this is a dialog box with the transparency of 60%! "). SetPositiveButton ("OK", null ). create (); Window window = alertDialog. getWindow (); window. setGravity (Gravity. TOP); WindowManager. layoutParams wl = window. getAttributes (); // The alpha range is 0.0f-1.0f wl. alpha = 0.6f; window. setAttributes (wl); alertDialog. show ();}

Run:



Android Application development problems, for the text box in the dialog box

EditText = (EditText) this. findViewById (R. string. phone );

Hello, first of all, there is a problem with your code. You have come out of a new editText before. Why is there findViewById? Besides, findViewById is not R. string, it should also be R. id. First, you have not sorted out your ideas.

How can I add a jump event in the Android List dialog box for Android front-end development?

This is too simple. You didn't pay attention to the Listener. I added it for you. Let's try it.
AlertDialog. Builder my_renren = new AlertDialog. Builder (this );
My_renren.setTitle ("Renren username ");
My_renren.setIcon (R. drawable. ic_launcher );
My_renren.setItems (new String [] {"friend", "status", "share"}, new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
Switch (which ){
Case 0: showToast ("friend... click success! ");
Break;
Case 1: showToast ("status... click success! ");
Break;
Case 2: showToast ("share... click" success! ");
Break;
}
}
});

My_renren.setNegativeButton ("rollback", new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
Dialog. dismiss ();
}
});
AlertDialog dlg = my_renren.create ();
Dlg. show ();
}
Private void showToast (String text ){
Toast. makeText (this, text, Toast. LENGTH_SHORT). 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.