Method of customizing dialog with builder mode in Android

preface

Our developers meet a variety of needs in the actual project process. Sometimes we need to match the design style of app. Sometimes we feel that the dialog box of the system is not free to use. Therefore, it is necessary to define a dialog suitable for ourselves.

Why use builder mode

Builder Design pattern is a creation pattern that creates a complex object step by step. It allows users to more finely control the construction process of objects without knowing the internal construction details. Its advantage is to decouple the construction and representation of objects. We all know that the dialog box of the Android system itself, such as alertdialog, adopts the builder mode, so it can be seen that the builder mode is very suitable for building dialog objects.

I won't say much below. Let's go to the code.

BaseDialog. java

The code is very simple. The basedialog class defines the controls to be displayed in some dialog boxes, some properties corresponding to these controls, and finally the method show() to fill all properties into the control. Builder class is an internal class of basedialog, which defines the set method of all properties of basedialog class and the create () method after assembly.

The layout file dialog of the corresponding custom dialog_ base. The XML is as follows:

Other resource documents involved are as follows:

Dialog style styles xml

By setting these properties, you can ensure that the dialog box background is transparent without black edges.

Determines the color value of the Cancel button colors xml

Dialog background BG_ base_ dialog. xml

Including background, fillet and shadow effects.

Determine button background BG_ dialog_ btn_ positive. xml

Contains normal and pressed effects.

Cancel button background BG_ dialog_ btn_ negative. xml

Contains normal and pressed effects.

The above is all about the whole custom dialog. Next, we will demonstrate how to use it through a simple demo.

MainActivity. java

Let's look at the renderings below:

It's ugly and wooden, but it doesn't matter. Here we just show its usage. How to make the dialog box look better depends on your play. It can be seen that the use method of the customized basedialog is basically the same as that of andorid's own alertdialog, and the objects are built through its builder class.

The customization dialog box also supports the function of displaying ProgressBar and customizing content filling.

The progressbar is displayed and the touch screen cannot be cancelled:

The renderings are as follows:

Customize the content area without displaying the Cancel button:

The corresponding layout file is dialog_ input_ amount. xml:

The renderings are as follows:

summary

It can be seen from this article that customizing the dialog through the builder mode can not only maintain the use method of the original Android dialog, but also be easy to use and have a higher degree of freedom. Everyone can make corresponding modifications to the code according to their own needs. It should be noted that this paper does not implement the dialog box in strict accordance with the traditional builder design mode, but makes some simplification to be more suitable for our scenario. The above is the whole content of this article. I hope the content of this article can be helpful to Android developers.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>