Simple implementation of Android custom dialog

This paper focuses on the custom dialog box. You can clearly understand the principle through the following steps. By changing the interface settings and style types, it can be applied to a variety of apps suitable for you.

Let's first look at the renderings:

The first is the activity interface

Click the button of the above picture to pop up the dialog box:

Click OK in the dialog box:

Click the Cancel button in the dialog box:

The specific implementation steps are as follows:

Step 1: set the style of dialog (general items can be used directly): style.xml

Step 2: customize the dialog:

(1) Custom dialog layout: dialog.xml

(2) Customize the background in the dialog layout: Free_ dialog_ bg.xml

(3) Custom dialog Java: mydialog.class

Step 3: use custom dialog box in activity:

(1) Layout file for activity: activity_ main.xml

(2) MainActivity.class

Principle:

1. Set a theme r.style.mydialog for dialog through the construction method, which mainly sets the display properties of dialog, which is generally fully transparent and borderless;

2. Then, in the oncreate () method of dialog, set the XML file for dialog with setcontentview (r.layout. Selfdialog), and we can create a custom dialog style in the layout file. Here, I have customized the XML file format and realized the customized appearance style, which is not affected by the theme of the system.

3. Then pass values to the custom dialog by setting some public exposed methods for the outside world. The title and message here can be set through external value transmission. For example, the following public method is used by external activities to set title and message:

After the activity instantiates the dialog, you can set the title and message.

In addition, in mydialog.class, you can more flexibly select the type of dialog through the following constructor

Use custom dialog in activity:

4. Finally, when the custom dialog contains some buttons, if you want the button to have a click event, and the click event can be passed to the activity to let the acitvity do something, you need to set a listening interface to let the button click event be known to the external activity. As shown in the following code.

Activity can set the listening interface to get the button click events in real time as follows:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>