Android Andbase Framework Internal Package Implementation Progress box, Toast box, pop-up box, confirmation box (ii) _android

Source: Internet
Author: User
Tags stub

This article is for the Andbase framework to learn to organize the second note, want to understand the andbase framework of friends can read this article, we learn together.

Implement a Progress box, Toast box, pop-up box, confirmation box using the Abactivity internal encapsulation method

Andbase in the abactivity encapsulation of a number of ways to provide us to use, so that when used more convenient, only need to pass the relevant parameters. Eliminates our own use of the underlying function to construct ...

Just like the Progress box, Toast box, pop-up box, confirmation box ... These basic things are packaged in the Andbase abactivity ... We only need to pass parameters to call inside the method to complete the creation of these views ... Whether it's a progress bar call, or Toast box, or pop-up box, confirm box (check box and pop-up box is basically similar), just Confirm box than pop-up box more than some related controls, pop-up box is generally used to directly pop a piece of text information, and go to confirm box will also need to add related buttons ... In short, the way to call is very simple ...

* * * Multi-function Menu ... * * * * * Package com.example.andbaseanotheractivity;
Import com.ab.activity.AbActivity;

Import com.ab.global.AbConstant;

Import Android.content.DialogInterface;
Import Android.content.DialogInterface.OnClickListener;
Import Android.os.Bundle;
Import Android.view.Menu;

Import Android.view.View;

Import Android.widget.Button;
 public class Mainactivity extends Abactivity implements View.onclicklistener {private Button but[]=new button[6];
 Private view view;
 @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
 
 
 Setabcontentview (R.layout.activity_main);
 Initfindid ();
  public void Initfindid () {for (int i=0;i<but.length;i++) {String resid= ' but_ ' +i; but[i]= (Button) Findviewbyid (This.getresources (). Getidentifier (RESID, "id", "com.example.andbaseanotheractivity") ); A better way to look for IDs.
 The use of this approach is premised on the definition of ID must have certain rules ... but[i].setonclicklistener (this); @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is present.
 Getmenuinflater (). Inflate (R.menu.main, menu);
 return true; @Override public void OnClick (View v) {//TODO auto-generated Method stub view=minflater.inflate (R.layout.demo_tex
 T, NULL);
 Switch (V.getid ()) {case r.id.but_0:showprogressdialog ();///Eject the circular progress box directly ... break; Case R.id.but_1:showtoast ("Toast text box");
 Toast text box ... break; Case R.id.but_2:showdialog (abconstant.dialogtop, view); Pop-up box ... Pass the parameter to the display position and the view displayed ...
 This indicates a pop-up at the top ... break; Case R.id.but_3:showdialog (abconstant.dialogcenter, view);
 Break
  Case R.id.but_4:showdialog (Abconstant.dialogbottom, view);
 Break Case R.ID.BUT_5://You can see the confirmation box is also called ShowDialog method ... Only the parameters passed are not the same. Abactivity also defines different methods for these different pop-up boxes ... showdialog (title, Description, New Onclicklistener () {@Override public void OnClick (Di
  Aloginterface dialog, int which) {//TODO auto-generated Method Stub showtoast ("Click OK");
  }
  });
 Break

 }
 }

}

Here we can see that only the relevant methods need to be invoked to pass the relevant parameters to complete the creation of these basic view ... So as to make our operation more convenient ...

How the previous Toast information pop-up box was invoked:

Toast.maketext (Getapplicationcontext (), "need information to display", Toast.length). Show ();
This is the way it is now:

Showtoast ("Information to be displayed");

This allows you to complete the display of the Toast information box. For such a simple way of encapsulation, perhaps we disagree. But what about the amount of code? Just take our confirmation pop-up box ... If you are writing a confirmation pop-up in a previous activity, we need to manually rewrite it ... Here is a confirmation box that is written based on the activity ...

Alertdialog.builder builder=new Builder (mainactivity.this);
Builder.settitle ("hint");
Builder.setmessage ("Confirm exit");
Builder.setpositivebutton ("Confirm", new Onclicklistener () {///Add Confirmation button ...
  
  @Override public
  void OnClick (dialoginterface dialog, int which) {
   //TODO auto-generated a stub
   dialog . dismiss (); /Prompt box disappears ...
  }
  );
Builder.setnegativebutton ("Cancel", new Onclicklistener () {///Add Cancel button ...
  
  @Override public
  void OnClick (dialoginterface dialog, int which) {
   //TODO auto-generated stub
   Dialog.dismiss ();
  }
  );

Using the Andbase framework by calling the function, this makes the process very simple. We just need to pass the relevant parameters to call the method ... The call to this function is the same as the way it was written, and we don't need to set Dialog.dismiss (), and the pop-up box disappears when we click the button. The OnClick method here is what you need to do after a subsequent pop-up box disappears ... By contrast, using a framework simplifies the amount of code

ShowDialog ("Prompt", "Confirm exit", new Onclicklistener () {
  
  @Override public
  void OnClick (dialoginterface dialog, int which) {
   //TODO auto-generated method stub
   showtoast ("click OK");
  }
  );

In fact, these simple view is very simple ... Although it seems that the amount of code is not very much reduced, if for more complex operations ... The optimization method in the frame is still very useful ... That is to reduce the redundancy of the code, but also to achieve more features ...

Or look at the implementation process of the source code ...

Toast box of the source code implementation process ... We can see that the method in the framework encapsulates the original ecological method, we just need to pass the relevant parameters to use the Toast box directly ... In fact, there is also the source code Showtoastinthread (). By opening a new thread

/**
 * Description: Toast hint text.
 * @param text text */public
 void Showtoast (String text) {
 Toast.maketext (this, "" +text, Toast.length_ Short). Show (); This method has been encapsulated here ... All we have to do is pass the text that needs to be displayed ...
 }
 
 /**
 * Description: Toast hint text.
 * @param resid Text's resource ID
 */public
 void showtoast (int resid) {
 Toast.maketext (this, "" +this.getresources ( ). GetText (Resid), Toast.length_short). Show (); This method uses Resid to set the text you want to display ...
 }

Progress box of the source code implementation process ... The source code is very simple ... We only need to call these two methods when we use a progress bar ...

/**
 * Description: Show Progress box.
 *
 /public void Showprogressdialog () {
 showprogressdialog (null);//No Progress box showing progress ...
 }
 
 /**
 * Description: Show Progress box.
 * @param Message "* */public
 void Showprogressdialog (String message) {
 //Create a dialog
 that shows progress if (! Abstrutil.isempty (message)) {
  mprogressmessage = messages;//Set the information displayed in the Progress box ...
 }
 if (Mprogressdialog = = null) {
  Mprogressdialog = new ProgressDialog (this);
  Set Tap Screen dialog does not disappear 
  mprogressdialog.setcanceledontouchoutside (false);
 }
 Mprogressdialog.setmessage (mprogressmessage);/set the message displayed by the Progress box ...
 ShowDialog (abconstant.dialogprogress);
 }

Pop-up box of the source code I will not be pasted, because the source code is written a bit more ... Here just paste the package method ...

This method shows the dialog box is an information pop-up box, no button, just to carry out the message ... the role of the ID set pop-up display position ... View represents the views that need to be displayed ... This creates a pop-up box to display at the specified location ...

public void ShowDialog (int id,view View) {}
during the execution of the above method, the Setdialoglayoutparams () function is invoked to set the related properties, set the parameters of the popup layout, and also the pop-up box to display a better setting on the screen ...

This is the source of the call process, it does not look very difficult to understand ...

private void Setdialoglayoutparams (Dialog dialog,int dialogpadding,int gravity) {
 dialog.requestwindowfeature ( Window.feature_no_title); Setting has no title ...
 window window = Dialog.getwindow (); Get window ...
 Windowmanager.layoutparams LP = Window.getattributes ();//Get Window Properties ...
 Here you can set the location of the dialog display
 window.setgravity (gravity);//Set the window to its way ...
 Set width
 lp.width = diaplaywidth-dialogpadding; 
 Lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
 Background transparent
 //lp.screenbrightness = 0.2f;
 Lp.alpha = 0.8f;
 Lp.dimamount = 0f;
 Window.setattributes (LP); 
 Add animated
 window.setwindowanimations (Android. R.style.animation_dialog); 
 Set Tap Screen dialog does not disappear 
 dialog.setcanceledontouchoutside (false);

 }

The pop-up box for this method is a confirmation box with a button, and the title and MSG represent the contents of the dialog box and the contents of the message ... And the listening event that happens when you click on the confirmation button ...

public void ShowDialog (String title,string msg,dialoginterface.onclicklistener mokonclicklistener) {}
The only difference between the pop-up of this method and the above is that the contents of the pop-up box are not a specified string, but a view that we customize.

Public Alertdialog ShowDialog (String title,view view,dialoginterface.onclicklistener mokonclicklistener) {}
Pop-up boxes are basically these modes ... The implementation of the source code is very simple ... The principle is the encapsulation of those basic functions ... The remaining few will not be pasted. Same

The above is the entire content of this article, I hope to help you learn.

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.