Universal core code base – code: suitable for students to learn from the beginning of programming ideas, can also be directly used.
If you want the source git address, you can download it yourself.
Application Scenarios
I believe many of you have encountered such a scenario when you are developing a project: when you click to delete an item or an image, a dialog box will pop up asking “Do you want to delete it?” Click OK to delete it.
This scenario is used in most projects, of course, there are many other places to use the prompt box besides deleting things, so I also wrapped it into the core code base, let’s see how easy it is to use once wrapped.
Code using
Use the showTipDialog method as shown below
public class MainActivity extends BaseActivity implements View.OnClickListener {
@Override
protected Object setLayout(a) {
return R.layout.activity_main;
}
@Override
protected void onBindView(@Nullable Bundle savedInstanceState) {
findView(R.id.btnDelete).setOnClickListener(this);
}
@Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.btnDelete) {
showTipDialog("Please confirm whether to delete?".new TipDialog.TipClickCallBack() {
@Override
public void onConfirm(a) {
// Verify that the actual deletion logic is performed
}
@Override
public void onCancel(a) {
/ / not to delete}}); }}}Copy the code
If you want to customize the text of two buttons, you can call another overloaded method like this:
showTipDialog("Please confirm whether to delete?".false."Cancel"."Delete".new TipDialog.TipClickCallBack() {
@Override
public void onConfirm(a) {
// Verify that the actual deletion logic is performed
}
@Override
public void onCancel(a) {
/ / not to delete}});Copy the code
Implementation details
The showTipDialog method is written in BaseActivity, so let’s go into BaseActivity and look at the code for the showTipDialog method, which is actually a couple of overloaded methods, Finally, a TipDialogCreator class is used to create and display TipDialog.
@Override
public void showTipDialog(String tipMsg, final boolean isFinishWhenCancel) {
showTipDialog(tipMsg, isFinishWhenCancel, false.null.null.null);
}
@Override
public void showTipDialog(String tipMsg, TipDialog.TipClickCallBack tipClickCallBack) {
showTipDialog(tipMsg, false.false.null.null, tipClickCallBack);
}
@Override
public void showTipDialog(String tipMsg, boolean canceledOnTouchOutside, TipDialog.TipClickCallBack tipClickCallBack) {
showTipDialog(tipMsg, false, canceledOnTouchOutside,
null.null, tipClickCallBack);
}
@Override
public void showTipDialog(String tipMsg, boolean canceledOnTouchOutside, String cancelString,
String confirmString, TipDialog.TipClickCallBack tipClickCallBack) {
showTipDialog(tipMsg, false, canceledOnTouchOutside,
cancelString, confirmString, tipClickCallBack);
}
@Override
public void showTipDialog(String tipMsg, final boolean isFinishWhenCancel,
boolean canceledOnTouchOutside, String cancelString,
String confirmString, TipDialog.TipClickCallBack tipClickCallBack) {
tipDialogCreator.showTipDialog(tipMsg, isFinishWhenCancel, canceledOnTouchOutside,
cancelString, confirmString, tipClickCallBack);
}
Copy the code
Go to TipDialogCreator and look at its code. It is very simple to create a TipDialog and set various text and callbacks to it. TipDialog is a normal custom Dialog:
public void showTipDialog(String tipMsg, final boolean isFinishWhenCancel, boolean canceledOnTouchOutside,
String cancelString, String confirmString, TipDialog.TipClickCallBack tipClickCallBack) {
TipDialog tipDialog = new TipDialog(context);
tipDialog.setTipClickCallBack(tipClickCallBack);
tipDialog.setCanceledOnTouchOutside(canceledOnTouchOutside);
tipDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
if(isFinishWhenCancel && cancelAble ! =null) { cancelAble.toCancel(); }}}); tipDialog.setTipText(tipMsg); tipDialog.setButtonText(cancelString, confirmString);try {
tipDialog.show();
} catch(Exception e) { e.printStackTrace(); }}Copy the code
Design principles and techniques
-
We follow a single principle and use TipDialogCreator to create and display tipDialogs. This process improves the reusability of the code. Not only does BaseActivity need to create TipDialog, BaseFragment, BaseDialog, etc., you can reuse them directly.
-
What if you find the layout of TipDialog too ugly or inappropriate for your APP and want to change the UI
-
Of course, the quickest way is to change the layout file directly. However, I do not recommend doing so. The core code base is designed to follow the “general” features, which are specific to the needs of the specific project. Do not directly change the code in the core code base.
-
We provide a way to set the layout from the outside. Let’s see if TipDialog sets the layout from AppGlobal first. If it does, use the layout file with the outside Settings. Let’s go back to the AppGlobal configuration and set up a custom layout file.
-
For an introduction to AppGlobal, if you don’t know, watch a video.
-
Left left left left left left left left left left down down down down down down down down down left left left left left left left left left left down down down down down down down down down left left left left left left left left left left down down down down down down down down down left left left left left left left left left left down down down down down down down down down
If you think it’s useful, please give me a thumbs up. Personal public account “Grade GUI”, welcome to follow