Use Anko and Kotlin to implement dialog boxes and warning prompts on Android (KAD 24)

Source: Internet
Author: User

Antonio Leiva

Time:Mar 9,

Original link:https://antonioleiva.com/dialogs-android-anko-kotlin/

with Builder, creating warning tips and dialogs on Android is relatively simple, but because of the complexity of the language, some hints often become very vague.

in the in Kotlin, only Lambda Expressions can help us understand a little bit about how the code is handled.

However, with the Anko, we can make it all easier, and in this article we see what's going on.

in the Kotlin use Anko as a warning tip.

through Anko Writing warning tips is very easy. You only need to create an alert code block:

1 alert ("Testing Alerts") {2    ... 3 }.show ()

In this block of code, you can specify something, such as the title of the hint, or the button you want to display:

1 alert ("Testing Alerts") {2     title ("Alert")3     Yesbutton {toast ("Yess!!!" )}4    Nobutton {}5 }.show ()

The result of this piece of code is this:

You can also use positiveButton , negativeButton and the neutralButton method to customize some actions:

1 alert ("Testing Alerts") {2     title ("Alert")3     Positivebutton ( "Cool") {toast ("Yess!!!" )}4     Negativebutton ("Never Ever") {}5     Neutralbutton ("I ll think About it ")6 }.show ()

of course, you can even pass Anko Custom Views:

1 Alert {2Title ("Alert")3Positivebutton ("Cool") {toast ("Yess!!!") }4 CustomView {5 LinearLayout {6TextView ("I ' m a text")7button ("I ' m a button")8padding = Dip (16)9         }Ten     } One}.show ()

Progress dialog Box

Anko There is another feature that allows you to create a progress dialog box, and an indeterminate progress.

Here is a second example where you can create a progress dialog box in such a simple way:

1 indeterminateprogressdialog ("This a progress dialog"). Show ()

The results are as follows:

Conclusion

as a result of Anko and Kotlin, as you can see, creating a small DSL is an easy thing to do. Ideally, you can create a DSL in any part of the framework , so it makes the interaction very easy.

in the later in the article , we explain how to process the database through SQLite .

now, is it time to read Free usage guide to Learn how to build your first project? or read a book and learn how to create a complete application from scratch?

Use Anko and Kotlin to implement dialog boxes and warning prompts on Android (KAD 24)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.