Kotlin learning log (a) TextView, Button, Toast use

In Android Studio to use Kotlin write TextView, Button, Toast

Introduction, then I will not say, you can read my first article on Kotlin, and talked about the reasons why the use of Kotlin, entered, we have now re-created a project Kotlin, I'm activity_main.xml file put an id tv_hello the TextView and an id btn_test the Button, and
then in the head MainActivity.kt import
stickers layout file activity_main.xml of code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv_hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
    <Button
        android:layout_marginTop="20dp"
        android:id="@+id/btn_test"
        android:text="测试"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>
import kotlinx.android.synthetic.main.activity_main.*

What this means is the introduction of Kotlin control variable automatic mapping function, as long as the next activity_main.xml document control, we do not need to call findViewById method to get the objects. As shown below

Here Insert Picture Description
I believe it is easy to understand, layout file TextView's text property is "Hello World!", We adopted the code to "Hello Kotlin", then click on the button we change the button text is "You point a bit!" when you point in the future will change, as well as press code is relatively simple, I posted about

//Button  长按事件
        btn_test.setOnLongClickListener { btn_test.text = "您长按了一会儿";true }

Long press the need to add a Boolean return value, just now we just change the text of the button, then click on the pop-up after us to write a Toast message, this is relatively simple, as follows

//Button  点击事件 Toast消息提示 短消息
        btn_test.setOnClickListener { toast("小提示:您点了一下") }

kotlin of toast default method is briefly shows a message, a message is displayed if you want to grow it? It is also very simple, as follows:

//Button  点击事件 Toast消息提示  长消息
        btn_test.setOnClickListener { longToast("长提示:您点了一下") }

What if we want to change the text button while clicking and pop Toast news?
code show as below:

//Button  点击事件 改变按钮文本并弹出Toast消息
        btn_test.setOnClickListener { btn_test.text = "您点了一下!";toast("小提示:您点了一下")  }

Is said and done, I will introduce a library: Anko library
Description: Anko is using an Android library written in Kotlin language enhancements that simplify Kotlin code when developing Android, to make your code more concise and easy to understand Kotlin , just as we have just used toast and longToast , these two functions are then Anko original definition library like this:

toast

fun Context.toast(message: CharSequence) = Toast.makeText(this, message, Toast.LENGTH_SHORT).show()

longToast

fun Context.longToast(message: CharSequence) = Toast.makeText(this, message, Toast.LENGTH_LONG).show()

Toasts.kt noticed Anko library file is added to the Context class and longToast toast spread function, which means that those who inherited the Context of the class (including the Activity, Service, etc.), can be called toast and longToast direct method in the class internal code implement the message prompts. For normal use, toast and longToast, we need to project build.gradle , in buildscript supplement following line of code in the node,

ext.anko_version = '0.9'//指定Anko的版本

Of course, there may be a later version, the current first with 0.9, as shown below
Here Insert Picture Description
simultaneously, in build.gradle module, the dependencies supplementary node anko-common package the following code:

implementation"org.jetbrains.anko:anko-common:$anko_version"

As shown below
Here Insert Picture Description
OK, is it, I think you can look to the future from the back, continued. . .

Published 25 original articles · won praise 6 · views 9169

Guess you like

Origin blog.csdn.net/qq_38436214/article/details/104259927