Internal encapsulation of the implementation progress box Toast box pop up box confirmation box within the Android AndBase framework (2)

  • 2021-06-28 14:12:19
  • OfStack

This is the second note for AndBase framework learning. Friends who want to know the AndBase framework can read this article and learn from it together.

Implement progress boxes, Toast boxes, pop-up boxes, confirmation boxes using the AbActivity internal encapsulation method

AbActivity in AndBase encapsulates a number of methods for us to use, making it more convenient to use, simply passing relevant parameters... Eliminating our own use of basic functions to construct..

It's like a progress box, an Toast box, a pop-up box, a confirmation box... these basic things are encapsulated in AndBase's AbActivity... We just need to pass parameters to invoke internal methods to create these views... whether it's a call to a progress bar, an Toast box, or a pop-up box, a confirmation box (the confirmation box and a pop-up box are basically similar),It's just that the confirmation box has more controls than the pop-up box. Pop-up box 1 is usually used to directly pop up a piece of text information, while to confirm the box you need to add related buttons... In short, the way to call it is very simple..


/*
 * 
 *  Multifunctional 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")); // Seek ID Of 1 A better way .. This is done on the premise that ID Definition 1 Must have 1 Defined 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_text, null);
 switch(v.getId()){
 case R.id.but_0:
  showProgressDialog(); // Direct pop-up circle progress box ...
  break;
 case R.id.but_1:
  showToast("Toast Text Box "); //Toast Text Box ...
  break;
 case R.id.but_2:
  showDialog(AbConstant.DIALOGTOP, view); // Popup .. Pass in the position where the parameter is displayed and the view displayed ... This means 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 that the confirmation box is also called showDialog Method .. Only the parameters passed are different ..AbActivity Different methods are also defined for these different pop-ups ...
  showDialog(" Title ", " describe ", new OnClickListener() {
  
  @Override
  public void onClick(DialogInterface dialog, int which) {
   // TODO Auto-generated method stub
   showToast(" Click to confirm ");
  }
  });
  break;
 }
 }

}

Here we can see that simply calling the related methods and passing the relevant parameters will complete the creation of these basic Views... which will make our operation more convenient...

Previous Toast information pop-up box calls:

Toast.makeText (getApplicationContext(), "Need information to show", Toast.LENGTH). show();
Now the way is this:

showToast ("Information to be displayed");

This allows the display of the Toast message box to be completed directly. We might not think about this simple way of encapsulating it. But what if it's more code?Let's take our confirmation pop-up box for example... If you were writing a confirmation pop-up box in the previous Activity, we need to rewrite it manually... Here's a confirmation box handwritten based on Activity...


AlertDialog.Builder builder=new Builder(MainActivity.this);
builder.setTitle(" Tips ");
builder.setMessage(" Confirm Exit ");
builder.setPositiveButton(" confirm ", new OnClickListener() { // Add Confirmation Button ...
  
  @Override
  public void onClick(DialogInterface dialog, int which) {
   // TODO Auto-generated method stub
   dialog.dismiss(); / Tip box disappears ..
  }
  });
builder.setNegativeButton(" cancel ", new OnClickListener() { // Add Cancel Button ..
  
  @Override
  public void onClick(DialogInterface dialog, int which) {
   // TODO Auto-generated method stub
   dialog.dismiss();
  }
  });

By using the AndBase framework to call functions, this makes the process very simple. We just need to pass in the relevant parameter invocation methods... The call to this function has the same effect as the above writing, and we don't need to set dialog.dismiss() here anymore;The pop-up box disappears when the button is clicked.. The OnClick method here is what you need to do after the pop-up box disappears.. In contrast, using the frame simplifies the amount of code


showDialog(" Tips ", " Confirm Exit ", new OnClickListener() {
  
  @Override
  public void onClick(DialogInterface dialog, int which) {
   // TODO Auto-generated method stub
   showToast(" Click to confirm ");
  }
  });

In fact, these simple Views are still very simple... Although it doesn't seem like a lot of code reduction, they can be very useful for more complex operations... the optimized methods in the framework are still very useful... that is, they reduce the redundancy of the code, and they also achieve more functions...

Let's see the implementation of the source code.

Source implementation of the Toast box... We can see that the methods in the frame encapsulate the original ecological methods, and we can use the Toast box directly by passing only relevant parameters... There is also the source code of showToastInThread (). By opening a new thread


/**
 *  Description: Toast Prompt Text .
 * @param text  text 
 */
 public void showToast(String text) {
 Toast.makeText(this,""+text, Toast.LENGTH_SHORT).show(); // This method is already encapsulated here ... We just need to pass in the text we need to display ...
 }
 
 /**
 *  Description: Toast Prompt Text .
 * @param resId  Resources for Text ID
 */
 public void showToast(int resId) {
 Toast.makeText(this,""+this.getResources().getText(resId), Toast.LENGTH_SHORT).show(); // This is done by resID To set the text you want to display ...
 }

