Use Anko and Kotlin to implement the dialog box and warning prompt (KAD 24) on Android and ankokotlin

Source: Internet
Author: User

Use Anko and Kotlin to implement the dialog box and warning prompt (KAD 24) on Android and ankokotlin

By Antonio Leiva

Time: Mar 9 and 2017

Link: https://antonioleiva.com/dialogs-android-anko-kotlin/

 

 

Using Builder, creating warning prompts and dialog boxes in Android is relatively simple. However, due to the complicated language, some prompts are usually vague.

 

In Kotlin, only Lambda expressions can help us understand how code is handled.

 

However, with Anko, we can make it easier. In this article, we can see what is going on.

 

Use Anko for warning in Kotlin

 

It is very easy to write warning prompts through Anko. You only need to create an alert code block:

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

 

In this Code block, you can specify some things, such as the title of the prompt 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 Code is as follows:

 

You can also usepositiveButton,negativeButtonAndneutralButtonMethod customization:

 

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 customize the view through Anko plus:

 

 1 alert { 2     title("Alert") 3     positiveButton("Cool") { toast("Yess!!!") } 4     customView { 5         linearLayout { 6             textView("I'm a text") 7             button("I'm a button") 8             padding = dip(16) 9         }10     }11 }.show()

 

 

 

Progress dialog box

 

Anko has another feature. You can create a progress dialog box and uncertain the progress.

 

Here is the second example. You can use this simple method to create a progress dialog box:

 

1 indeterminateProgressDialog("This a progress dialog").show()

 

 

The result is as follows:

 

 

 

Conclusion

 

With Anko and Kotlin, creating a small DSL is simple. Ideally, you can create a DSL in any part of the framework, so it makes interaction very easy.

 

In a later article, we will explain how to use SQLite to process databases.

 

Now, should you read the free User Guide to learn how to build your first project? Or read a book and learn how to create a complete application from scratch?

 

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.