Android UI component (2)-Windows-like dialog box

Source: Internet
Author: User

Android UI component (2)-Windows-like dialog box

Android UI component (2)-Windows-like dialog box


Before starting this chapter, I would like to wish you a happy Mid-Autumn Festival. I believe that many of my friends at work will be given three days off!

If you have time, go home and accompany your parents! The tree is quiet, and the child is not waiting for it! Lucky!

Now let's talk about the truth and the plain language. Today we have prepared a dialog box that imitates the Windows style!


:

I believe that most AlertDialog statements look like the following:



The following dialog box is displayed:



Compare the two types of dialog boxes. From the user's perspective, I believe you prefer the second type of dialog box with clear colors.

Now let's begin to explain how to create a dialog box that imitates the windows style!




The logical process of the Code:

In fact, it is just the layout of the custom dialog box, loading and processing corresponding events!




Core code analysis:

① Button clicking effect: btnexit_selctor.xml

 
     
  
  
 



② Layout file of the dialog box: dialog_win.xml

 
     
          
           
               
             
          
           
       
           
               
              
               
      
 



③ MainActivity. java

Package com. jay. example. windowsdialogdemo; import android. app. activity; import android. app. alertDialog; import android. app. alertDialog. builder; import android. content. context; import android. content. intent; import android.net. uri; import android. OS. bundle; import android. view. layoutInflater; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. imageButton; import android. widget. toast; public class MainActivity extends Activity {private Button btnshow; private View dialogView; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); btnshow = (Button) findViewById (R. id. btnshow); btnshow. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {Builder builder = myBuilder (MainActivity. this); final AlertDialog dialog = builder. show (); // click the outside of the screen. The dialog box does not disappear. If it is not set, the dialog box that is clicked outside disappears. setCanceledOnTouchOutside (false); // click the three buttons in the listener dialog box: // ① close the dialog box: ImageButton imgcancle = (ImageButton) dialogView. findViewById (R. id. btncancle); imgcancle. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {dialog. dismiss () ;}}); // ① Button linked to the blog: Button btnblob = (Button) dialogView. findViewById (R. id. btnblog); btnblob. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {Toast. makeText (getApplicationContext (), "Access blog", Toast. LENGTH_SHORT ). show (); Uri uri = Uri. parse ("http://blog.csdn.net/coder_pig"); Intent intent = new Intent (Intent. ACTION_VIEW, uri); startActivity (intent); dialog. dismiss () ;}}); // ③ click the exit Button: Button btnclose = (Button) dialogView. findViewById (R. id. btnclose); btnclose. setOnClickListener (new OnClickListener () {public void onClick (View v) {dialog. dismiss () ;}}) ;}// defines a method for returning the Builder object. In fact, this is the method for setting the view content in the dialog box. private Builder myBuilder (Context context) {final LayoutInflater inflater = MainActivity. this. getLayoutInflater (); AlertDialog. builder builder = new AlertDialog. builder (context); dialogView = inflater. inflate (R. layout. dialog_win, null); return builder. setView (dialogView );}});}}



Download instance code:

Http://pan.baidu.com/s/1jGone5o















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.