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

The first method is for individual controls to write their own click events-in the form of anonymous inner classes

myBtn.setOnClickListener {
    Toast.makeText(this."Sesame seed - Click on the event.", Toast.LENGTH_SHORT).show()
}
Copy the code

😜 way 2

The second method is to implement the view. OnClickListener interface. Multiple click events are processed in the same onClick, using switch in Java and when in Kotlin.

class MainActivity : AppCompatActivity(), View.OnClickListener {

    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) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        myText.setOnClickListener(this)
        myImg.setOnClickListener(this)
        myBtn.setOnClickListener(this)
        imageView.setOnClickListener {
           Toast.makeText(this."Sesame seed - Click on the event.", Toast.LENGTH_SHORT).show()
        }
    }

    override fun onClick(p0: View?) { when (p0? .id) { R.id.myText -> { Toast.makeText(this."Click on text", Toast.LENGTH_SHORT).show()
            }
            R.id.myImg -> {
                Toast.makeText(this."Click on the picture", Toast.LENGTH_SHORT).show()
            }
            R.id.myBtn -> {
                Toast.makeText(this."Click the button.", Toast.LENGTH_SHORT).show()
            }
        }
}
}
Copy the code

Three 😜 way

The third method is to use the [Android :onClick] attribute in the layout, the class directly create the same attribute name method, but according to the actual situation, in the project use less, and the project structure is very complex and easy to chaos, so do not mention this.

In addition to the above three approaches, a real business might add some safeguards to click events: for example, to prevent quick clicks.

Complex business logic often takes time to process, even if it takes 1 second. There will always be anxious or bored users who click the button of the application quickly. If not handled properly, unexpected events will inevitably occur, even a blood collapse.

So young people, there is a price to pay for saving trouble, you don’t do processing to get a moment of freedom, but users endless ridicule, this truth, do you understand?

Take the click event method above for example. If you run and click quickly, you will find that even if you stop clicking, Toast keeps popping up.

So let’s try again with a guarantee:

public abstract class NoFastClickListener implements View.OnClickListener {

    // Minimum quick click interval, at least 1000 ms from the last interval
    private final int MIN_CLICK_TIME = 1000;
    private long lastClickTime = 0;
    @Override
    public void onClick(View view) {
        long currentTime = Calendar.getInstance().getTimeInMillis();
        if(currentTime - lastClickTime > MIN_CLICK_TIME) { lastClickTime = currentTime; onNoFastClick(view); }}public abstract void onNoFastClick(View view);
}
Copy the code

Click events like this:

myBtn.setOnClickListener(object : NoFastClickListener() {
    override fun onNoFastClick(view: View?) {
        Toast.makeText(this@MainActivity."Sesame seed - Preventing clicked events", Toast.LENGTH_SHORT).show()
    }
})
Copy the code

And then you run it and you try it again and you say, hey? Really, there’s not that much toast. That’s really good.

👉 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 📝