Create a custom Dialog dialog box for example sharing in Android _android

Source: Internet
Author: User
Tags event listener stub

Custom Dialog Base version
Most of the time, when we use the Android SDK to provide the Alerdialog, will have different effect because of your system different, just like you brush is MIUI system, the pop-up box will display at the top! Here is a simple introduction to the application of the custom pop-up box.

First create the Layout file dialog:

Code:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
  android:layout_width=" match_parent "
  android:layout_height=" match_parent "
  android:o" rientation= "vertical" >
  <edittext 
    android:id= "@+id/edit"
    android:layout_width= "250dip"
     android:layout_height= "Wrap_content"
    />
  <button 
    android:id= "@+id/clickbtn"
     android: Layout_width= "Wrap_content"
     android:layout_height= "wrap_content"
     android:text= "click me"/>
  

</LinearLayout>

Second, create the Mycustomdialog class to inherit dialog:

Code:

Package com.xzw.custom.dialog;
Import Android.app.Dialog;
Import Android.content.Context;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.EditText; /** * Custom Dialog/public class Mycustomdialog extends Dialog {//define callback event for Dialog Click event public Interface Oncustomdi
    aloglistener{public void Back (String name);
    private String name;
    Private Oncustomdialoglistener Customdialoglistener;

    EditText Etname;
        Public Mycustomdialog (Context context,string Name,oncustomdialoglistener Customdialoglistener) {super (context);
        THIS.name = name;
    This.customdialoglistener = Customdialoglistener; 
        } @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
        Setcontentview (R.layout.dialog); 
        Set the title Settitle (name);
        Etname = (edittext) Findviewbyid (R.id.edit); Button clickbtn = (button)Findviewbyid (R.ID.CLICKBTN);
    Clickbtn.setonclicklistener (Clicklistener); Private View.onclicklistener Clicklistener = new View.onclicklistener () {@Override PU
          Blic void OnClick (View v) {customdialoglistener.back (string.valueof (Etname.gettext ()));
        MyCustomDialog.this.dismiss ();

}
    };

 }

Finally complete the mainactivity:

Code:

Package com.xzw.custom.dialog;
Import Android.os.Bundle;
Import android.app.Activity;
Import Android.view.Menu;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.TextView;

Import Android.widget.Toast;
  public class Mainactivity extends activity implements Onclicklistener {private TextView resulttext;
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
    Resulttext = (TextView) Findviewbyid (R.id.result);
    Button showdialogbtn = (button) Findviewbyid (R.id.showdialog);
    
   
  Showdialogbtn.setonclicklistener (this); @Override public void OnClick (View v) {mycustomdialog dialog = new Mycustomdialog (this, "Enter your NA Me ", new Mycustomdialog.oncustomdialoglistener () {@Override public void B ACK (String name) {Resulttext.settext ("EntEr name is "+name";
            }
            });
        
  Dialog.show ();

 }
         
  
}

Effect as shown:

Cool upgrade Version
in the day-to-day development process, Android's own dialog box control is far from beautiful enough to meet the requirements of development, especially in relation to porting development, the demo described below is based on the 1280x720 resolution implementation results.

The custom dialog box is very similar to the custom Ratingbar of the last record, both by inheriting the style of the parent class (here, dialog) in Styles.xml.
Styles.xml

<style name= "Noticedialog" parent= "@android: Style/theme.dialog" >
    <item name= "Android:windowframe" > @null </item><!--border-->
    <item name= "android:windowisfloating" >true</item><!-- Whether it emerges above the activity-->
    <item name= "android:windowistranslucent" >false</item><!--Translucent-->
    <item name= "Android:windownotitle" >true</item><!--untitled-->
    <item name= "Android: Windowbackground "> @drawable/tck_bg</item><!--background transparent-->
    <item name=" Android: backgrounddimenabled ">false</item><!--fuzzy-->
  </style>

We're going to do the following three effects:
(1) Hint with select confirmation box

(2) picture + text Tips

(3) Picture + picture

To achieve the above three effects we only need to inherit a dialog class, and then add the corresponding XML layout according to the different layout can simply realize the effect of the function extension.
1. Inherit the dialog class, override the method of the parent class, and add the subclass's own method.
Noticedialog.java, inherits from the dialog parent class, realizes the Click event interface, if has the confirmation selection box, then the confirmation selection box control adds click event listens, realizes the interface update and the logic operation in the callback method inside the UI main thread.

Package com.zlc.dialog;
Import Android.app.Dialog;
Import Android.content.Context;
Import Android.os.Bundle;
Import android.view.KeyEvent;
Import Android.view.View;
Import Android.view.View.OnClickListener;
 
 
Import Android.widget.TextView;
  public class Noticedialog extends Dialog implements onclicklistener{context;
  Private Noticedialoglistener listener;
  dialog box event listener interface for handling callback click events Public interface Noticedialoglistener {public void OnClick (view view);
    Public Noticedialog {Super (context);
  TODO auto-generated constructor stub this.context = context;
    } public Noticedialog (context Context,int theme) {Super (context, theme);
  This.context = context;
    } public Noticedialog (context Context,int Theme,noticedialoglistener listener) {Super (context, theme);
    This.context = context;
  This.listener = listener; } @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestATE); TextView Enter = (TextView) Findviewbyid (r.id.dialog_enter);//Determine control TextView Cancel = (TextView) Findviewbyid (R.id.dialo g_cancle)//Cancel the control if (enter!= null && cancel!= null) {//If the checkbox is not with a confirmation, do not do an event listening operation Enter.setonclicklistener (this
      );
      Cancel.setonclicklistener (this);
    Enter.requestfocus ();
  @Override public void OnClick (View v) {//TODO auto-generated Method stub Listener.onclick (v);

 }
}

2. Add different XML layouts, corresponding to the above three effects.
(1) prompts with a select confirmation box dialog_notice_choise.xml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "android:orientation=" vertical "android:layout_width=" 652DP "android:layout_height=" 352DP "> <line Arlayout android:layout_width= "500DP" android:layout_height= "200DP" android:layout_marginleft= "76DP" and roid:layout_margintop= "76DP" android:orientation= "vertical" android:background= "@drawable/tck01" > <linea Rlayout android:layout_width= "fill_parent" android:layout_height= "Wrap_content" Android:layout_marginto
       p= "5DP" android:layout_marginleft= "10DP" > <textview android:textsize= "26sp" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:textcolor= "@color/dialog_titl
     
    E "android:text=" @string/dialog_title "android:focusable= false"/> </LinearLayout> <linearlayout AndrOid:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:layout_margintop= "40DP" Andr oid:gravity= "center" > <textview android:id= "@+id/notice_value" android:textsize= " 32SP "android:layout_marginleft=" 10DP "android:layout_width=" wrap_content "android:layout_height=" Wrap_ Content "android:textcolor=" @color/dialog_content "android:text=" @string/dialog_uninstall "Android:foc
      Usable= "false"/> </LinearLayout> <linearlayout android:layout_width= "Fill_parent"  
      android:layout_height= "44DP" android:layout_margintop= "35DP" android:layout_marginleft= "4DP" > <textview android:id= "@+id/dialog_enter" android:textsize= "25sp" android:layout_width= "246d
      P "android:layout_height=" Fill_parent "android:text=" "@string/dialog_enter" android:gravity= "center" Android:textcolor= "@drAwable/app_manager_dialog_textcolor "android:background=" @drawable/app_manager_dialog_btn_color "Android:focus
         Able= "true"/> <textview android:id= "@+id/dialog_cancle" android:textsize= "25SP"
       Android:layout_width= "246DP" android:layout_height= "fill_parent" android:text= "@string/dialog_cancel" android:gravity= "center" android:textcolor= "@drawable/app_manager_dialog_textcolor" Android:backgrou
       
    nd= "@drawable/app_manager_dialog_btn_color" android:focusable= "true"/> </LinearLayout>   
 </LinearLayout> </LinearLayout>

 
(2) picture + text hint Dialog_notice_ing.xml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "android:orientation=" vertical "android:layout_width=" 652DP "android:layout_height=" 352DP "> <line
       Arlayout android:layout_width= "500DP" android:layout_height= "200DP" android:layout_marginleft= "76DP" android:layout_margintop= "76DP" android:orientation= "vertical" android:background= "@drawable/tck01" > <li Nearlayout android:layout_width= "fill_parent" android:layout_height= "Wrap_content" Android:layout_margi
       ntop= "5DP" android:layout_marginleft= "10DP" > <textview android:textsize= "26sp" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:textcolor= "@color/dialog_t Itle "android:text=" @string/dialog_title "/> </LinearLayout> <linearlayout and
 Roid:layout_width= "Fill_parent"     android:layout_height= "Wrap_content" android:layout_margintop= "50DP" android:gravity= "Center" > <imageview android:layout_width= "38DP" android:layout_height= "42DP" android:src= "@dr
      Awable/uninstall_icon "/> <textview android:id=" @+id/dialog_in_msg "android:textsize=" 32SP "
      android:layout_marginleft= "10DP" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:textcolor= "@color/dialog_content" android:text= "@string/dialog_uninstall_in"/> </li    

 Nearlayout> </LinearLayout> </LinearLayout>

(3) picture + picture Dialog_notice_finish.xml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "android:orientation=" vertical "android:layout_width=" 652DP "android:layout_height=" 352DP "> <line
       Arlayout android:layout_width= "500DP" android:layout_height= "200DP" android:layout_marginleft= "76DP" android:layout_margintop= "76DP" android:orientation= "vertical" android:background= "@drawable/tck01" > <li Nearlayout android:layout_width= "fill_parent" android:layout_height= "Wrap_content" Android:layout_margi
       ntop= "5DP" android:layout_marginleft= "10DP" > <textview android:textsize= "26sp" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:textcolor= "@color/dialog_t Itle "android:text=" @string/dialog_title "/> </LinearLayout> <linearlayout and
 Roid:layout_width= "Fill_parent"     android:layout_height= "Wrap_content" android:layout_margintop= "40DP" android:gravity= "Center" > <imageview android:layout_width= "66DP" android:layout_height= "67DP" android:src= "@dr
       AWABLE/CG "/> <imageview android:id=" @+id/dialog_finish_img "android:layout_marginleft=" 20DP " Android:layout_width= "165DP" android:layout_height= "36DP" android:src= "@drawable/uninstall_ok" 
 > </LinearLayout> </LinearLayout> </LinearLayout>

    
3. Implement the add display for a custom dialog box in Mainactivity.
Mainactivity.java, you just need to set up a different XML configuration file when you switch the dialog box to display. (Note: The context parameter of the construction method inside the Noticedialog can only be xxxactivity.this, not a context object obtained by Getapplicationcontext)

Package com.zlc.dialog;
Import Java.util.Timer;
 
Import Java.util.TimerTask;
Import android.app.Activity;
Import Android.content.Context;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.os.Message;
 
Import Android.view.View;
 
Import Com.zlc.dialog.NoticeDialog.NoticeDialogListener;
  public class Mainactivity extends activity {private context context;
  Private Noticedialog Notidialog;
  int count = 0;
  Handler Handler;
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
    context = Getapplicationcontext (); Notidialog = new Noticedialog (Mainactivity.this, R.style.noticedialog, New Noticedialoglistener () {@Overrid e public void OnClick (view view) {try {if (View.getid () = R.id.dialog_enter) {Notidi
            Alog.dismiss ();
        TODO purchase} notidialog.dismiss ();
   catch (Exception e) {       E.printstacktrace ();
    }
      }
    });
    Notidialog.setcontentview (r.layout.dialog_notice_choise);
    Notidialog.show ();
    Timer timer = new timer ();
     
    Handler = new MyHandler ();
        Timer.schedule (New TimerTask () {@Override public void run () {//TODO auto-generated method stub
        Count = count% 4;
        Notidialog.cancel ();
        Handler.sendemptymessage (count);
      Count + +;
  }, 3000, 3000); Private class MyHandler extends handler{@Override public void Handlemessage (msg) {//TODO Auto -generated method Stub switch (msg.what) {case 0:notidialog.setcontentview (r.layout.dialog_notice_in
        g);
      Break
        Case 1:notidialog.setcontentview (r.layout.dialog_notice_finish);
      Break
        Case 2:notidialog.setcontentview (r.layout.dialog_notice_choise);
      Break
      Default:break;
    } notidialog.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.