Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

👉 About the author

As we all know, life is a long process of constantly overcoming difficulties and reflecting on progress. In this process, there will be a lot of questions and thoughts about life, so I decided to share my thoughts, experiences and stories to find resonance!!

Focus on Android/Unity and various game development tips, as well as various resource sharing (websites, tools, materials, source code, games, etc.)

Welcome to pay attention to the public account [Mr. Empty name] for more resources and communication!

👉 premise

We learned Kotlin earlier, so let’s try Kotlin on Android.

If you are new, please finish learning Android basics first.

Recommended to see the small empty before writing stay up late Android series, and then try.

👉 Practice

A 😜 way

With extensions, if you choose Kotlin when you first create your project, the plug-in will come with it by default, and if you choose Java by default, you will need to add it manually.

The advantage of this approach is that programmers can get the View ID directly, without having to define variables and findViewById.

Add [app->build.gradle] at the beginning of your project’s build file.

apply plugin: ‘kotlin-android-extensions’

Then add an import to the Activity in a fixed format.

import kotlinx.android.synthetic.main. Change to your layout name.*

[*] represents all controls in the layout. If you only need to specify controls, change the name of the control to [*], as shown in the following example

import kotlinx.android.synthetic.main.activity_main.*

import kotlinx.android.synthetic.main.activity_main.mytextview

You can then use the control’S ID directly in your code to respond.

<TextView
        android:id="@+id/myText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:text="Sesame seed and Mr. Empty Name."
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
Copy the code

However, at the same time, there are hidden problems. At the bottom level, the original use of findViewById is still returned, so it will affect the performance.

😜 way 2

With findViewById, there are still two ways to do this. One is the [lateInit] keyword, but it has a pit, See this (in Kotlin code careful lateinit properties zhuanlan.zhihu.com/p/31297995) used recommended way two “lazy”, as follows:


private val myText: TextView by lazy { findViewById<TextView>(R.id.myText) }
private val myImg: ImageView by lazy { findViewById<ImageView>(R.id.myImg) }
private val imageView: ImageView by lazy { findViewById<ImageView>(R.id.imageView) }
private val myBtn: Button by lazy { findViewById<Button>(R.id.myBtn) }
Copy the code

How to use it in a Fragment?

class LoginFragment : Fragment(a){
  private var myText: TextView? = null
  override fun onCreateView(inflater: LayoutInflater, container: ViewGroup? , savedInstanceState: Bundle?): View? {
        // Set the layout
        return inflater.inflate(R.layout.login_fragment, container, false)}override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState) myText = view.findViewById(R.id.myText) myText? .setText("Dynamically modify text")}}Copy the code

Android has given us a better solution:

class LoginFragment : Fragment(a){
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup? , savedInstanceState: Bundle?): View? {
        // Set the layout
        return inflater.inflate(R.layout.login_fragment, container, false)}private lateinit var myText: TextView
    private lateinit var myImg: ImageView
    private lateinit var imageView: ImageView
    private lateinit var myBtn: Button

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        / / find the view
        myText = view.findViewById(R.id.myText)
        myImg = view.findViewById(R.id.myImg)
        imageView = view.findViewById(R.id.imageView)
        myBtn = view.findViewById(R.id.myBtn)
    }
}
Copy the code

Another way to do this is to use an automated plugin. Search the file-setting-plugins market for the keyword findView, find the latest ones, and choose the ones you want to use.

👉 other

📢 author: Kom and Kom in Kom

📢 reprint instructions – be sure to specify the source: Zhim Granular’s personal home page – column – Nuggets (juejin. Cn)

📢 welcome to like 👍 collect 🌟 message 📝