The source code implementation of the progress box...The source code is very simple...We just need to call both methods when using a progress bar...


/**
 *  Description: Display progress box .
 */
 public void showProgressDialog() {
 showProgressDialog(null); // No progress box showing progress ..
 }
 
 /**
 *  Description: Display progress box .
 * @param message the message
 */
 public void showProgressDialog(String message) {
 //  Establish 1 Display progress Dialog
 if(!AbStrUtil.isEmpty(message)){
  mProgressMessage = message; // Set the information displayed in the progress box ...
 }
 if (mProgressDialog == null) {
  mProgressDialog = new ProgressDialog(this);
  //  Set Click Screen Dialog Do not disappear  
  mProgressDialog.setCanceledOnTouchOutside(false);
 }
 mProgressDialog.setMessage(mProgressMessage);// Set the message displayed in the progress box ..
 showDialog(AbConstant.DIALOGPROGRESS);
 }

I will not paste the source code of the pop-up box, because the source code does write a bit too much.. Here only paste the encapsulation method under 1..

This method displays a dialog box which is an information pop-up box with no buttons but a prompt for information.. The function of id is to set the location where the pop-up box is displayed.. View represents the view that needs to be displayed.. This creates a pop-up box to display in the specified location..

public void showDialog(int id,View view) {}
During the execution of the above method, the setDialogLayoutParams() function is called to set the related properties, set the parameters of the pop-up layout, that is, how the pop-up box displays better on the screen.

This is the call from the source code and it doesn't seem hard to understand...


private void setDialogLayoutParams(Dialog dialog,int dialogPadding,int gravity){
 dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // Setup has no title ..
 Window window = dialog.getWindow(); // Get Window ...
 WindowManager.LayoutParams lp = window.getAttributes();// Get window properties ...
 // Here you can set dialog Displayed position 
 window.setGravity(gravity); // Set the way windows are paired ...
 // 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 animation 
 window.setWindowAnimations(android.R.style.Animation_Dialog); 
 //  Set Click Screen Dialog Do not disappear  
 dialog.setCanceledOnTouchOutside(false);

 }

The pop-up box for this method is a confirmation box with buttons. title and msg represent the title and prompt of the dialog box.. and the delivery of the listening event that occurs when the confirmation button is clicked..

public void showDialog(String title,String msg,DialogInterface.OnClickListener mOkOnClickListener) {}
The only difference between this method's pop-up box and the one above is that the content of the pop-up box is not a specified string, but a view we customize..

public AlertDialog showDialog(String title,View view,DialogInterface.OnClickListener mOkOnClickListener) {}
The pop-up boxes are basically these patterns...the implementation of the source code is very simple...the principle is to encapsulate a few of those basic functions...the remaining few are not pasted.

This is the whole content of this article, and I hope it will be helpful for everyone to learn.


Related articles: