PS: Gradually understand, stay in the laboratory for three years is better than a year in the enterprise ...
Learning content:
1. Use the Abactivity internal encapsulation method to implement the Progress box, Toast box, popup box, confirmation box ...
Andbase in the Abactivity package good many methods provide us to use, making it more convenient when using, only need to pass the relevant parameters can be. Eliminates the use of our own basic functions to construct ...
Just like the Progress box, Toast box, popup box, confirmation box ... These basic things are in the andbase of the abactivity package well ... We just need to pass parameters to call inside of the method to complete the creation of these views ... Whether it is a progress bar call, or Toast box, or pop-up box, confirmation box (confirmation box and pop-up box basically similar), just confirm box more than the popup box more related controls, pop-up box is generally used to directly pop up a text message, and go to confirm the box also need to add related buttons ... In short, the way to call is very simple ...
/** * [email protected] by darker 2015-10-31 * multi-function Menu ... **/ Packagecom.example.andbaseanotheractivity;Importcom.ab.activity.AbActivity;Importcom.ab.global.AbConstant;ImportAndroid.content.DialogInterface;ImportAndroid.content.DialogInterface.OnClickListener;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.widget.Button; Public classMainactivityextendsAbactivityImplementsView.onclicklistener {PrivateButton but[]=NewButton[6]; Privateview view; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setabcontentview (R.layout.activity_main); Initfindid (); } Public voidInitfindid () { for(inti=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 find the ID: The use of this method is premised on the definition of the ID must have certain rules ...But[i].setonclicklistener ( This); }} @Override Public BooleanOncreateoptionsmenu (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 voidOnClick (View v) {//TODO auto-generated Method StubView=minflater.inflate (R.layout.demo_text,NULL); Switch(V.getid ()) { CaseR.id.but_0:showprogressdialog ();//pop up the circular progress box directly ... Break; CaseR.id.but_1:showtoast ("Toast text box");//toast text Box ... Break; CaseR.id.but_2:showdialog (abconstant.dialogtop, view);//pop-up box: Pass parameters to the displayed position and the displayed view ... This means that it pops up at the top ... Break; CaseR.id.but_3:showdialog (abconstant.dialogcenter, view);// Break; CaseR.id.but_4:showdialog (Abconstant.dialogbottom, view); Break; Caser.id.but_5://you can see the confirmation box also called the ShowDialog method: Only the arguments passed are not the same: Abactivity also defines different methods for these different pop-up boxes ...ShowDialog ("title", "description",NewOnclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) { //TODO auto-generated Method StubShowtoast ("Click to confirm"); } }); Break; } }}
Here we can see that only the relevant methods need to be called, passing the relevant parameters to complete the creation of these basic view: Thus making our operation more convenient ...
How the previous toast information pop-up box was called:
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 directly. For such a simple way of encapsulation, we may not be able to disagree. But what about the amount of code? Just take our confirmation pop-up box and say ... If you are writing a confirmation popup in your previous activity, we need to manually rewrite it ... Here is a confirmation box based on activity to hand-write ...
Alertdialog.builder builder=NewBuilder (mainactivity. This); Builder.settitle (Prompted); Builder.setmessage ("Confirm Exit"); Builder.setpositivebutton ("Confirm",NewOnclicklistener () {//Add Confirmation button ...@Override Public voidOnClick (Dialoginterface Dialog,intwhich) { //TODO auto-generated Method StubDialog.dismiss (); /The prompt box disappears ... }}); Builder.setnegativebutton ("Cancel",NewOnclicklistener () {//Add Cancel button ...@Override Public voidOnClick (Dialoginterface Dialog,intwhich) { //TODO auto-generated Method StubDialog.dismiss (); } });
Using the Andbase framework makes this process very simple by invoking a function. We just need to pass the relevant parameter call method on it ... The effect of this function is the same as that of the above notation, and we do not need to set Dialog.dismiss () here, as long as the button is clicked, then the popup box disappears ... The OnClick method here is what needs to be done after the subsequent popup disappears ... In contrast, the use of frameworks simplifies the amount of code
New Onclicklistener () { @Override publicvoidint which) { // TODO auto-generated Method stub Showtoast ("Click to confirm");} );
In fact, these simple view is still very simple ... While it may seem that the code volume is not much reduced, but if for more complex operations ... The best way to optimize the frame is to be very useful ... That reduces the redundancy of the code, but also realizes more functions ...
Or take a look at the source of the implementation process ...
Toast box of the source code implementation process ... We can see that the methods in the framework encapsulate the original ecological approach, and we just need to pass the relevant parameters to use the Toast box directly ... In fact, the source code inside the Showtoastinthread (). By opening a new thread
/*** Description: Toast hint text. * @paramtext literal*/ Public voidshowtoast (String text) {Toast.maketext ( This, "" +text, Toast.length_short). Show ();//This method has been encapsulated here ... We just need to pass the text that needs to be displayed ... } /*** Description: Toast hint text. * @paramresId The resource ID of the text*/ Public voidShowtoast (intresId) {Toast.maketext ( This,""+ This. Getresources (). GetText (ResId), Toast.length_short). Show ();//This method uses Resid to set the text to be displayed ...}
Progress frame of the source code implementation process ... The source code is very simple ... We only need to invoke these two methods when we are using a progress bar ...
/*** Description: Displays the Progress box. */ Public voidShowprogressdialog () {Showprogressdialog (NULL);//There is no progress box showing progress: } /*** Description: Displays the Progress box. * @paramMessage The message*/ Public voidshowprogressdialog (String message) {//Create a dialog that shows Progress if(!abstrutil.isempty (message)) {Mprogressmessage= message;//set the information that the progress box displays ... } if(Mprogressdialog = =NULL) {Mprogressdialog=NewProgressDialog ( This); //Set the Tap Screen dialog not disappearMprogressdialog.setcanceledontouchoutside (false); } mprogressdialog.setmessage (mprogressmessage);//Set the Progress box to display the message :ShowDialog (abconstant.dialogprogress); }
The source of the popup box I will not be pasted, because the source code is really a bit more ... Here just paste the method of encapsulation ...
This method displays a dialog box that is a message popup, no buttons, just a hint of information ... the function of the ID setting the popup box to show the location ... View represents the views that need to be displayed ... This creates a popup box to display at the specified location ...
Public void showDialog (int
During the execution of the above method, the Setdialoglayoutparams () function is called to set the relevant properties, set the parameters of the popup layout, that is, how the popup box is displayed on the screen for a better setting ...
This is the source of the call process, it does not seem difficult to understand ...
Private voidSetdialoglayoutparams (Dialog Dialog,intDialogpadding,intGravity) {dialog.requestwindowfeature (window.feature_no_title);//Set No caption ...window window = Dialog.getwindow ();//Get Window ...Windowmanager.layoutparams LP = Window.getattributes ();//Get Window Properties ...//Here you can set the location of the dialog displayWindow.setgravity (Gravity);//Set the window to its way ...//Set WidthLp.width = diaplaywidth-dialogpadding; Lp.type=WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG; //Transparent Background//lp.screenbrightness = 0.2f;Lp.alpha = 0.8f; Lp.dimamount=0f; Window.setattributes (LP); //Add animationsWindow.setwindowanimations (Android. R.style.animation_dialog); //Set the Tap Screen dialog not disappearDialog.setcanceledontouchoutside (false); }
The popup box for this method is a confirmation box with a button, title and MSG indicate the caption content of the dialog box and the prompt content ... And the pass of the listener event that occurs when you click the Confirm button ...
Public void
The only difference between the popup box and the above is that the content of the popup is not a specified string, but a view that we have customized.
Public Alertdialog ShowDialog (String title,view view,dialoginterface.onclicklistener mokonclicklistener) {}
pop-up boxes are basically these patterns ... The implementation of the source code is very simple ... The principle is some encapsulation of those basic functions ... The remaining few will not be pasted. Original Aim ...
finally paste the source code that you wrote: Http://files.cnblogs.com/files/RGogoing/AndBaseAnotherActivity.zip
Android Learning Note andbase Framework Learning (ii) Using the encapsulated Progress box, Toast box, popup box, confirmation box ...